diff --git a/app/Http/Controllers/Admin/ServerController.php b/app/Http/Controllers/Admin/ServerController.php index 9cdcadf..5e01f74 100755 --- a/app/Http/Controllers/Admin/ServerController.php +++ b/app/Http/Controllers/Admin/ServerController.php @@ -122,10 +122,9 @@ public function update(ServerUpdateRequest $request, Server $server, Pterodactyl * Remove the specified resource from storage. * * @param Server $server - * @param PterodactylClient $client * @return RedirectResponse */ - public function destroy(Server $server, PterodactylClient $client) + public function destroy(Server $server) { $this->checkPermission(self::WRITE_PERMISSIONS); diff --git a/app/Http/Controllers/CheckoutController.php b/app/Http/Controllers/User/CheckoutController.php similarity index 91% rename from app/Http/Controllers/CheckoutController.php rename to app/Http/Controllers/User/CheckoutController.php index 6967b3e..d69754e 100755 --- a/app/Http/Controllers/CheckoutController.php +++ b/app/Http/Controllers/User/CheckoutController.php @@ -1,10 +1,11 @@ back()->with('error', __('Unexpected error during server creation')); } - return redirect()->route('dashboard')->with('success', __('Server created successfully!')); + return redirect()->route('dashboard.index')->with('success', __('Server created successfully!')); } diff --git a/app/Http/Controllers/User/HomeController.php b/app/Http/Controllers/User/HomeController.php old mode 100755 new mode 100644 index f66e14d..548d854 --- a/app/Http/Controllers/User/HomeController.php +++ b/app/Http/Controllers/User/HomeController.php @@ -8,6 +8,7 @@ use App\Settings\GeneralSettings; use Exception; use Illuminate\Contracts\Support\Renderable; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Blade; use Yajra\DataTables\Html\Builder; @@ -24,16 +25,33 @@ public function index(Request $request, GeneralSettings $settings) { /** @var User $user */ $user = $request->user(); + $servers = $user->servers; //datatables if ($request->ajax()) { - return $this->dataTableQuery(); + return $this->dataTableQuery($request); } $html = $this->dataTable(); return view('home', compact('html', 'settings', 'user')); } + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return RedirectResponse + */ + public function destroy(int $id): RedirectResponse + { + $server = Server::query()->findOrFail($id); + $server->delete(); + + return redirect() + ->route('dashboard.index') + ->with('success', __('Server removed')); + } + /** * @description create table * @@ -41,37 +59,31 @@ public function index(Request $request, GeneralSettings $settings) */ public function dataTable(): Builder { - /** @var GeneralSettings $settings */ - $settings = app(GeneralSettings::class); - return $this->htmlBuilder ->addColumn(['data' => 'name', 'name' => 'name', 'title' => __('Name')]) - ->addColumn(['data' => 'price', 'name' => 'price', 'title' => $settings->credits_display_name]) - ->addColumn(['data' => 'cpu', 'name' => 'cpu', 'title' => __('CPU')]) - ->addColumn(['data' => 'memory', 'name' => 'memory', 'title' => __('Memory')]) - ->addColumn(['data' => 'disk', 'name' => 'disk', 'title' => __('Disk')]) - ->addColumn(['data' => 'egg.name', 'name' => 'egg.name', 'title' => __('Egg')]) + ->addColumn(['data' => 'price', 'name' => 'price', 'title' => __('Cost')]) + ->addColumn(['data' => 'egg.name', 'name' => 'egg.name', 'title' => __('Config')]) + ->addColumn(['data' => 'details', 'name' => 'details', 'title' => __('Details'),'searchable' => false, 'orderable' => false]) ->addColumn(['data' => 'suspended', 'name' => 'suspended', 'title' => __('Suspended')]) ->addAction(['data' => 'actions', 'name' => 'actions', 'title' => __('Actions'), 'searchable' => false, 'orderable' => false]) ->parameters($this->dataTableDefaultParameters()); } /** + * @param Request $request * @return mixed * @throws Exception */ - public function dataTableQuery(): mixed + public function dataTableQuery(Request $request): mixed { - $query = Server::query()->with('egg'); + $query = Server::query() + ->with(['egg']) + ->whereBelongsTo($request->user()); return datatables($query) ->addColumn('actions', function (Server $server) { return Blade::render(' - - - - -
' , compact('server')); }) + ->addColumn('details', function (Server $server) { + return "CPU: {$server->cpu}%, RAM: {$server->memory}MB, DISK:{$server->disk}MB"; + }) + ->editColumn('price', function (Server $server) { + return "{$server->price}"; + }) ->editColumn('suspended', function (Server $server) { return $server->suspended ? "" . __('Suspended') . "" : "" . __('Active') . ""; }) - ->rawColumns(['actions', 'suspended']) + ->rawColumns(['actions', 'suspended', 'price']) ->make(true); } } diff --git a/app/Http/Livewire/Checkout.php b/app/Http/Livewire/Checkout.php index 258baea..5b1cdb2 100755 --- a/app/Http/Livewire/Checkout.php +++ b/app/Http/Livewire/Checkout.php @@ -117,7 +117,7 @@ public function createServer(PterodactylServerHelper $pterodactylServerHelper, P return redirect()->back()->with('error', __('Unexpected error during server creation')); } - return redirect()->route('dashboard')->with('success', __('Server created successfully!')); + return redirect()->route('dashboard.index')->with('success', __('Server created successfully!')); } /** diff --git a/app/Http/Requests/Configuration/ConfigurationRequest.php b/app/Http/Requests/Configuration/ConfigurationRequest.php index fea1da7..d9ae4b9 100755 --- a/app/Http/Requests/Configuration/ConfigurationRequest.php +++ b/app/Http/Requests/Configuration/ConfigurationRequest.php @@ -43,7 +43,7 @@ public function rules() 'backups' => 'required|integer|max:1000000|min:0', 'allocations' => 'required|integer|max:1000000|min:0', 'locations' => 'required', - 'locations.*' => 'required|exists:nodes,id', + 'locations.*' => 'required|exists:nodes,location_id', 'eggs' => 'required', 'eggs.*' => 'required|exists:eggs,id', 'disabled' => 'required|boolean', diff --git a/app/Models/Server.php b/app/Models/Server.php index 2ab8fc4..d0c0c5c 100755 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Http\Client\Response; class Server extends Model @@ -35,10 +36,10 @@ class Server extends Model 'allocations', 'threads', 'oom_disabled', - 'node', - 'allocation', - 'nest', - 'egg', + 'node_id', + 'allocation_id', + 'nest_id', + 'egg_id', 'price', ]; @@ -58,9 +59,10 @@ class Server extends Model 'io' => 'int', 'databases' => 'int', 'backups' => 'int', - 'allocations' => 'int', - 'nest' => 'int', - 'egg' => 'int', + 'allocations_id' => 'int', + 'nest_id' => 'int', + 'egg_id' => 'int', + 'node_id' => 'int', ]; protected $appends = [ @@ -80,6 +82,7 @@ protected static function boot() $client = app(PterodactylClient::class); try { + //delete server on pterodactyl $client->deleteServer($server->pterodactyl_id); } catch (PterodactylRequestException $exception) { //throw exception if it's not a 404 error @@ -119,10 +122,10 @@ public static function createFromPterodactylResponse(Response $response, User $u 'databases' => $data['attributes']['feature_limits']['databases'], 'backups' => $data['attributes']['feature_limits']['backups'], 'allocations' => $data['attributes']['feature_limits']['allocations'], - 'node' => $data['attributes']['node'], - 'allocation' => $data['attributes']['allocation'], - 'nest' => $data['attributes']['nest'], - 'egg' => $data['attributes']['egg'], + 'node_id' => $data['attributes']['node'], + 'allocation_id' => $data['attributes']['allocation'], + 'nest_id' => $data['attributes']['nest'], + 'egg_id' => $data['attributes']['egg'], 'price' => $price ]); } @@ -157,6 +160,19 @@ public function pterodactylAdminUrl(): Attribute }); } + /** + * Get the price per hour + * + * @return Attribute + */ + public function price(): Attribute + { + return Attribute::make( + get: fn($value) => number_format($value, '2', '.', ''), + set: fn($value) => floatval($value), + ); + } + /** * Get the price per hour * @@ -164,7 +180,7 @@ public function pterodactylAdminUrl(): Attribute */ public function pricePerHour(): Attribute { - return Attribute::get(fn() => floatval(number_format($this->price / 30 / 24, 6))); + return Attribute::get(fn() => number_format($this->price / 30 / 24, '2', '.', '')); } /** @@ -174,7 +190,7 @@ public function pricePerHour(): Attribute */ public function pricePerDay(): Attribute { - return Attribute::get(fn() => floatval(number_format($this->price / 30, 6))); + return Attribute::get(fn() => number_format($this->price / 30, '2', '.', '')); } /** @@ -194,7 +210,7 @@ public function user(): BelongsTo */ public function egg(): BelongsTo { - return $this->belongsTo(Egg::class, 'egg'); + return $this->belongsTo(Egg::class); } /** @@ -204,6 +220,6 @@ public function egg(): BelongsTo */ public function node(): BelongsTo { - return $this->belongsTo(Node::class, 'node'); + return $this->belongsTo(Node::class, ); } } diff --git a/database/factories/ServerFactory.php b/database/factories/ServerFactory.php index 0826eef..e899d64 100644 --- a/database/factories/ServerFactory.php +++ b/database/factories/ServerFactory.php @@ -2,7 +2,6 @@ namespace Database\Factories; -use App\Models\Server; use Illuminate\Database\Eloquent\Factories\Factory; /** diff --git a/database/migrations/2022_03_08_201654_create_servers_table.php b/database/migrations/2022_03_08_201654_create_servers_table.php index ab9b65b..61596d9 100755 --- a/database/migrations/2022_03_08_201654_create_servers_table.php +++ b/database/migrations/2022_03_08_201654_create_servers_table.php @@ -32,10 +32,10 @@ public function up() $table->unsignedInteger('allocations'); $table->unsignedInteger('threads')->nullable(); $table->boolean('oom_disabled')->default(true); - $table->unsignedInteger('node'); - $table->unsignedInteger('allocation'); - $table->unsignedInteger('nest'); - $table->unsignedInteger('egg'); + $table->unsignedInteger('node_id'); + $table->unsignedInteger('allocation_id'); + $table->unsignedInteger('nest_id'); + $table->unsignedInteger('egg_id'); $table->float('price', 18, 6)->default(0); $table->timestamps(); }); diff --git a/resources/views/checkout.blade.php b/resources/views/checkout.blade.php index 7464cd5..3bea78d 100755 --- a/resources/views/checkout.blade.php +++ b/resources/views/checkout.blade.php @@ -30,7 +30,7 @@