This commit is contained in:
AVMG20 2021-06-06 22:04:46 +02:00
parent 2aabdfdc94
commit cb9cdd8ff2
4 changed files with 74 additions and 87 deletions

View file

@ -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.*');

View file

@ -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');
}
}

View file

@ -59,7 +59,8 @@ class Server extends Model
/**
*
*/
public static function boot() {
public static function boot()
{
parent::boot();
static::creating(function (Server $server) {
@ -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,39 +124,19 @@ class Server extends Model
return $this;
}
/**
* @return HasOne
*/
public function product(){
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');
}
/**
* @return BelongsTo
*/
public function user(){
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}

View file

@ -1,5 +1,6 @@
<?php
use App\Http\Controllers\Api\ServerController;
use App\Http\Controllers\Api\UserController;
use App\Http\Controllers\Api\VerifyController;
use Illuminate\Http\Request;
@ -22,6 +23,10 @@ Route::middleware('auth:api')->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']);
});