Add basic listing of server schedules

This commit is contained in:
Dane Everitt 2020-02-08 15:23:08 -08:00
parent f9ec96c70a
commit 32e9fb0346
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
21 changed files with 508 additions and 79 deletions

View file

@ -0,0 +1,28 @@
<?php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use Pterodactyl\Transformers\Api\Client\ScheduleTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
class ScheduleController extends ClientApiController
{
/**
* Returns all of the schedules belonging to a given server.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return array
*/
public function index(Request $request, Server $server)
{
$schedules = $server->schedule;
$schedules->loadMissing('tasks');
return $this->fractal->collection($schedules)
->transformWith($this->getTransformer(ScheduleTransformer::class))
->toArray();
}
}

View file

@ -2,6 +2,29 @@
namespace Pterodactyl\Models;
use Illuminate\Container\Container;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
/**
* @property int $id
* @property int $server_id
* @property string $name
* @property string $cron_day_of_week
* @property string $cron_day_of_month
* @property string $cron_hour
* @property string $cron_minute
* @property bool $is_active
* @property bool $is_processing
* @property \Carbon\Carbon|null $last_run_at
* @property \Carbon\Carbon|null $next_run_at
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*
* @property string $hashid
*
* @property \Pterodactyl\Models\Server $server
* @property \Pterodactyl\Models\Task[]|\Illuminate\Support\Collection $tasks
*/
class Schedule extends Validable
{
/**
@ -51,8 +74,6 @@ class Schedule extends Validable
* @var array
*/
protected $dates = [
self::CREATED_AT,
self::UPDATED_AT,
'last_run_at',
'next_run_at',
];
@ -93,7 +114,7 @@ class Schedule extends Validable
*/
public function getHashidAttribute()
{
return app()->make('hashids')->encode($this->id);
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
}
/**

View file

@ -2,8 +2,26 @@
namespace Pterodactyl\Models;
use Illuminate\Container\Container;
use Znck\Eloquent\Traits\BelongsToThrough;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
/**
* @property int $id
* @property int $schedule_id
* @property int $sequence_id
* @property string $action
* @property string $payload
* @property int $time_offset
* @property bool $is_queued
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*
* @property string $hashid
*
* @property \Pterodactyl\Models\Schedule $schedule
* @property \Pterodactyl\Models\Server $server
*/
class Task extends Validable
{
use BelongsToThrough;
@ -83,7 +101,7 @@ class Task extends Validable
*/
public function getHashidAttribute()
{
return app()->make('hashids')->encode($this->id);
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
}
/**

View file

@ -0,0 +1,64 @@
<?php
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\Eloquent\Model;
class ScheduleTransformer extends BaseClientTransformer
{
/**
* @var array
*/
protected $availableIncludes = ['tasks'];
/**
* {@inheritdoc}
*/
public function getResourceName(): string
{
return Schedule::RESOURCE_NAME;
}
/**
* Returns a transformed schedule model such that a client can view the information.
*
* @param \Pterodactyl\Models\Schedule $model
* @return array
*/
public function transform(Schedule $model)
{
return [
'id' => $model->id,
'name' => $model->name,
'cron' => [
'day_of_week' => $model->cron_day_of_week,
'day_of_month' => $model->cron_day_of_month,
'hour' => $model->cron_hour,
'minute' => $model->cron_minute,
],
'is_active' => $model->is_active,
'is_processing' => $model->is_processing,
'last_run_at' => $model->last_run_at ? $model->last_run_at->toIso8601String() : null,
'next_run_at' => $model->next_run_at ? $model->next_run_at->toIso8601String() : null,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];
}
/**
* Allows attaching the tasks specific to the schedule in the response.
*
* @param \Pterodactyl\Models\Schedule $model
* @return \League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeTasks(Schedule $model)
{
return $this->collection(
$model->tasks, $this->makeTransformer(TaskTransformer::class), Task::RESOURCE_NAME
);
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Task;
class TaskTransformer extends BaseClientTransformer
{
/**
* {@inheritdoc}
*/
public function getResourceName(): string
{
return Task::RESOURCE_NAME;
}
/**
* Transforms a schedule's task into a client viewable format.
*
* @param \Pterodactyl\Models\Task $model
* @return array
*/
public function transform(Task $model)
{
return [
'id' => $model->id,
'sequence_id' => $model->sequence_id,
'action' => $model->action,
'payload' => $model->payload,
'time_offset' => $model->time_offset,
'is_queued' => $model->is_queued,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];
}
}