allowedFilters(['short', 'long']) ->allowedSorts(['id', 'short', 'created_at']); $locations = $query->getEloquentBuilder() ->withCount(['nodes', 'servers']) ->paginate($request->perPage()); return Fractal::collection($locations) ->transformWith($this->getTransformer(LocationTransformer::class)) ->toResponseArray(); } /** * Show location. * * @return ApiPayload */ #[Endpoint('Get location', 'Returns a single location by ID.')] #[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "nodes" and "servers".', required: false, example: 'nodes', enum: ['nodes', 'servers', 'nodes,servers'])] #[ResponseFromTransformer(LocationTransformer::class, Location::class, description: 'Location returned.', resourceKey: 'location', include: ['nodes'])] public function show(GetLocationRequest $request, Location $location): array { $location->loadCount(['nodes', 'servers']); $location->load(['nodes' => function (HasMany $query): void { $query->withCount('servers'); }]); return Fractal::item($location) ->transformWith($this->getTransformer(LocationTransformer::class)) ->toResponseArray(); } /** * Create location. */ #[Endpoint('Create location', 'Creates a location for grouping nodes and servers.')] #[ResponseFromTransformer(LocationTransformer::class, Location::class, status: 201, description: 'Location created.', resourceKey: 'location', meta: ['resource' => 'https://panel.example.com/api/admin/locations/1'])] public function store(StoreLocationRequest $request, CreatesLocations $locations): JsonResponse { $location = $locations->create($request->payload()); Activity::event('admin:location.create') ->subject($location) ->property('short', $location->short) ->log(); return Fractal::item($location) ->transformWith($this->getTransformer(LocationTransformer::class)) ->addMeta([ 'resource' => route('api.admin.locations.view', [ 'location' => $location->id, ]), ]) ->respond(Response::HTTP_CREATED); } /** * Update location. * * @return ApiPayload */ #[Endpoint('Update location', 'Updates a location short identifier and description.')] #[ResponseFromTransformer(LocationTransformer::class, Location::class, description: 'Location updated.', resourceKey: 'location')] public function update(UpdateLocationRequest $request, UpdatesLocations $locations, Location $location): array { $location = $locations->update($location, $request->payload()); Activity::event('admin:location.update') ->subject($location) ->property('short', $location->short) ->log(); return Fractal::item($location) ->transformWith($this->getTransformer(LocationTransformer::class)) ->toResponseArray(); } /** * Delete location. */ #[Endpoint('Delete location', 'Deletes a location when no dependent resources prevent removal.')] #[ScribeResponse(status: 204, description: 'Location deleted.')] public function destroy(DeleteLocationRequest $request, DeletesLocations $locations, Location $location): Response { $locations->delete($location); Activity::event('admin:location.delete') ->subject($location) ->property('short', $location->short) ->log(); return $this->returnNoContent(); } }