From c9108efd63575f6712542cc1f7c96836e4bfa3d7 Mon Sep 17 00:00:00 2001 From: cp6 Date: Wed, 20 Jul 2022 14:35:37 +1000 Subject: [PATCH] Updated SeedBoxes class to use relationships + cache Updated SeedBoxes class to use relationships + cache --- app/Http/Controllers/SeedBoxesController.php | 29 ++-- app/Models/SeedBoxes.php | 49 +++--- resources/views/seedboxes/edit.blade.php | 68 ++++---- resources/views/seedboxes/index.blade.php | 16 +- resources/views/seedboxes/show.blade.php | 80 +++++----- resources/views/servers/index-cards.blade.php | 149 ++++++++++++++++++ 6 files changed, 265 insertions(+), 126 deletions(-) create mode 100644 resources/views/servers/index-cards.blade.php diff --git a/app/Http/Controllers/SeedBoxesController.php b/app/Http/Controllers/SeedBoxesController.php index ed5bfe0..5a4ad2d 100644 --- a/app/Http/Controllers/SeedBoxesController.php +++ b/app/Http/Controllers/SeedBoxesController.php @@ -17,8 +17,7 @@ class SeedBoxesController extends Controller { public function index() { - $seedboxes = SeedBoxes::seedBoxesDataIndexPage(); - + $seedboxes = SeedBoxes::allSeedboxes(); return view('seedboxes.index', compact(['seedboxes'])); } @@ -48,13 +47,10 @@ class SeedBoxesController extends Controller $seedbox_id = Str::random(8); $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - $pricing->insertPricing(6, $seedbox_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); Labels::deleteLabelsAssignedTo($seedbox_id); - Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $seedbox_id); SeedBoxes::create([ @@ -73,6 +69,7 @@ class SeedBoxesController extends Controller 'was_promo' => $request->was_promo ]); + Cache::forget("all_seedboxes"); Home::homePageCacheForget(); return redirect()->route('seedboxes.index') @@ -82,20 +79,14 @@ class SeedBoxesController extends Controller public function show(SeedBoxes $seedbox) { - $seedbox_extras = SeedBoxes::seedBoxDataShowPage($seedbox->id); - - $labels = Labels::labelsForService($seedbox->id); - - return view('seedboxes.show', compact(['seedbox', 'seedbox_extras', 'labels'])); + $seedbox_data = SeedBoxes::seedbox($seedbox->id)[0]; + return view('seedboxes.show', compact(['seedbox_data'])); } public function edit(SeedBoxes $seedbox) { - $seedbox = SeedBoxes::seedBoxEditDataPage($seedbox->id); - - $labels = Labels::labelsForService($seedbox[0]->id); - - return view('seedboxes.edit', compact(['seedbox', 'labels'])); + $seedbox_data = SeedBoxes::seedbox($seedbox->id)[0]; + return view('seedboxes.edit', compact(['seedbox_data'])); } public function update(Request $request, SeedBoxes $seedbox) @@ -134,17 +125,15 @@ class SeedBoxesController extends Controller ]); $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - $pricing->updatePricing($seedbox->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); Labels::deleteLabelsAssignedTo($seedbox->id); - Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $seedbox->id); + Cache::forget("all_seedboxes"); + Cache::forget("seedbox.{$seedbox->id}"); Cache::forget("labels_for_service.{$seedbox->id}"); - Home::homePageCacheForget(); return redirect()->route('seedboxes.index') @@ -162,6 +151,8 @@ class SeedBoxesController extends Controller Labels::deleteLabelsAssignedTo($seedbox_id); + Cache::forget("all_seedboxes"); + Cache::forget("seedbox.{$seedbox->id}"); Home::homePageCacheForget(); return redirect()->route('seedboxes.index') diff --git a/app/Models/SeedBoxes.php b/app/Models/SeedBoxes.php index 4582881..3b82126 100644 --- a/app/Models/SeedBoxes.php +++ b/app/Models/SeedBoxes.php @@ -17,34 +17,39 @@ class SeedBoxes extends Model protected $fillable = ['id', 'active', 'title', 'hostname', 'seed_box_type', 'provider_id', 'location_id', 'bandwidth', 'port_speed', 'disk', 'disk_type', 'disk_as_gb', 'was_promo', 'owned_since']; - public static function seedBoxesDataIndexPage() - { - return DB::table('seedboxes as s') - ->join('providers as p', 's.provider_id', '=', 'p.id') - ->join('locations as l', 's.location_id', '=', 'l.id') - ->join('pricings as pr', 's.id', '=', 'pr.service_id') - ->get(['s.*', 'p.name as provider_name', 'pr.*', 'l.name as location']); + public static function allSeedboxes() + {//All seedboxes and relationships (no using joins) + return Cache::remember("all_seedboxes", now()->addMonth(1), function () { + return SeedBoxes::with(['location', 'provider', 'price', 'labels.label'])->get(); + }); } - public static function seedBoxDataShowPage(string $seed_box_id) - { - return DB::table('seedboxes as s') - ->join('pricings as pr', 's.id', '=', 'pr.service_id') - ->join('providers as p', 's.provider_id', '=', 'p.id') - ->join('locations as l', 's.location_id', '=', 'l.id') - ->where('s.id', '=', $seed_box_id) - ->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*']); + public static function seedbox(string $seedbox_id) + {//Single seedbox and relationships (no using joins) + return Cache::remember("seedbox.$seedbox_id", now()->addMonth(1), function () use ($seedbox_id) { + return SeedBoxes::where('id', $seedbox_id) + ->with(['location', 'provider', 'price', 'labels.label'])->get(); + }); } - public static function seedBoxEditDataPage(string $seed_box_id) + public function location() { - return DB::table('seedboxes as s') - ->join('pricings as pr', 's.id', '=', 'pr.service_id') - ->join('providers as p', 's.provider_id', '=', 'p.id') - ->join('locations as l', 's.location_id', '=', 'l.id') - ->where('s.id', '=', $seed_box_id) - ->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*']); + return $this->hasOne(Locations::class, 'id', 'location_id'); } + public function provider() + { + return $this->hasOne(Providers::class, 'id', 'provider_id'); + } + + public function price() + { + return $this->hasOne(Pricing::class, 'service_id', 'id'); + } + + public function labels() + { + return $this->hasMany(LabelsAssigned::class, 'service_id', 'id'); + } } diff --git a/resources/views/seedboxes/edit.blade.php b/resources/views/seedboxes/edit.blade.php index e846120..737c7bd 100644 --- a/resources/views/seedboxes/edit.blade.php +++ b/resources/views/seedboxes/edit.blade.php @@ -1,7 +1,7 @@ -@section('title') {{$seedbox[0]->title}} {{'edit'}} @endsection +@section('title') {{$seedbox_data->title}} {{'edit'}} @endsection - Edit {{ $seedbox[0]->title }} + Edit {{ $seedbox_data->title }}
@@ -11,7 +11,7 @@ Back to seed boxes -
+ @csrf @method('PUT')
@@ -20,7 +20,7 @@
Title
+ name="title" value="{{$seedbox_data->title}}"> @error('title') {{ $message }} @enderror
@@ -30,44 +30,44 @@
Hostname
+ name="hostname" value="{{$seedbox_data->hostname}}">
- +
Type
@@ -77,14 +77,14 @@
- {{$seedbox[0]->provider_id}} + {{$seedbox_data->provider->id}}
Price price - {{$seedbox[0]->price}} + {{$seedbox_data->price->price}} 9999 0.01 @@ -92,40 +92,40 @@
- {{$seedbox[0]->term}} + {{$seedbox_data->price->term}}
- {{$seedbox[0]->currency}} + {{$seedbox_data->price->currency}}
- {{$seedbox[0]->location_id}} + {{$seedbox_data->location_id}}
Promo price was_promo - {{ $seedbox[0]->was_promo }} + {{ $seedbox_data->was_promo }}
Owned since owned_since - {{$seedbox[0]->owned_since }} + {{$seedbox_data->owned_since }}
Next due date next_due_date - {{$seedbox[0]->next_due_date }} + {{$seedbox_data->price->next_due_date }}
@@ -137,7 +137,7 @@ 1 999999 1 - {{$seedbox[0]->disk_as_gb}} + {{$seedbox_data->disk_as_gb}}
@@ -147,7 +147,7 @@ 1 999999 1 - {{$seedbox[0]->bandwidth}} + {{$seedbox_data->bandwidth}}
@@ -157,7 +157,7 @@ 1 999999 1 - {{$seedbox[0]->port_speed}} + {{$seedbox_data->port_speed}}
@@ -166,8 +166,8 @@ label label1 - @if(isset($labels[0]->id)) - {{$labels[0]->id}} + @if(isset($seedbox_data->labels[0]->label->id)) + {{$seedbox_data->labels[0]->label->id}} @endif @@ -175,8 +175,8 @@ label label2 - @if(isset($labels[1]->id)) - {{$labels[1]->id}} + @if(isset($seedbox_data->labels[1]->label->id)) + {{$seedbox_data->labels[1]->label->id}} @endif @@ -184,8 +184,8 @@ label label3 - @if(isset($labels[2]->id)) - {{$labels[2]->id}} + @if(isset($seedbox_data->labels[2]->label->id)) + {{$seedbox_data->labels[2]->label->id}} @endif @@ -193,15 +193,15 @@ label label4 - @if(isset($labels[3]->id)) - {{$labels[3]->id}} + @if(isset($seedbox_data->labels[3]->label->id)) + {{$seedbox_data->labels[3]->label->id}} @endif
active === 1) ? 'checked' : '' }}> + value="1" {{ ($seedbox_data->active === 1) ? 'checked' : '' }}> diff --git a/resources/views/seedboxes/index.blade.php b/resources/views/seedboxes/index.blade.php index 4e07d1b..aa1afd5 100644 --- a/resources/views/seedboxes/index.blade.php +++ b/resources/views/seedboxes/index.blade.php @@ -43,8 +43,8 @@ {{ $row->title }} {{ $row->seed_box_type }} - {{ $row->location }} - {{ $row->provider_name }} + {{ $row->location->name }} + {{ $row->provider->name }} @if($row->disk_as_gb >= 1000) {{ number_format($row->disk_as_gb / 1000,1) }} TB @@ -66,22 +66,22 @@ {{ $row->port_speed }} Mbps @endif - {{ $row->price }} {{$row->currency}} {{\App\Process::paymentTermIntToString($row->term)}} - {{Carbon\Carbon::parse($row->next_due_date)->diffForHumans()}} + {{ $row->price->price }} {{$row->price->currency}} {{\App\Process::paymentTermIntToString($row->price->term)}} + {{Carbon\Carbon::parse($row->price->next_due_date)->diffForHumans()}} {{ $row->owned_since }} - + @csrf - - + id="btn-{{$row->title}}" title="{{$row->id}}"> diff --git a/resources/views/seedboxes/show.blade.php b/resources/views/seedboxes/show.blade.php index b52f480..579a95d 100644 --- a/resources/views/seedboxes/show.blade.php +++ b/resources/views/seedboxes/show.blade.php @@ -1,4 +1,4 @@ -@section('title') {{$seedbox->title}} {{'Seed box'}} @endsection +@section('title') {{$seedbox_data->title}} {{'Seed box'}} @endsection {{ __('Seed box details') }} @@ -7,18 +7,14 @@
-

