Files

80 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2020-03-18 21:08:32 -07:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2020-03-18 21:08:32 -07:00
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Illuminate\Http\JsonResponse;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Response;
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Schedules\CreatesScheduleTasks;
use Pterodactyl\Contracts\Schedules\DeletesScheduleTasks;
use Pterodactyl\Contracts\Schedules\UpdatesScheduleTasks;
2020-06-28 14:41:22 -07:00
use Pterodactyl\Exceptions\Service\ServiceLimitExceededException;
2026-10-11 21:15:38 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromResource;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\DeleteTaskRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\StoreTaskRequest;
2026-10-11 21:15:38 -04:00
use Pterodactyl\Http\Resources\Api\Client\TaskResource;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Task;
2020-03-18 21:08:32 -07:00
2026-10-05 18:10:18 -04:00
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Server Schedules', 'Create, update, execute, delete, and manage scheduled server tasks.')]
2020-03-18 21:08:32 -07:00
class ScheduleTaskController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array LIMIT_ERROR = [
'errors' => [
[
'code' => 'ServiceLimitExceededException',
'status' => '400',
'detail' => 'Schedules may not have more than 10 tasks associated with them. Creating this task would put this schedule over the limit.',
],
],
];
2020-03-18 21:08:32 -07:00
/**
* Create a new task for a given schedule and store it in the database.
*
2024-10-21 19:18:20 -06:00
* @throws ServiceLimitExceededException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Create schedule task', 'Creates a task in a server schedule.')]
2026-10-11 21:15:38 -04:00
#[ResponseFromResource(TaskResource::class, Task::class, description: 'Schedule task created.', resourceKey: 'schedule_task')]
2026-10-05 18:10:18 -04:00
#[ScribeResponse(self::LIMIT_ERROR, status: 400, description: 'The schedule has reached its configured task limit.')]
2026-10-11 21:15:38 -04:00
public function store(StoreTaskRequest $request, CreatesScheduleTasks $tasks, Server $server, Schedule $schedule): TaskResource
{
2026-10-11 06:11:40 -04:00
$task = $tasks->create($schedule, $request->actionInput());
2026-10-11 21:15:38 -04:00
return TaskResource::make($task);
}
/**
* Updates a given task for a server.
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Update schedule task', 'Updates a task in a server schedule, including its sequence position.')]
2026-10-11 21:15:38 -04:00
#[ResponseFromResource(TaskResource::class, Task::class, description: 'Schedule task updated.', resourceKey: 'schedule_task')]
public function update(StoreTaskRequest $request, UpdatesScheduleTasks $tasks, Server $server, Schedule $schedule, Task $task): TaskResource
{
2026-10-11 06:11:40 -04:00
$task = $tasks->update($schedule, $task, $request->actionInput());
2026-10-11 21:15:38 -04:00
return TaskResource::make($task);
}
2020-03-18 21:08:32 -07:00
/**
* Delete a given task for a schedule. If there are subsequent tasks stored in the database
* for this schedule their sequence IDs are decremented properly.
2020-03-18 21:08:32 -07:00
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Delete schedule task', 'Deletes a task from a server schedule and compacts subsequent sequence IDs.')]
#[ScribeResponse(status: 204, description: 'Schedule task deleted.')]
public function delete(DeleteTaskRequest $request, DeletesScheduleTasks $tasks, Server $server, Schedule $schedule, Task $task): JsonResponse
2020-03-18 21:08:32 -07:00
{
2026-10-05 18:10:18 -04:00
$tasks->delete($schedule, $task);
2020-03-18 21:08:32 -07:00
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
2020-03-18 21:08:32 -07:00
}
}