misc_pterodactyl-panel/app/Http/Controllers/Api/Application/Mounts/MountController.php

180 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Application\Mounts;
2021-03-05 17:03:12 +00:00
use Illuminate\Http\Response;
2021-08-07 23:10:24 +00:00
use Pterodactyl\Models\Mount;
use Illuminate\Http\JsonResponse;
use Spatie\QueryBuilder\QueryBuilder;
use Pterodactyl\Transformers\Api\Application\MountTransformer;
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
use Pterodactyl\Http\Requests\Api\Application\Mounts\GetMountRequest;
use Pterodactyl\Http\Requests\Api\Application\Mounts\GetMountsRequest;
use Pterodactyl\Http\Requests\Api\Application\Mounts\MountEggsRequest;
use Pterodactyl\Http\Requests\Api\Application\Mounts\MountNodesRequest;
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;
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 = (int) $request->query('per_page', '10');
if ($perPage < 1 || $perPage > 100) {
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
}
$mounts = QueryBuilder::for(Mount::query())
->allowedFilters(['id', 'name', 'source', 'target'])
->allowedSorts(['id', 'name', 'source', 'target'])
->paginate($perPage);
return $this->fractal->collection($mounts)
2021-08-07 21:32:40 +00:00
->transformWith(MountTransformer::class)
->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)
->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)
->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)
->toArray();
}
/**
* Deletes a mount.
*
* @throws \Exception
*/
2021-03-05 17:03:12 +00:00
public function delete(DeleteMountRequest $request, Mount $mount): Response
{
$mount->delete();
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
/**
2021-03-05 17:03:12 +00:00
* Attaches eggs to a mount.
*
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function addEggs(MountEggsRequest $request, Mount $mount): array
{
$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)
->toArray();
}
/**
2021-03-05 17:03:12 +00:00
* Attaches nodes to a mount.
*
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function addNodes(MountNodesRequest $request, Mount $mount): array
{
$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)
->toArray();
}
/**
2021-03-05 17:03:12 +00:00
* Detaches eggs from a mount.
*
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function deleteEggs(MountEggsRequest $request, Mount $mount): array
{
$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)
->toArray();
}
/**
2021-03-05 17:03:12 +00:00
* Detaches nodes from a mount.
*
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function deleteNodes(MountNodesRequest $request, Mount $mount): array
{
$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)
->toArray();
}
}