85 lines
2.4 KiB
PHP
85 lines
2.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Pterodactyl\Http\Controllers\API;
|
||
|
|
||
|
use Illuminate\Http\Request;
|
||
|
|
||
|
use Pterodactyl\Models;
|
||
|
use Pterodactyl\Transformers\ServerTransformer;
|
||
|
use Pterodactyl\Repositories\ServerRepository;
|
||
|
|
||
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
||
|
use Pterodactyl\Exceptions\DisplayException;
|
||
|
use Dingo\Api\Exception\ResourceException;
|
||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
||
|
|
||
|
/**
|
||
|
* @Resource("Servers")
|
||
|
*/
|
||
|
class ServerController extends BaseController
|
||
|
{
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
//
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* List All Servers
|
||
|
*
|
||
|
* Lists all servers currently on the system.
|
||
|
*
|
||
|
* @Get("/servers/{?page}")
|
||
|
* @Versions({"v1"})
|
||
|
* @Parameters({
|
||
|
* @Parameter("page", type="integer", description="The page of results to view.", default=1)
|
||
|
* })
|
||
|
* @Response(200)
|
||
|
*/
|
||
|
public function getServers(Request $request)
|
||
|
{
|
||
|
$servers = Models\Server::paginate(15);
|
||
|
return $this->response->paginator($servers, new ServerTransformer);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* List Specific Server
|
||
|
*
|
||
|
* Lists specific fields about a server or all fields pertaining to that server.
|
||
|
*
|
||
|
* @Get("/servers/{id}/{fields}")
|
||
|
* @Versions({"v1"})
|
||
|
* @Parameters({
|
||
|
* @Parameter("id", type="integer", required=true, description="The ID of the server to get information on."),
|
||
|
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
|
||
|
* })
|
||
|
* @Response(200)
|
||
|
*/
|
||
|
public function getServer(Request $request, $id)
|
||
|
{
|
||
|
$query = Models\Server::where('id', $id);
|
||
|
|
||
|
if (!is_null($request->input('fields'))) {
|
||
|
foreach(explode(',', $request->input('fields')) as $field) {
|
||
|
if (!empty($field)) {
|
||
|
$query->addSelect($field);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
if (!$query->first()) {
|
||
|
throw new NotFoundHttpException('No server by that ID was found.');
|
||
|
}
|
||
|
return $query->first();
|
||
|
} catch (NotFoundHttpException $ex) {
|
||
|
throw $ex;
|
||
|
} catch (\Exception $ex) {
|
||
|
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|