diff --git a/app/Http/Controllers/RequestController.php b/app/Http/Controllers/RequestController.php index ff024de3..f0185269 100644 --- a/app/Http/Controllers/RequestController.php +++ b/app/Http/Controllers/RequestController.php @@ -100,9 +100,10 @@ private function guardOverQuota($token) public function all(HttpRequest $httpRequest, $tokenId) { $token = $this->tokens->find($tokenId); + $sorting = $httpRequest->get('sorting', 'oldest'); $page = (int)$httpRequest->get('page', 1); $perPage = (int)$httpRequest->get('per_page', 50); - $requests = $this->requests->all($token, $page, $perPage); + $requests = $this->requests->all($token, $page, $perPage, $sorting); $total = $this->tokens->countRequests($token); return new JsonResponse([ diff --git a/app/Storage/Redis/RequestStore.php b/app/Storage/Redis/RequestStore.php index 8f2c3965..01ba1276 100644 --- a/app/Storage/Redis/RequestStore.php +++ b/app/Storage/Redis/RequestStore.php @@ -47,11 +47,12 @@ public function find(Token $token, $requestId) * @param Token $token * @param int $page * @param int $perPage + * @param string $sort * @return Collection|static */ - public function all(Token $token, $page = 1, $perPage = 50) + public function all(Token $token, $page = 1, $perPage = 50, $sorting = 'oldest') { - return collect( + $requests = collect( $this->redis->hgetall(Request::getIdentifier($token->uuid)) ) ->filter() @@ -59,15 +60,20 @@ public function all(Token $token, $page = 1, $perPage = 50) function ($request) { return json_decode($request); } - )->sortBy( + ); + + $requests = $requests->sortBy( function ($request) { return Carbon::createFromFormat( 'Y-m-d H:i:s', $request->created_at )->getTimestamp(); }, - SORT_DESC - )->forPage( + SORT_REGULAR, + $sorting === 'newest' + ); + + return $requests->forPage( $page, $perPage )->values(); diff --git a/app/Storage/RequestStore.php b/app/Storage/RequestStore.php index f105cbb4..7c29c456 100644 --- a/app/Storage/RequestStore.php +++ b/app/Storage/RequestStore.php @@ -17,9 +17,10 @@ public function find(Token $token, $requestId); * @param Token $token * @param int $page * @param int $perPage + * @param string $sorting * @return Collection */ - public function all(Token $token, $page = 1, $perPage = 50); + public function all(Token $token, $page = 1, $perPage = 50, $sorting = 'oldest'); /** * @param Token $token diff --git a/tests/RequestControllerTest.php b/tests/RequestControllerTest.php index 89ed5a27..78e0fe04 100644 --- a/tests/RequestControllerTest.php +++ b/tests/RequestControllerTest.php @@ -65,4 +65,53 @@ public function testPagination() 'to' => 175, ]); } + + public function testSorting() { + // Prevent throttling + $this->withoutMiddleware(); + + $number = 175; + + $tokenId = $this->json('POST', 'token')->json()['uuid']; + + for ($i = 0; $i < $number; $i++) { + $this->call('GET', $tokenId); + } + + // Test newest + $requests = $this->json('GET', "token/$tokenId/requests?sorting=newest"); + + $requests->assertJson([ + 'total' => $number, + 'per_page' => 50, + 'current_page' => 1, + 'is_last_page' => false, + 'from' => 1, + 'to' => 50, + ]); + + $data = $requests->json()['data']; + $timestamps = array_column($data, 'created_at'); + $sortedDescTimestamps = $timestamps; + rsort($sortedDescTimestamps); + $this->assertSame($sortedDescTimestamps, $timestamps, "The 'created_at' field is not sorted in descending order."); + + // Test oldest + $requests = $this->json('GET', "token/$tokenId/requests?sorting=oldest"); + + $requests->assertJson([ + 'total' => $number, + 'per_page' => 50, + 'current_page' => 1, + 'is_last_page' => false, + 'from' => 1, + 'to' => 50, + ]); + + $data = $requests->json()['data']; + $timestamps = array_column($data, 'created_at'); + $sortedAscTimestamps = $timestamps; + sort($sortedAscTimestamps); + $this->assertSame($sortedAscTimestamps, $timestamps,'The "created_at" field is not sorted in ascending order.'); + } }