diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 97407644..1c15d905 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -3,6 +3,10 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; +use App\Models\Egg; +use App\Models\Location; +use App\Models\Nest; +use App\Models\Node; use App\Models\Product; use Exception; use Illuminate\Contracts\Foundation\Application; @@ -32,7 +36,10 @@ class ProductController extends Controller */ public function create() { - return view('admin.products.create'); + return view('admin.products.create' , [ + 'locations' => Location::with('nodes')->get(), + 'nests' => Nest::with('eggs')->get(), + ]); } /** @@ -55,11 +62,17 @@ class ProductController extends Controller "databases" => "required|numeric|max:1000000|min:0", "backups" => "required|numeric|max:1000000|min:0", "allocations" => "required|numeric|max:1000000|min:0", + "nodes.*" => "required|exists:nodes,id", + "eggs.*" => "required|exists:eggs,id", "disabled" => "nullable", ]); $disabled = !is_null($request->input('disabled')); - Product::create(array_merge($request->all(), ['disabled' => $disabled])); + $product = Product::create(array_merge($request->all(), ['disabled' => $disabled])); + + #link nodes and eggs + $product->eggs()->attach($request->input('eggs')); + $product->nodes()->attach($request->input('nodes')); return redirect()->route('admin.products.index')->with('success', 'product has been created!'); } @@ -86,7 +99,9 @@ class ProductController extends Controller public function edit(Product $product) { return view('admin.products.edit', [ - 'product' => $product + 'product' => $product, + 'locations' => Location::with('nodes')->get(), + 'nests' => Nest::with('eggs')->get(), ]); } @@ -111,12 +126,20 @@ class ProductController extends Controller "databases" => "required|numeric|max:1000000|min:0", "backups" => "required|numeric|max:1000000|min:0", "allocations" => "required|numeric|max:1000000|min:0", + "nodes.*" => "required|exists:nodes,id", + "eggs.*" => "required|exists:eggs,id", "disabled" => "nullable", ]); $disabled = !is_null($request->input('disabled')); $product->update(array_merge($request->all(), ['disabled' => $disabled])); + #link nodes and eggs + $product->eggs()->detach(); + $product->nodes()->detach(); + $product->eggs()->attach($request->input('eggs')); + $product->nodes()->attach($request->input('nodes')); + return redirect()->route('admin.products.index')->with('success', 'product has been updated!'); } @@ -174,6 +197,12 @@ class ProductController extends Controller ->addColumn('servers', function (Product $product) { return $product->servers()->count(); }) + ->addColumn('nodes', function (Product $product) { + return $product->nodes()->count(); + }) + ->addColumn('eggs', function (Product $product) { + return $product->eggs()->count(); + }) ->addColumn('disabled', function (Product $product) { $checked = $product->disabled == false ? "checked" : ""; return ' diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index bb6838f3..f5e75658 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers; +use App\Models\Egg; +use App\Models\Product; use App\Models\UsefulLink; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -16,6 +18,9 @@ class HomeController extends Controller /** Show the application dashboard. */ public function index(Request $request) { + + dd(Product::first()->nodes()->get() , Product::first()->eggs()->get()); + $usage = 0; foreach (Auth::user()->servers as $server){ diff --git a/app/Models/Egg.php b/app/Models/Egg.php index 68e0e63f..bd35f6c2 100644 --- a/app/Models/Egg.php +++ b/app/Models/Egg.php @@ -6,6 +6,7 @@ use App\Classes\Pterodactyl; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Egg extends Model { @@ -23,14 +24,6 @@ class Egg extends Model 'environment', ]; - /** - * @return BelongsTo - */ - public function nest() - { - return $this->belongsTo(Nest::class, 'id', 'nest_id'); - } - /** * @return array */ @@ -39,7 +32,7 @@ class Egg extends Model $array = []; foreach (json_decode($this->environment) as $variable) { - foreach ($variable as $key => $value){ + foreach ($variable as $key => $value) { $array[$key] = $value; } } @@ -47,12 +40,13 @@ class Egg extends Model return $array; } - public static function syncEggs(){ + public static function syncEggs() + { - Nest::all()->each(function (Nest $nest) { + Nest::all()->each(function (Nest $nest) { $eggs = Pterodactyl::getEggs($nest); - foreach ($eggs as $egg){ + foreach ($eggs as $egg) { $array = []; $environment = []; @@ -64,17 +58,32 @@ class Egg extends Model $array['startup'] = $egg['attributes']['startup']; //get environment variables - foreach ($egg['attributes']['relationships']['variables']['data'] as $variable){ + foreach ($egg['attributes']['relationships']['variables']['data'] as $variable) { $environment[$variable['attributes']['env_variable']] = $variable['attributes']['default_value']; } $array['environment'] = json_encode([$environment]); - self::firstOrCreate(['id' => $array['id']] , $array); + self::firstOrCreate(['id' => $array['id']], $array); } }); - - } + + /** + * @return BelongsTo + */ + public function nest() + { + return $this->belongsTo(Nest::class, 'id', 'nest_id'); + } + + /** + * @return BelongsToMany + */ + public function products() + { + return $this->belongsToMany(Product::class); + } + } diff --git a/app/Models/Node.php b/app/Models/Node.php index 5636c204..3e626802 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -7,6 +7,7 @@ use Exception; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Node extends Model { @@ -48,4 +49,12 @@ class Node extends Model } } + + /** + * @return BelongsToMany + */ + public function products() + { + return $this->belongsToMany(Product::class); + } } diff --git a/app/Models/Product.php b/app/Models/Product.php index 68dc8f74..2b30a560 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Spatie\Activitylog\Traits\LogsActivity; class Product extends Model @@ -25,6 +26,11 @@ class Product extends Model $product->{$product->getKeyName()} = $client->generateId($size = 21); }); + + static::deleting(function(Product $product) { + $product->nodes()->detach(); + $product->eggs()->detach(); + }); } public function getHourlyPrice() @@ -45,8 +51,22 @@ class Product extends Model /** * @return BelongsTo */ - public function servers(): BelongsTo + public function servers() { return $this->belongsTo(Server::class , 'id' , 'product_id'); } + + /** + * @return BelongsToMany + */ + public function eggs() { + return $this->belongsToMany(Egg::class); + } + + /** + * @return BelongsToMany + */ + public function nodes() { + return $this->belongsToMany(Node::class); + } } diff --git a/database/migrations/2021_07_06_152319_create_egg_product_table.php b/database/migrations/2021_07_06_152319_create_egg_product_table.php new file mode 100644 index 00000000..4271f80b --- /dev/null +++ b/database/migrations/2021_07_06_152319_create_egg_product_table.php @@ -0,0 +1,32 @@ +foreignId('egg_id')->constrained(); + $table->foreignUuid('product_id')->constrained(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('egg_product'); + } +} diff --git a/database/migrations/2021_07_06_154314_create_node_product_table.php b/database/migrations/2021_07_06_154314_create_node_product_table.php new file mode 100644 index 00000000..0bb798c6 --- /dev/null +++ b/database/migrations/2021_07_06_154314_create_node_product_table.php @@ -0,0 +1,32 @@ +foreignId('node_id')->constrained(); + $table->foreignUuid('product_id')->constrained(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('node_product'); + } +} diff --git a/database/migrations/2021_07_06_154658_add_disabled_to_eggs_table.php b/database/migrations/2021_07_06_154658_add_disabled_to_eggs_table.php new file mode 100644 index 00000000..353cf99b --- /dev/null +++ b/database/migrations/2021_07_06_154658_add_disabled_to_eggs_table.php @@ -0,0 +1,32 @@ +boolean('disabled')->default(false); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('eggs', function (Blueprint $table) { + $table->dropColumn('disabled'); + }); + } +} diff --git a/resources/views/admin/products/create.blade.php b/resources/views/admin/products/create.blade.php index 2a29bbe8..9b039e1e 100644 --- a/resources/views/admin/products/create.blade.php +++ b/resources/views/admin/products/create.blade.php @@ -24,17 +24,24 @@
+
+ @csrf +
+
+
+
+
Product Details
+
+
-
-
-
-
- - @csrf
- - + +
@@ -105,7 +112,10 @@
- +