Process likes in a special method

This commit is contained in:
Pig Fang 2019-03-19 15:19:33 +08:00
parent 9828f66f5d
commit 749339f331
2 changed files with 25 additions and 6 deletions

View File

@ -55,10 +55,6 @@ class SkinlibController extends Controller
// Filter result by uploader's uid
$uploader = intval($request->input('uploader', 0));
// Available sorting methods: time, likes
$sort = $request->input('sort', 'time');
$sortBy = ($sort == 'time') ? 'upload_at' : $sort;
// Current page
$page = $request->input('page', 1);
$currentPage = ($page <= 0) ? 1 : $page;
@ -101,11 +97,22 @@ class SkinlibController extends Controller
$totalPages = ceil($query->count() / $itemsPerPage);
$textures = $query->orderBy($sortBy, 'desc')
->skip(($currentPage - 1) * $itemsPerPage)
// Available sorting methods: time
$sort = $request->input('sort', 'time');
$sortBy = ($sort == 'time') ? 'upload_at' : $sort;
// `likes` property is not actually existed in database.
if ($sortBy != 'likes') {
$query = $query->orderBy($sortBy, 'desc');
}
$textures = $query->skip(($currentPage - 1) * $itemsPerPage)
->take($itemsPerPage)
->get();
if ($sortBy == 'likes') {
$textures = $textures->sortByDesc('likes')->values();
}
if ($user) {
$closet = $user->closet()->get();
foreach ($textures as $item) {

View File

@ -109,6 +109,18 @@ class SkinlibControllerTest extends TestCase
}, $items);
$this->assertArraySubset($ordered, $items);
// Sort by `likes`
$user = factory(User::class)->create();
$user->closet()->attach($skins->random()->tid, ['item_name' => 'name']);
$items = $this->getJson('/skinlib/data?sort=likes')
->assertJson([
'current_uid' => 0,
'total_pages' => 1,
])
->decodeResponseJson('items');
$this->assertEquals(1, $items[0]['likes']);
$this->assertEquals(0, $items[1]['likes']);
// Search
$keyword = Str::limit($skins->random()->name, 1, '');
$keyworded = $skins