Use route model binding

This commit is contained in:
Lance Pioch 2024-03-17 20:57:06 -04:00
parent c8c3b55be8
commit 2aa9be62a1
5 changed files with 14 additions and 23 deletions

View File

@ -29,10 +29,8 @@ class ServerDetailsController extends Controller
* Returns details about the server that allows daemon to self-recover and ensure
* that the state of the server matches the Panel at all times.
*/
public function __invoke(Request $request, string $uuid): JsonResponse
public function __invoke(Server $server): JsonResponse
{
$server = Server::findOrFailByUuid($uuid);
return new JsonResponse([
'settings' => $this->configurationStructureService->handle($server),
'process_configuration' => $this->eggConfigurationService->handle($server),

View File

@ -23,9 +23,8 @@ class ServerInstallController extends Controller
/**
* Returns installation information for a server.
*/
public function index(Request $request, string $uuid): JsonResponse
public function index(Server $server): JsonResponse
{
$server = Server::findOrFailByUuid($uuid);
$egg = $server->egg;
return new JsonResponse([
@ -40,9 +39,8 @@ class ServerInstallController extends Controller
*
* @throws \App\Exceptions\Model\DataValidationException
*/
public function store(InstallationDataRequest $request, string $uuid): JsonResponse
public function store(InstallationDataRequest $request, Server $server): JsonResponse
{
$server = Server::findOrFailByUuid($uuid);
$status = null;
// Make sure the type of failure is accurate

View File

@ -29,9 +29,8 @@ class ServerTransferController extends Controller
*
* @throws \Throwable
*/
public function failure(string $uuid): JsonResponse
public function failure(Server $server): JsonResponse
{
$server = Server::findOrFailByUuid($uuid);
$transfer = $server->transfer;
if (is_null($transfer)) {
throw new ConflictHttpException('Server is not being transferred.');
@ -45,9 +44,8 @@ class ServerTransferController extends Controller
*
* @throws \Throwable
*/
public function success(string $uuid): JsonResponse
public function success(Server $server): JsonResponse
{
$server = Server::findOrFailByUuid($uuid);
$transfer = $server->transfer;
if (is_null($transfer)) {
throw new ConflictHttpException('Server is not being transferred.');

View File

@ -324,6 +324,14 @@ class Server extends Model
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
}
public function resolveRouteBinding($value, $field = null): ?self
{
return match($field) {
'uuid' => $this->where('uuidShort', $value)->orWhere('uuid', $value)->firstOrFail(),
default => $this->where('id', $value)->firstOrFail(),
};
}
/**
* Checks if the server is currently in a user-accessible state. If not, an
* exception is raised. This should be called whenever something needs to make
@ -361,17 +369,6 @@ class Server extends Model
}
}
public static function findOrFailByUuid(string $uuid): Server
{
/** @var Server $server */
$server = Server::with(['node'])
->where('uuidShort', $uuid)
->orWhere('uuid', $uuid)
->firstOrFail();
return $server;
}
/**
* Sends a command or multiple commands to a running server instance.
*

View File

@ -10,7 +10,7 @@ Route::get('/servers', [Remote\Servers\ServerDetailsController::class, 'list']);
Route::post('/servers/reset', [Remote\Servers\ServerDetailsController::class, 'resetState']);
Route::post('/activity', Remote\ActivityProcessingController::class);
Route::group(['prefix' => '/servers/{uuid}'], function () {
Route::group(['prefix' => '/servers/{server:uuid}'], function () {
Route::get('/', Remote\Servers\ServerDetailsController::class);
Route::get('/install', [Remote\Servers\ServerInstallController::class, 'index']);
Route::post('/install', [Remote\Servers\ServerInstallController::class, 'store']);