added node location in node select

This commit is contained in:
AVMG20 2021-11-07 16:29:18 +01:00
parent bf5c36b232
commit 88f2c22804
4 changed files with 83 additions and 33 deletions

View file

@ -3,14 +3,12 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Egg; use App\Models\Egg;
use App\Models\Location;
use App\Models\Node; use App\Models\Node;
use App\Models\Product; use App\Models\Product;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class ProductController extends Controller class ProductController extends Controller
@ -44,6 +42,38 @@ class ProductController extends Controller
return $nodes; return $nodes;
} }
/**
* @description get product locations based on selected egg
* @param Request $request
* @param Egg $egg
* @return Collection|JsonResponse
*/
public function getLocationsBasedOnEgg(Request $request, Egg $egg)
{
$nodes = $this->getNodesBasedOnEgg($request, $egg);
$locations = collect();
//locations
$nodes->each(function (Node $node) use ($nodes, $locations) {
/** @var Location $location */
$location = $node->location;
if (!$locations->contains('id' , $location->id)){
$nodeIds = $nodes->map(function ($node){
return $node->id;
});
$location->nodes = $location->nodes()
->whereIn('id' , $nodeIds)
->get();
$locations->add($location);
}
});
return $locations;
}
/** /**
* @param Node $node * @param Node $node
* @return Collection|JsonResponse * @return Collection|JsonResponse
@ -53,7 +83,7 @@ class ProductController extends Controller
if (is_null($node->id)) return response()->json('node id is required', '400'); if (is_null($node->id)) return response()->json('node id is required', '400');
return Product::query()->whereHas('nodes', function (Builder $builder) use ($node) { return Product::query()->whereHas('nodes', function (Builder $builder) use ($node) {
$builder->where('id' , '=' , $node->id); $builder->where('id', '=', $node->id);
})->get(); })->get();
} }
} }

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Classes\Pterodactyl; use App\Classes\Pterodactyl;
use App\Models\Configuration; use App\Models\Configuration;
use App\Models\Egg; use App\Models\Egg;
use App\Models\Location;
use App\Models\Nest; use App\Models\Nest;
use App\Models\Node; use App\Models\Node;
use App\Models\Product; use App\Models\Product;
@ -39,16 +40,19 @@ class ServerController extends Controller
$eggs = Egg::query()->has('products')->get(); $eggs = Egg::query()->has('products')->get();
$locations = Location::all();
$nests = Nest::query()->whereHas('eggs', function (Builder $builder) { $nests = Nest::query()->whereHas('eggs', function (Builder $builder) {
$builder->has('products'); $builder->has('products');
})->get(); })->get();
return view('servers.create')->with([ return view('servers.create')->with([
'productCount' => $productCount, 'productCount' => $productCount,
'nodeCount' => $nodeCount, 'nodeCount' => $nodeCount,
'nests' => $nests, 'nests' => $nests,
'eggs' => $eggs, 'locations' => $locations,
'user' => Auth::user(), 'eggs' => $eggs,
'user' => Auth::user(),
]); ]);
} }

View file

@ -93,7 +93,7 @@
<div class="row"> <div class="row">
<div class="col-md-6"> <div class="col-md-6">
<div class="form-group"> <div class="form-group">
<label for="nest">{{__('Software')}}</label> <label for="nest">{{__('Software / Games')}}</label>
<select class="custom-select" <select class="custom-select"
required required
name="nest" name="nest"
@ -112,14 +112,14 @@
<div class="col-md-6"> <div class="col-md-6">
<div class="form-group"> <div class="form-group">
<label for="egg">{{__('Configuration')}}</label> <label for="egg">{{__('Specification ')}}</label>
<div> <div>
<select id="egg" <select id="egg"
required required
name="egg" name="egg"
:disabled="eggs.length == 0" :disabled="eggs.length == 0"
x-model="selectedEgg" x-model="selectedEgg"
@change="fetchNodes();" @change="fetchLocations();"
required="required" required="required"
class="custom-select"> class="custom-select">
<option x-text="getEggInputText()" <option x-text="getEggInputText()"
@ -139,15 +139,26 @@
required required
id="node" id="node"
x-model="selectedNode" x-model="selectedNode"
:disabled="!fetchedNodes" :disabled="!fetchedLocations"
@change="fetchProducts();" @change="fetchProducts();"
class="custom-select"> class="custom-select">
<option <option
x-text="getNodeInputText()" x-text="getNodeInputText()"
disabled selected hidden value="null"></option> disabled selected hidden value="null">
<template x-for="node in nodes" :key="node.id"> </option>
<option x-text="node.name" :value="node.id"></option>
<template x-for="location in locations" :key="location.id">
<optgroup :label="location.name">
<template x-for="node in location.nodes" :key="node.id">
<option x-text="node.name"
:value="node.id">
</option>
</template>
</optgroup>
</template> </template>
</select> </select>
</div> </div>
@ -190,14 +201,14 @@
<li x-show="selectedNestObject.name" <li x-show="selectedNestObject.name"
class="list-group-item d-flex justify-content-between lh-condensed"> class="list-group-item d-flex justify-content-between lh-condensed">
<div> <div>
<h6 class="my-0">{{__('Software')}}</h6> <h6 class="my-0">{{__('Software / Games')}}</h6>
<small x-text="selectedNestObject?.name ?? '{{__('No selection')}}'" <small x-text="selectedNestObject?.name ?? '{{__('No selection')}}'"
class="text-muted"></small> class="text-muted"></small>
</div> </div>
</li> </li>
<li class="list-group-item d-flex justify-content-between lh-condensed"> <li class="list-group-item d-flex justify-content-between lh-condensed">
<div> <div>
<h6 class="my-0">{{__('Configuration')}}</h6> <h6 class="my-0">{{__('Specification')}}</h6>
<small x-text="selectedEggObject?.name ?? '{{__('No selection')}}'" <small x-text="selectedEggObject?.name ?? '{{__('No selection')}}'"
class="text-muted"></small> class="text-muted"></small>
</div> </div>
@ -248,7 +259,7 @@
return { return {
//loading //loading
loading: false, loading: false,
fetchedNodes: false, fetchedLocations: false,
fetchedProducts: false, fetchedProducts: false,
//input fields //input fields
@ -269,7 +280,7 @@
nests: {!! $nests !!}, nests: {!! $nests !!},
eggsSave:{!! $eggs !!}, //store back-end eggs eggsSave:{!! $eggs !!}, //store back-end eggs
eggs: [], eggs: [],
nodes: [], locations: [],
products: [], products: [],
@ -279,9 +290,9 @@
* @see selectedNest * @see selectedNest
*/ */
setNests() { setNests() {
this.fetchedNodes = false; this.fetchedLocations = false;
this.fetchedProducts = false; this.fetchedProducts = false;
this.nodes = []; this.locations = [];
this.products = []; this.products = [];
this.selectedEgg = 'null'; this.selectedEgg = 'null';
this.selectedNode = 'null'; this.selectedNode = 'null';
@ -296,20 +307,20 @@
* @note called whenever a server configuration is selected * @note called whenever a server configuration is selected
* @see selectedEg * @see selectedEg
*/ */
async fetchNodes() { async fetchLocations() {
this.loading = true; this.loading = true;
this.fetchedNodes = false; this.fetchedLocations = false;
this.fetchedProducts = false; this.fetchedProducts = false;
this.nodes = []; this.locations = [];
this.products = []; this.products = [];
this.selectedNode = 'null'; this.selectedNode = 'null';
this.selectedProduct = 'null'; this.selectedProduct = 'null';
let response = await axios.get(`{{route('products.nodes.egg')}}/${this.selectedEgg}`) let response = await axios.get(`{{route('products.locations.egg')}}/${this.selectedEgg}`)
.catch(console.error) .catch(console.error)
this.fetchedNodes = true; this.fetchedLocations = true;
this.nodes = response.data this.locations = response.data
this.loading = false; this.loading = false;
this.updateSelectedObjects() this.updateSelectedObjects()
}, },
@ -341,7 +352,11 @@
updateSelectedObjects() { updateSelectedObjects() {
this.selectedNestObject = this.nests.find(nest => nest.id == this.selectedNest) ?? {} this.selectedNestObject = this.nests.find(nest => nest.id == this.selectedNest) ?? {}
this.selectedEggObject = this.eggs.find(egg => egg.id == this.selectedEgg) ?? {} this.selectedEggObject = this.eggs.find(egg => egg.id == this.selectedEgg) ?? {}
this.selectedNodeObject = this.nodes.find(node => node.id == this.selectedNode) ?? {}
this.locations.forEach(location => {
this.selectedNodeObject = location.nodes.find(node => node.id == this.selectedNode) ?? {};
})
this.selectedProductObject = this.products.find(product => product.id == this.selectedProduct) ?? {} this.selectedProductObject = this.products.find(product => product.id == this.selectedProduct) ?? {}
}, },
@ -358,8 +373,8 @@
}, },
getNodeInputText() { getNodeInputText() {
if (this.fetchedNodes) { if (this.fetchedLocations) {
if (this.nodes.length > 0) { if (this.locations.length > 0) {
return '{{__('Please select a node...')}}'; return '{{__('Please select a node...')}}';
} }
return '{{__('No nodes found matching current configuration')}}' return '{{__('No nodes found matching current configuration')}}'
@ -384,10 +399,10 @@
return '{{__('---')}}'; return '{{__('---')}}';
}, },
getProductOptionText(product){ getProductOptionText(product) {
let text = product.name + ' (' + product.description + ')'; let text = product.name + ' (' + product.description + ')';
if (product.minimum_credits > this.user.credits){ if (product.minimum_credits > this.user.credits) {
return '{{__('Not enough credits!')}} | ' + text; return '{{__('Not enough credits!')}} | ' + text;
} }

View file

@ -57,6 +57,7 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
#server create utility routes (product) #server create utility routes (product)
#routes made for server create page to fetch product info #routes made for server create page to fetch product info
Route::get('/products/nodes/egg/{egg?}', [FrontProductController::class, 'getNodesBasedOnEgg'])->name('products.nodes.egg'); Route::get('/products/nodes/egg/{egg?}', [FrontProductController::class, 'getNodesBasedOnEgg'])->name('products.nodes.egg');
Route::get('/products/locations/egg/{egg?}', [FrontProductController::class, 'getLocationsBasedOnEgg'])->name('products.locations.egg');
Route::get('/products/products/node/{node?}', [FrontProductController::class, 'getProductsBasedOnNode'])->name('products.products.node'); Route::get('/products/products/node/{node?}', [FrontProductController::class, 'getProductsBasedOnNode'])->name('products.products.node');
#payments #payments