*/ #[Endpoint('List locations', 'Returns a paginated list of locations.')] #[QueryParam('filter[short]', 'string', 'Filter locations by short identifier.', required: false, example: 'us-east')] #[QueryParam('filter[long]', 'string', 'Filter locations by long description.', required: false, example: 'US East')] #[QueryParam('sort', 'string', 'Sort locations by ID, short identifier, or creation date. Prefix with a hyphen for descending order.', required: false, example: 'short')] #[QueryParam('page', 'integer', 'Page number to return.', required: false, example: 1)] #[QueryParam('per_page', 'integer', 'Results to return per page.', required: false, example: 50)] #[ResponseFromTransformer(LocationTransformer::class, Location::class, description: 'Locations returned.', collection: true, resourceKey: 'location', paginate: [IlluminatePaginatorAdapter::class, 50])] public function index(GetLocationsRequest $request): array { $query = QueryBuilder::for(Location::query()) ->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 array */ #[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)->withExtensionFields()) ->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'])] #[ExtensionFieldsParam] public function store(StoreLocationRequest $request, CreatesLocations $locations): JsonResponse { $location = $locations->create($request->actionInput()); Activity::event('admin:location.create') ->subject($location) ->property('short', $location->short) ->log(); return Fractal::item($location) ->transformWith($this->getTransformer(LocationTransformer::class)->withExtensionFields()) ->addMeta([ 'resource' => route('api.admin.locations.view', [ 'location' => $location->id, ]), ]) ->respond(Response::HTTP_CREATED); } /** * Update location. * * @return array */ #[Endpoint('Update location', 'Updates a location short identifier and description.')] #[ResponseFromTransformer(LocationTransformer::class, Location::class, description: 'Location updated.', resourceKey: 'location')] #[ExtensionFieldsParam] public function update(UpdateLocationRequest $request, UpdatesLocations $locations, Location $location): array { $location = $locations->update($location, $request->actionInput()); Activity::event('admin:location.update') ->subject($location) ->property('short', $location->short) ->log(); return Fractal::item($location) ->transformWith($this->getTransformer(LocationTransformer::class)->withExtensionFields()) ->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(); } }