diff --git a/app/Http/Controllers/Admin/ServerController.php b/app/Http/Controllers/Admin/ServerController.php index 6a1395d6..0a1d98b6 100644 --- a/app/Http/Controllers/Admin/ServerController.php +++ b/app/Http/Controllers/Admin/ServerController.php @@ -114,7 +114,7 @@ class ServerController extends Controller */ public function dataTable(Request $request) { - $query = Server::with(['user', 'product', 'egg']); + $query = Server::with(['user', 'product']); if ($request->has('product')) $query->where('product_id', '=', $request->input('product')); if ($request->has('user')) $query->where('user_id', '=', $request->input('user')); $query->select('servers.*'); diff --git a/app/Http/Controllers/Api/ServerController.php b/app/Http/Controllers/Api/ServerController.php index b8e444a4..afe146ca 100644 --- a/app/Http/Controllers/Api/ServerController.php +++ b/app/Http/Controllers/Api/ServerController.php @@ -3,83 +3,81 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; +use App\Models\Server; +use Exception; +use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Http\JsonResponse; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Http\Response; class ServerController extends Controller { /** * Display a listing of the resource. * - * @return \Illuminate\Http\Response + * @param Request $request + * @return LengthAwarePaginator */ - public function index() + public function index(Request $request) { - // + return Server::with('product')->paginate($request->query('per_page') ?? 50); } - /** - * Show the form for creating a new resource. - * - * @return \Illuminate\Http\Response - */ - public function create() - { - // - } - - /** - * Store a newly created resource in storage. - * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response - */ - public function store(Request $request) - { - // - } /** * Display the specified resource. * - * @param int $id - * @return \Illuminate\Http\Response + * @param Server $server + * @return Server */ - public function show($id) + public function show(Server $server) { - // + return $server->load('product'); } - /** - * Show the form for editing the specified resource. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function edit($id) - { - // - } - - /** - * Update the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param int $id - * @return \Illuminate\Http\Response - */ - public function update(Request $request, $id) - { - // - } /** * Remove the specified resource from storage. * - * @param int $id - * @return \Illuminate\Http\Response + * @param Server $server + * @return Server */ - public function destroy($id) + public function destroy(Server $server) { - // + $server->delete(); + return $server; + } + + + /** + * suspend server + * @param Server $server + * @return Server|JsonResponse + */ + public function suspend(Server $server) { + try { + $server->suspend(); + } catch (Exception $exception) { + return response()->json(['message' => $exception->getMessage()] , 500); + } + + return $server->load('product'); + } + + + /** + * unsuspend server + * @param Server $server + * @return Server|JsonResponse + */ + public function unSuspend(Server $server) { + try { + $server->unSuspend(); + } catch (Exception $exception) { + return response()->json(['message' => $exception->getMessage()] , 500); + } + + return $server->load('product'); } } diff --git a/app/Models/Server.php b/app/Models/Server.php index ea83d856..d6f5e178 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -30,7 +30,7 @@ class Server extends Model /** * @var string[] */ - protected static $ignoreChangedAttributes = ['pterodactyl_id' , 'identifier' , 'updated_at']; + protected static $ignoreChangedAttributes = ['pterodactyl_id', 'identifier', 'updated_at']; /** * @var string[] @@ -59,16 +59,17 @@ class Server extends Model /** * */ - public static function boot() { + public static function boot() + { parent::boot(); - static::creating(function(Server $server) { + static::creating(function (Server $server) { $client = new Client(); $server->{$server->getKeyName()} = $client->generateId($size = 21); }); - static::deleting(function(Server $server) { + static::deleting(function (Server $server) { Pterodactyl::client()->delete("/application/servers/{$server->pterodactyl_id}"); }); } @@ -76,7 +77,8 @@ class Server extends Model /** * @return bool */ - public function isSuspended(){ + public function isSuspended() + { return !is_null($this->suspended); } @@ -84,7 +86,8 @@ class Server extends Model /** * @return PromiseInterface|Response */ - public function getPterodactylServer(){ + public function getPterodactylServer() + { return Pterodactyl::client()->get("/application/servers/{$this->pterodactyl_id}"); } @@ -92,7 +95,8 @@ class Server extends Model * * @throws Exception */ - public function suspend(){ + public function suspend() + { $response = Pterodactyl::suspendServer($this); if ($response->successful()) { @@ -120,40 +124,20 @@ class Server extends Model return $this; } - /** * @return HasOne */ - public function product(){ - return $this->hasOne(Product::class , 'id' , 'product_id'); - } - - /** - * @return HasOne - */ - public function nest(){ - return $this->hasOne(Nest::class , 'id' , 'nest_id'); - } - - /** - * @return HasOne - */ - public function egg(){ - return $this->hasOne(Egg::class , 'id' , 'egg_id'); - } - - /** - * @return HasOne - */ - public function location(){ - return $this->hasOne(Location::class , 'id' , 'location_id'); + public function product() + { + return $this->hasOne(Product::class, 'id', 'product_id'); } /** * @return BelongsTo */ - public function user(){ - return $this->belongsTo(User::class , 'user_id' , 'id'); + public function user() + { + return $this->belongsTo(User::class, 'user_id', 'id'); } } diff --git a/routes/api.php b/routes/api.php index fdcd7096..9e228e3e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,5 +1,6 @@ get('/user', function (Request $request) { }); Route::middleware('api.token')->group(function(){ Route::resource('users' , UserController::class)->except(['store' , 'create']); + + Route::patch('/servers/{server}/suspend' , [ServerController::class , 'suspend']); + Route::patch('/servers/{server}/unsuspend' , [ServerController::class , 'unSuspend']); + Route::resource('servers' , ServerController::class)->except(['store' , 'create' , 'edit' , 'update']); });