Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Locations/LocationController.php
10277 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Application\Locations;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Location;
7
use Illuminate\Http\JsonResponse;
8
use Spatie\QueryBuilder\QueryBuilder;
9
use Pterodactyl\Services\Locations\LocationUpdateService;
10
use Pterodactyl\Services\Locations\LocationCreationService;
11
use Pterodactyl\Services\Locations\LocationDeletionService;
12
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
13
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
14
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationRequest;
15
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest;
16
use Pterodactyl\Http\Requests\Api\Application\Locations\StoreLocationRequest;
17
use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest;
18
use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest;
19
20
class LocationController extends ApplicationApiController
21
{
22
/**
23
* LocationController constructor.
24
*/
25
public function __construct(
26
private LocationCreationService $creationService,
27
private LocationDeletionService $deletionService,
28
private LocationUpdateService $updateService,
29
) {
30
parent::__construct();
31
}
32
33
/**
34
* Return all the locations currently registered on the Panel.
35
*/
36
public function index(GetLocationsRequest $request): array
37
{
38
$locations = QueryBuilder::for(Location::query())
39
->allowedFilters(['short', 'long'])
40
->allowedSorts(['id'])
41
->paginate($request->query('per_page') ?? 50);
42
43
return $this->fractal->collection($locations)
44
->transformWith($this->getTransformer(LocationTransformer::class))
45
->toArray();
46
}
47
48
/**
49
* Return a single location.
50
*/
51
public function view(GetLocationRequest $request, Location $location): array
52
{
53
return $this->fractal->item($location)
54
->transformWith($this->getTransformer(LocationTransformer::class))
55
->toArray();
56
}
57
58
/**
59
* Store a new location on the Panel and return an HTTP/201 response code with the
60
* new location attached.
61
*
62
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
63
*/
64
public function store(StoreLocationRequest $request): JsonResponse
65
{
66
$location = $this->creationService->handle($request->validated());
67
68
return $this->fractal->item($location)
69
->transformWith($this->getTransformer(LocationTransformer::class))
70
->addMeta([
71
'resource' => route('api.application.locations.view', [
72
'location' => $location->id,
73
]),
74
])
75
->respond(201);
76
}
77
78
/**
79
* Update a location on the Panel and return the updated record to the user.
80
*
81
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
82
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
83
*/
84
public function update(UpdateLocationRequest $request, Location $location): array
85
{
86
$location = $this->updateService->handle($location, $request->validated());
87
88
return $this->fractal->item($location)
89
->transformWith($this->getTransformer(LocationTransformer::class))
90
->toArray();
91
}
92
93
/**
94
* Delete a location from the Panel.
95
*
96
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
97
*/
98
public function delete(DeleteLocationRequest $request, Location $location): Response
99
{
100
$this->deletionService->handle($location);
101
102
return response('', 204);
103
}
104
}
105
106