[Breaking] Return server allocations automatically as a relation object

This commit is contained in:
Dane Everitt 2020-07-09 19:17:24 -07:00
parent 5c18fd1f0c
commit bfb28f949d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 26 additions and 13 deletions

View file

@ -28,10 +28,12 @@ abstract class ClientApiController extends ApplicationApiController
/**
* Returns the parsed includes for this request.
*
* @return string[]
*/
protected function parseIncludes()
{
$includes = $this->request->query('include');
$includes = $this->request->query('include') ?? [];
if (! is_string($includes)) {
return $includes;

View file

@ -24,13 +24,11 @@ class AllocationTransformer extends BaseClientTransformer
*/
public function transform(Allocation $model)
{
$model->loadMissing('server');
return [
'ip' => $model->ip,
'alias' => $model->ip_alias,
'ip_alias' => $model->ip_alias,
'port' => $model->port,
'default' => $model->getRelation('server')->allocation_id === $model->id,
'is_default' => $model->server->allocation_id === $model->id,
];
}
}

View file

@ -9,6 +9,11 @@ use Pterodactyl\Models\Allocation;
class ServerTransformer extends BaseClientTransformer
{
/**
* @var string[]
*/
protected $defaultIncludes = ['allocations'];
/**
* @var array
*/
@ -42,14 +47,6 @@ class ServerTransformer extends BaseClientTransformer
'port' => $server->node->daemonSFTP,
],
'description' => $server->description,
'allocations' => $server->allocations->map(function (Allocation $allocation) use ($server) {
return [
'ip' => $allocation->ip,
'ip_alias' => $allocation->ip_alias,
'port' => $allocation->port,
'is_default' => $allocation->id === $server->allocation_id,
];
}),
'limits' => [
'memory' => $server->memory,
'swap' => $server->swap,
@ -67,6 +64,22 @@ class ServerTransformer extends BaseClientTransformer
];
}
/**
* Returns the allocations associated with this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Collection
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeAllocations(Server $server)
{
return $this->collection(
$server->allocations,
$this->makeTransformer(AllocationTransformer::class),
Allocation::RESOURCE_NAME
);
}
/**
* Returns the egg associated with this server.
*