forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLatestController.php
More file actions
44 lines (39 loc) · 1.39 KB
/
GetLatestController.php
File metadata and controls
44 lines (39 loc) · 1.39 KB
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
37
38
39
40
41
42
43
44
<?php
namespace App\Http\Controllers\API\Speedtest;
use App\Http\Controllers\Controller;
use App\Models\Result;
use Illuminate\Http\JsonResponse;
class GetLatestController extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(): JsonResponse
{
$latest = Result::query()
->latest()
->first();
if (! $latest) {
return response()->json([
'message' => 'No results found.',
], 404);
}
return response()->json([
'message' => 'ok',
'data' => [
'id' => $latest->id,
'ping' => $latest->ping,
'download' => ! blank($latest->download) ? toBits(convertSize($latest->download)) : null,
'upload' => ! blank($latest->upload) ? toBits(convertSize($latest->upload)) : null,
'server_id' => $latest->server_id,
'server_host' => $latest->server_host,
'server_name' => $latest->server_name,
'url' => $latest->url,
'scheduled' => $latest->scheduled,
'failed' => ! $latest->successful,
'created_at' => $latest->created_at->toISOString(true),
'updated_at' => $latest->created_at->toISOString(true), // faking updated at to match legacy api payload
],
]);
}
}