Skip to content
Merged
3 changes: 2 additions & 1 deletion app/Http/Controllers/RequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
16 changes: 11 additions & 5 deletions app/Storage/Redis/RequestStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,33 @@ 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()
->map(
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();
Expand Down
3 changes: 2 additions & 1 deletion app/Storage/RequestStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions tests/RequestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}