{{ $seedbox->title }}

- @foreach($labels as $label) - @if($loop->last) - {{$label->label}} - @else - {{$label->label}}, - @endif - @endforeach +

{{ $seedbox_data->title }}

+ @foreach($seedbox_data->labels as $label) + {{$label->label->label}} + @endforeach
-
{{ $seedbox->id }}
- @if($seedbox->active !== 1) +
{{ $seedbox_data->id }}
+ @if($seedbox_data->active !== 1)
not active
@endif
@@ -30,57 +26,57 @@ Type - {{ $seedbox->seed_box_type }} + {{ $seedbox_data->seed_box_type }} Hostname - {{ $seedbox_extras[0]->hostname }} + {{ $seedbox_data->hostname }} Location - {{ $seedbox_extras[0]->location }} + {{ $seedbox_data->location->name }} Provider - {{ $seedbox_extras[0]->provider_name }} + {{ $seedbox_data->provider->name }} Price - {{ $seedbox_extras[0]->price }} {{ $seedbox_extras[0]->currency }} - {{\App\Process::paymentTermIntToString($seedbox_extras[0]->term)}} + {{ $seedbox_data->price->price }} {{ $seedbox_data->price->currency }} + {{\App\Process::paymentTermIntToString($seedbox_data->price->term)}} Was promo - {{ ($seedbox_extras[0]->was_promo === 1) ? 'Yes' : 'No' }} + {{ ($seedbox_data->was_promo === 1) ? 'Yes' : 'No' }} Owned since - @if(!is_null($seedbox->owned_since)) - {{ date_format(new DateTime($seedbox->owned_since), 'jS F Y') }} + @if(!is_null($seedbox_data->owned_since)) + {{ date_format(new DateTime($seedbox_data->owned_since), 'jS F Y') }} @endif Next due date - {{Carbon\Carbon::parse($seedbox_extras[0]->next_due_date)->diffForHumans()}} - ({{Carbon\Carbon::parse($seedbox_extras[0]->next_due_date)->format('d/m/Y')}}) + {{Carbon\Carbon::parse($seedbox_data->price->next_due_date)->diffForHumans()}} + ({{Carbon\Carbon::parse($seedbox_data->price->next_due_date)->format('d/m/Y')}}) Inserted - @if(!is_null($seedbox->created_at)) - {{ date_format(new DateTime($seedbox->created_at), 'jS M y g:i a') }} + @if(!is_null($seedbox_data->created_at)) + {{ date_format(new DateTime($seedbox_data->created_at), 'jS M y g:i a') }} @endif Updated - @if(!is_null($seedbox->updated_at)) - {{ date_format(new DateTime($seedbox->updated_at), 'jS M y g:i a') }} + @if(!is_null($seedbox_data->updated_at)) + {{ date_format(new DateTime($seedbox_data->updated_at), 'jS M y g:i a') }} @endif @@ -94,30 +90,30 @@ Disk - @if($seedbox->disk_as_gb >= 1000) - {{ number_format($seedbox->disk_as_gb / 1000,1) }} TB + @if($seedbox_data->disk_as_gb >= 1000) + {{ number_format($seedbox_data->disk_as_gb / 1000,1) }} TB @else - {{ $seedbox->disk_as_gb }} GB + {{ $seedbox_data->disk_as_gb }} GB @endif Bandwidth - @if($seedbox->bandwidth >= 1000) - {{ number_format($seedbox->bandwidth / 1000,1) }} TB + @if($seedbox_data->bandwidth >= 1000) + {{ number_format($seedbox_data->bandwidth / 1000,1) }} TB @else - {{ $seedbox->bandwidth }} GB + {{ $seedbox_data->bandwidth }} GB @endif Port speed - @if($seedbox->port_speed >= 1000) - {{ number_format($seedbox->port_speed / 1000,1) }} Gbps + @if($seedbox_data->port_speed >= 1000) + {{ number_format($seedbox_data->port_speed / 1000,1) }} Gbps @else - {{ $seedbox->port_speed }} Mbps + {{ $seedbox_data->port_speed }} Mbps @endif @@ -126,14 +122,12 @@
- - Go back - - - Edit - + + {{ route('seedboxes.index') }} + + + {{ route('seedboxes.edit', $seedbox_data->id) }} + @if(Session::has('timer_version_footer') && Session::get('timer_version_footer') === 1)

