Add server details modification endpoint to API.

This commit is contained in:
Dane Everitt 2018-01-20 16:03:23 -06:00
parent 3e327b8b0e
commit 17f6f3eeb6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
7 changed files with 160 additions and 267 deletions

View file

@ -401,7 +401,7 @@ class ServersController extends Controller
*/
public function setDetails(Request $request, Server $server)
{
$this->detailsModificationService->edit($server, $request->only([
$this->detailsModificationService->handle($server, $request->only([
'owner_id', 'name', 'description',
]));

View file

@ -0,0 +1,47 @@
<?php
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Servers\DetailsModificationService;
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest;
class ServerDetailsController extends ApplicationApiController
{
/**
* @var \Pterodactyl\Services\Servers\DetailsModificationService
*/
private $modificationService;
/**
* ServerDetailsController constructor.
*
* @param \Pterodactyl\Services\Servers\DetailsModificationService $modificationService
*/
public function __construct(DetailsModificationService $modificationService)
{
parent::__construct();
$this->modificationService = $modificationService;
}
/**
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\UpdateServerDetailsRequest $request
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function details(UpdateServerDetailsRequest $request, Server $server): array
{
$server = $this->modificationService->returnUpdatedModel()->handle($server, $request->validated());
return $this->fractal->item($server)
->transformWith($this->getTransformer(ServerTransformer::class))
->toArray();
}
}

View file

@ -0,0 +1,53 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
use Pterodactyl\Models\Server;
class UpdateServerDetailsRequest extends ServerWriteRequest
{
/**
* Rules to apply to a server details update request.
*
* @return array
*/
public function rules(): array
{
$rules = Server::getUpdateRulesForId($this->route()->parameter('server')->id);
return [
'name' => $rules['name'],
'user' => $rules['owner_id'],
'description' => $rules['description'],
];
}
/**
* Convert the posted data into the correct format that is expected
* by the application.
*
* @return array
*/
public function validated(): array
{
return [
'name' => $this->input('name'),
'owner_id' => $this->input('user'),
'description' => $this->input('description'),
];
}
/**
* Rename some of the attributes in error messages to clarify the field
* being discussed.
*
* @return array
*/
public function attributes(): array
{
return [
'user' => 'User ID',
'name' => 'Server Name',
];
}
}