forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLatestResult.php
More file actions
36 lines (32 loc) · 996 Bytes
/
LatestResult.php
File metadata and controls
36 lines (32 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Resources\V1\ResultResource;
use App\Models\Result;
use Http\Discovery\Exception\NotFoundException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use OpenApi\Attributes as OA;
class LatestResult extends ApiController
{
#[OA\Get(
path: '/api/v1/results/latest',
description: 'Get the latest result.',
responses: [
new OA\Response(response: 200, description: 'OK'),
new OA\Response(response: Response::HTTP_NOT_FOUND, description: 'No result found'),
])]
public function __invoke(Request $request)
{
$result = Result::query()
->latest()
->firstOr(function () {
self::throw(
e: new NotFoundException('No result found.'),
code: 404,
);
});
return self::sendResponse(
data: new ResultResource($result),
);
}
}