diff --git a/resources/views/servers/index-cards.blade.php b/resources/views/servers/index-cards.blade.php new file mode 100644 index 0000000..7f5af80 --- /dev/null +++ b/resources/views/servers/index-cards.blade.php @@ -0,0 +1,149 @@ +@section('title') + {{'Servers'}} +@endsection +@section('style') + +@endsection +@section('scripts') + + +@endsection + + + {{ __('Servers') }} + +

+ + +
+
+
+ @if(!empty($servers)) + @foreach($servers as $server) +
+
+
+
+
+
{{ $server->hostname }}
+
+
+ + + +
+
+
+
+
{{ $server->location->name }}
+
+
+
{{ $server->price->currency }} {{ $server->price->price }}
+
+
+
+
+

{{ $server->provider->name }}

+
+
+

{{ $server->os->name }}

+
+
+
+
+ {{$server->cpu}} + +
+
+ {{$server->ram}} {{$server->ram_type}} + +
+
+
+
+ {{$server->disk}} {{$server->disk_type}} + +
+
+ @if(isset($server->yabs[0]->cpu_cores)) + YABs + @else + YABs + @endif +
+
+
+
+
+ @endforeach + @endif +
+
+
+ +
+ @if(Session::has('timer_version_footer') && Session::get('timer_version_footer') === 1) +

Built on Laravel + v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }})

+ @endif +
+ + +