diff --git a/app/Http/Controllers/API/Speedtest/GetLatestController.php b/app/Http/Controllers/API/Speedtest/GetLatestController.php new file mode 100644 index 000000000..849bc300d --- /dev/null +++ b/app/Http/Controllers/API/Speedtest/GetLatestController.php @@ -0,0 +1,43 @@ +latest() + ->firstOr(function () { + return response()->json([ + 'message' => 'No results found.', + ], 404); + }); + + return response()->json([ + 'message' => 'ok', + 'data' => [ + 'id' => $latest->id, + 'ping' => $latest->ping, + 'download' => ! blank($latest->download) ? formatBits(formatBytestoBits($latest->download), 4, false) : null, + 'upload' => ! blank($latest->upload) ? formatBits(formatBytestoBits($latest->upload), 4, false) : 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(), + 'updated_at' => $latest->created_at->toISOString(), // faking updated at to match legacy api payload + ], + ]); + } +} diff --git a/routes/api.php b/routes/api.php index eb6fa48c2..96a514837 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,5 +1,6 @@ get('/user', function (Request $request) { return $request->user(); }); + +Route::get('/speedtest/latest', GetLatestController::class) + ->name('speedtest.latest');