2021-01-05 21:52:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Mounts;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Mount;
|
2021-03-05 17:03:12 +00:00
|
|
|
use Illuminate\Http\Response;
|
2021-01-05 21:52:49 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\MountTransformer;
|
2021-03-23 21:57:29 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
2021-01-05 21:52:49 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Mounts\GetMountRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Mounts\GetMountsRequest;
|
2021-01-15 16:20:15 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Mounts\MountEggsRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Mounts\MountNodesRequest;
|
2021-01-05 21:52:49 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Mounts\StoreMountRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Mounts\DeleteMountRequest;
|
2021-03-05 17:03:12 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Mounts\UpdateMountRequest;
|
2021-01-05 21:52:49 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
|
|
|
|
|
|
class MountController extends ApplicationApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* MountController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of all mount.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function index(GetMountsRequest $request): array
|
|
|
|
{
|
|
|
|
$perPage = $request->query('per_page', 10);
|
2021-03-23 21:57:29 +00:00
|
|
|
if ($perPage < 1 || $perPage > 100) {
|
|
|
|
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
|
2021-01-05 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$mounts = QueryBuilder::for(Mount::query())
|
2021-07-14 22:43:59 +00:00
|
|
|
->allowedFilters(['id', 'name', 'source', 'target'])
|
|
|
|
->allowedSorts(['id', 'name', 'source', 'target'])
|
2021-01-05 21:52:49 +00:00
|
|
|
->paginate($perPage);
|
|
|
|
|
|
|
|
return $this->fractal->collection($mounts)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-05 21:52:49 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single mount.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function view(GetMountRequest $request, Mount $mount): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($mount)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-05 21:52:49 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new mount.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function store(StoreMountRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
$mount = Mount::query()->create($request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($mount)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-05 21:52:49 +00:00
|
|
|
->respond(JsonResponse::HTTP_CREATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a mount.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function update(UpdateMountRequest $request, Mount $mount): array
|
|
|
|
{
|
|
|
|
$mount->update($request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($mount)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-05 21:52:49 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a mount.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function delete(DeleteMountRequest $request, Mount $mount): Response
|
2021-01-05 21:52:49 +00:00
|
|
|
{
|
|
|
|
$mount->delete();
|
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2021-01-05 21:52:49 +00:00
|
|
|
}
|
2021-01-14 18:04:19 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-05 17:03:12 +00:00
|
|
|
* Attaches eggs to a mount.
|
2021-01-14 18:04:19 +00:00
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2021-01-14 18:04:19 +00:00
|
|
|
*/
|
2021-01-15 16:20:15 +00:00
|
|
|
public function addEggs(MountEggsRequest $request, Mount $mount): array
|
2021-01-14 18:04:19 +00:00
|
|
|
{
|
|
|
|
$data = $request->validated();
|
|
|
|
|
|
|
|
$eggs = $data['eggs'] ?? [];
|
|
|
|
if (count($eggs) > 0) {
|
|
|
|
$mount->eggs()->syncWithoutDetaching($eggs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fractal->item($mount)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-14 18:04:19 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-05 17:03:12 +00:00
|
|
|
* Attaches nodes to a mount.
|
2021-01-14 18:04:19 +00:00
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2021-01-14 18:04:19 +00:00
|
|
|
*/
|
2021-01-15 16:20:15 +00:00
|
|
|
public function addNodes(MountNodesRequest $request, Mount $mount): array
|
2021-01-14 18:04:19 +00:00
|
|
|
{
|
|
|
|
$data = $request->validated();
|
|
|
|
|
|
|
|
$nodes = $data['nodes'] ?? [];
|
|
|
|
if (count($nodes) > 0) {
|
|
|
|
$mount->nodes()->syncWithoutDetaching($nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fractal->item($mount)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-14 18:04:19 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-05 17:03:12 +00:00
|
|
|
* Detaches eggs from a mount.
|
2021-01-14 18:04:19 +00:00
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2021-01-14 18:04:19 +00:00
|
|
|
*/
|
2021-01-15 16:20:15 +00:00
|
|
|
public function deleteEggs(MountEggsRequest $request, Mount $mount): array
|
2021-01-14 18:04:19 +00:00
|
|
|
{
|
|
|
|
$data = $request->validated();
|
|
|
|
|
|
|
|
$eggs = $data['eggs'] ?? [];
|
|
|
|
if (count($eggs) > 0) {
|
|
|
|
$mount->eggs()->detach($eggs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fractal->item($mount)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-14 18:04:19 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-05 17:03:12 +00:00
|
|
|
* Detaches nodes from a mount.
|
2021-01-14 18:04:19 +00:00
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2021-01-14 18:04:19 +00:00
|
|
|
*/
|
2021-01-15 16:20:15 +00:00
|
|
|
public function deleteNodes(MountNodesRequest $request, Mount $mount): array
|
2021-01-14 18:04:19 +00:00
|
|
|
{
|
|
|
|
$data = $request->validated();
|
|
|
|
|
|
|
|
$nodes = $data['nodes'] ?? [];
|
|
|
|
if (count($nodes) > 0) {
|
|
|
|
$mount->nodes()->detach($nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fractal->item($mount)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(MountTransformer::class)
|
2021-01-14 18:04:19 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
2021-01-05 21:52:49 +00:00
|
|
|
}
|