From eb31701076be35d44a56a128be2d56775409ba9c Mon Sep 17 00:00:00 2001 From: cp6 Date: Tue, 19 Jul 2022 15:55:10 +1000 Subject: [PATCH] Updated Reseller model to use relationships Updated Reseller model to use relationships Added caching for Reseller --- app/Http/Controllers/ResellerController.php | 45 ++++------- app/Models/Reseller.php | 50 +++++++----- resources/views/reseller/edit.blade.php | 86 ++++++++++----------- resources/views/reseller/index.blade.php | 16 ++-- resources/views/reseller/show.blade.php | 30 ++++--- resources/views/shared/edit.blade.php | 82 ++++++++++---------- 6 files changed, 151 insertions(+), 158 deletions(-) diff --git a/app/Http/Controllers/ResellerController.php b/app/Http/Controllers/ResellerController.php index 075fc72..399a3c6 100644 --- a/app/Http/Controllers/ResellerController.php +++ b/app/Http/Controllers/ResellerController.php @@ -18,16 +18,13 @@ class ResellerController extends Controller { public function index() { - $resellers = Reseller::resellerDataIndexPage(); - + $resellers = Reseller::allResellerHosting(); return view('reseller.index', compact(['resellers'])); } public function create() { - $Providers = Providers::allProviders(); - $Locations = Locations::allLocations(); - return view('reseller.create', compact(['Providers', 'Locations'])); + return view('reseller.create'); } public function store(Request $request) @@ -60,9 +57,7 @@ class ResellerController extends Controller $reseller_id = Str::random(8); $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - $pricing->insertPricing(3, $reseller_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); if (!is_null($request->dedicated_ip)) { @@ -91,32 +86,23 @@ class ResellerController extends Controller 'db_limit' => $request->db ]); + Cache::forget("all_reseller"); Home::homePageCacheForget(); return redirect()->route('reseller.index') ->with('success', 'Reseller hosting created Successfully.'); } - public function show(Reseller $reseller) { - $reseller_extras = Reseller::resellerDataShowPage($reseller->id); - - $labels = Labels::labelsForService($reseller->id); - - $ip_address = IPs::ipsForServer($reseller->id); - return view('reseller.show', compact(['reseller', 'reseller_extras', 'labels', 'ip_address'])); + $reseller = Reseller::resellerHosting($reseller->id)[0]; + return view('reseller.show', compact(['reseller'])); } public function edit(Reseller $reseller) { - $labels = Labels::labelsForService($reseller->id); - - $ip_address = IPs::ipsForServer($reseller->id); - - $reseller = Reseller::resellerDataEditPage($reseller->id); - - return view('reseller.edit', compact(['reseller', 'ip_address', 'labels'])); + $reseller = Reseller::resellerHosting($reseller->id)[0]; + return view('reseller.edit', compact(['reseller'])); } public function update(Request $request, Reseller $reseller) @@ -164,13 +150,10 @@ class ResellerController extends Controller ]); $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - $pricing->updatePricing($request->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); Labels::deleteLabelsAssignedTo($request->id); - Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $request->id); IPs::deleteIPsAssignedTo($request->id); @@ -179,6 +162,7 @@ class ResellerController extends Controller IPs::insertIP($request->id, $request->dedicated_ip); } + Cache::forget("reseller_hosting.{$request->id}"); Cache::forget("labels_for_service.{$request->id}"); Home::homePageCacheForget(); @@ -189,18 +173,19 @@ class ResellerController extends Controller public function destroy(Reseller $reseller) { - $id = $reseller->id; - $items = Reseller::find($id); - + $reseller_id = $reseller->id; + $items = Reseller::find($reseller_id); $items->delete(); $p = new Pricing(); - $p->deletePricing($id); + $p->deletePricing($reseller_id); - Labels::deleteLabelsAssignedTo($id); + Labels::deleteLabelsAssignedTo($reseller_id); - IPs::deleteIPsAssignedTo($id); + IPs::deleteIPsAssignedTo($reseller_id); + Cache::forget("all_reseller"); + Cache::forget("reseller_hosting.$reseller_id"); Home::homePageCacheForget(); return redirect()->route('reseller.index') diff --git a/app/Models/Reseller.php b/app/Models/Reseller.php index b3dfd31..df43c73 100644 --- a/app/Models/Reseller.php +++ b/app/Models/Reseller.php @@ -17,32 +17,44 @@ class Reseller extends Model public $incrementing = false; - public static function resellerDataIndexPage() - { - return DB::table('reseller_hosting 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 allResellerHosting() + {//All reseller hosting and relationships (no using joins) + return Cache::remember("all_reseller", now()->addMonth(1), function () { + return Reseller::with(['location', 'provider', 'price', 'ips', 'labels', 'labels.label'])->get(); + }); } - public static function resellerDataShowPage(string $reseller_id) + public static function resellerHosting(string $shared_id) + {//Single reseller hosting and relationships (no using joins) + return Cache::remember("reseller_hosting.$shared_id", now()->addMonth(1), function () use ($shared_id) { + return Reseller::where('id', $shared_id) + ->with(['location', 'provider', 'price', 'ips', 'labels', 'labels.label'])->get(); + }); + } + + public function ips() { - return DB::table('reseller_hosting 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', '=', $reseller_id) - ->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*']); + return $this->hasMany(IPs::class, 'service_id', 'id'); } - public static function resellerDataEditPage(string $reseller_id) + public function location() { - return DB::table('reseller_hosting as s') - ->join('pricings as p', 's.id', '=', 'p.service_id') - ->where('s.id', '=', $reseller_id) - ->get(['s.*', 'p.*']); + 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/reseller/edit.blade.php b/resources/views/reseller/edit.blade.php index a7aaea2..8cee131 100644 --- a/resources/views/reseller/edit.blade.php +++ b/resources/views/reseller/edit.blade.php @@ -1,7 +1,7 @@ -@section('title') {{$reseller[0]->main_domain}} {{'edit'}} @endsection +@section('title') {{$reseller->main_domain}} {{'edit'}} @endsection - Edit {{ $reseller[0]->main_domain }} + Edit {{ $reseller->main_domain }}
@@ -12,7 +12,7 @@ Back to reseller hosting -
+ @csrf @method('PUT')
@@ -21,58 +21,58 @@
Domain
+ name="domain" value="{{$reseller->main_domain}}"> @error('name') {{ $message }} @enderror
- +
Type
@@ -84,21 +84,21 @@ dedicated_ip 255 @if(isset($ip_address[0]['address'])){{$ip_address[0]['address']}}@endif + name="value">@if(isset($reseller->ips[0]['address'])){{$reseller->ips[0]['address']}}@endif
- {{$reseller[0]->provider_id}} + {{$reseller->provider->id}}
Price price - {{$reseller[0]->price}} + {{$reseller->price->price}} 9999 0.01 @@ -106,40 +106,40 @@
- {{$reseller[0]->term}} + {{$reseller->price->term}}
- {{$reseller[0]->currency}} + {{$reseller->price->currency}}
- {{$reseller[0]->location_id}} + {{$reseller->location->id}}
Promo price was_promo - {{ $reseller[0]->was_promo }} + {{ $reseller->was_promo }}
Owned since owned_since - {{$reseller[0]->owned_since }} + {{$reseller->owned_since }}
Next due date next_due_date - {{$reseller[0]->next_due_date }} + {{$reseller->price->next_due_date }}
@@ -152,7 +152,7 @@ 1 999999 1 - {{$reseller[0]->accounts}} + {{$reseller->accounts}}
@@ -162,7 +162,7 @@ 1 999999 1 - {{$reseller[0]->domains_limit}} + {{$reseller->domains_limit}}
@@ -172,7 +172,7 @@ 1 999999 1 - {{$reseller[0]->subdomains_limit}} + {{$reseller->subdomains_limit}}
@@ -182,7 +182,7 @@ 1 999999 1 - {{$reseller[0]->disk_as_gb}} + {{$reseller->disk_as_gb}}
@@ -194,7 +194,7 @@ 1 999999 1 - {{$reseller[0]->email_limit}} + {{$reseller->email_limit}}
@@ -204,7 +204,7 @@ 1 999999 1 - {{$reseller[0]->bandwidth}} + {{$reseller->bandwidth}}
@@ -214,7 +214,7 @@ 1 999999 1 - {{$reseller[0]->ftp_limit}} + {{$reseller->ftp_limit}}
@@ -224,17 +224,17 @@ 1 999999 1 - {{$reseller[0]->db_limit}} + {{$reseller->db_limit}}
-
+
label label1 - @if(isset($labels[0]->id)) - {{$labels[0]->id}} + @if(isset($reseller->labels[0]->label->id)) + {{$reseller->labels[0]->label->id}} @endif
@@ -242,8 +242,8 @@ label label2 - @if(isset($labels[1]->id)) - {{$labels[1]->id}} + @if(isset($reseller->labels[1]->label->id)) + {{$reseller->labels[1]->label->id}} @endif
@@ -251,8 +251,8 @@ label label3 - @if(isset($labels[2]->id)) - {{$labels[2]->id}} + @if(isset($reseller->labels[2]->label->id)) + {{$reseller->labels[2]->label->id}} @endif
@@ -260,15 +260,15 @@ label label4 - @if(isset($labels[3]->id)) - {{$labels[3]->id}} + @if(isset($reseller->labels[3]->label->id)) + {{$reseller->labels[3]->label->id}} @endif
active === 1) ? 'checked' : '' }}> + value="1" {{ ($reseller->active === 1) ? 'checked' : '' }}> diff --git a/resources/views/reseller/index.blade.php b/resources/views/reseller/index.blade.php index 0b2b08f..47347a6 100644 --- a/resources/views/reseller/index.blade.php +++ b/resources/views/reseller/index.blade.php @@ -44,20 +44,20 @@ {{ $row->main_domain }} {{ $row->reseller_type }} {{ $row->accounts }} - {{ $row->location }} - {{ $row->provider_name }} + {{ $row->location->name }} + {{ $row->provider->name }} {{ $row->disk_as_gb }} GB - {{ $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 }} - - + - + id="btn-{{$row->main_domain}}" title="{{$row->id}}"> diff --git a/resources/views/reseller/show.blade.php b/resources/views/reseller/show.blade.php index f3093ec..fd23e79 100644 --- a/resources/views/reseller/show.blade.php +++ b/resources/views/reseller/show.blade.php @@ -8,13 +8,9 @@

{{ $reseller->main_domain }}

- @foreach($labels as $label) - @if($loop->last) - {{$label->label}} - @else - {{$label->label}}, - @endif - @endforeach + @foreach($reseller->labels as $label) + {{$label->label->label}} + @endforeach
{{ $reseller->id }}
@@ -30,7 +26,7 @@ Type - {{ $reseller_extras[0]->reseller_type }} + {{ $reseller->reseller_type }} Main domain @@ -39,22 +35,22 @@ Location - {{ $reseller_extras[0]->location }} + {{ $reseller->location->name }} Provider - {{ $reseller_extras[0]->provider_name }} + {{ $reseller->provider->name }} Price - {{ $reseller_extras[0]->price }} {{ $reseller_extras[0]->currency }} - {{\App\Process::paymentTermIntToString($reseller_extras[0]->term)}} + {{ $reseller->price->price }} {{ $reseller->price->currency }} + {{\App\Process::paymentTermIntToString($reseller->price->term)}} Has dedicated IP? - @if(isset($ip_address[0]->address)) + @if(isset($reseller->ips[0]->address)) Yes @else No @@ -63,8 +59,8 @@ IP - @if(isset($ip_address[0]->address)) - {{$ip_address[0]->address}} + @if(isset($reseller->ips[0]->address)) + {{$reseller->ips[0]->address}} @endif @@ -78,8 +74,8 @@ Next due date - {{Carbon\Carbon::parse($reseller_extras[0]->next_due_date)->diffForHumans()}} - ({{Carbon\Carbon::parse($reseller_extras[0]->next_due_date)->format('d/m/Y')}}) + {{Carbon\Carbon::parse($reseller->price->next_due_date)->diffForHumans()}} + ({{Carbon\Carbon::parse($reseller->price->next_due_date)->format('d/m/Y')}}) diff --git a/resources/views/shared/edit.blade.php b/resources/views/shared/edit.blade.php index 10017fd..cba613e 100644 --- a/resources/views/shared/edit.blade.php +++ b/resources/views/shared/edit.blade.php @@ -1,7 +1,7 @@ -@section('title') {{$shared[0]->main_domain}} {{'edit'}} @endsection +@section('title') {{$shared->main_domain}} {{'edit'}} @endsection - Edit {{ $shared[0]->main_domain }} + Edit {{ $shared->main_domain }}
@@ -12,7 +12,7 @@ Back to shared hosting -
+ @csrf @method('PUT')
@@ -21,55 +21,55 @@
Domain
+ name="domain" value="{{$shared->main_domain}}"> @error('name') {{ $message }} @enderror
- +
Type
@@ -80,21 +80,21 @@ Dedicated IP dedicated_ip 255 - @if(isset($shared[0]->ips[0]->address)) {{$shared[0]->ips[0]->address}}@endif + @if(isset($shared->ips[0]->address)) {{$shared->ips[0]->address}}@endif
- {{$shared[0]->provider->id}} + {{$shared->provider->id}}
Price price - {{$shared[0]->price->price}} + {{$shared->price->price}} 9999 0.01 @@ -102,40 +102,40 @@
- {{$shared[0]->price->term}} + {{$shared->price->term}}
- {{$shared[0]->price->currency}} + {{$shared->price->currency}}
- {{$shared[0]->location->id}} + {{$shared->location->id}}
Promo price was_promo - {{ $shared[0]->was_promo }} + {{ $shared->was_promo }}
Owned since owned_since - {{$shared[0]->owned_since }} + {{$shared->owned_since }}
Next due date next_due_date - {{$shared[0]->price->next_due_date }} + {{$shared->price->next_due_date }}
@@ -148,7 +148,7 @@ 1 999999 1 - {{$shared[0]->domains_limit}} + {{$shared->domains_limit}}
@@ -158,7 +158,7 @@ 1 999999 1 - {{$shared[0]->subdomains_limit}} + {{$shared->subdomains_limit}}
@@ -168,7 +168,7 @@ 1 999999 1 - {{$shared[0]->disk_as_gb}} + {{$shared->disk_as_gb}}
@@ -178,7 +178,7 @@ 1 999999 1 - {{$shared[0]->email_limit}} + {{$shared->email_limit}}
@@ -190,7 +190,7 @@ 1 999999 1 - {{$shared[0]->bandwidth}} + {{$shared->bandwidth}}
@@ -200,7 +200,7 @@ 1 999999 1 - {{$shared[0]->ftp_limit}} + {{$shared->ftp_limit}}
@@ -210,7 +210,7 @@ 1 999999 1 - {{$shared[0]->db_limit}} + {{$shared->db_limit}}
@@ -219,8 +219,8 @@ label label1 - @if(isset($shared[0]->labels[0]->label->id)) - {{$shared[0]->labels[0]->label->id}} + @if(isset($shared->labels[0]->label->id)) + {{$shared->labels[0]->label->id}} @endif @@ -228,8 +228,8 @@ label label2 - @if(isset($shared[0]->labels[1]->label->id)) - {{$shared[0]->labels[1]->label->id}} + @if(isset($shared->labels[1]->label->id)) + {{$shared->labels[1]->label->id}} @endif @@ -237,8 +237,8 @@ label label3 - @if(isset($shared[0]->labels[2]->label->id)) - {{$shared[0]->labels[2]->label->id}} + @if(isset($shared->labels[2]->label->id)) + {{$shared->labels[2]->label->id}} @endif @@ -246,15 +246,15 @@ label label4 - @if(isset($shared[0]->labels[3]->label->id)) - {{$shared[0]->labels[3]->label->id}} + @if(isset($shared->labels[3]->label->id)) + {{$shared->labels[3]->label->id}} @endif
active === 1) ? 'checked' : '' }}> + value="1" {{ ($shared->active === 1) ? 'checked' : '' }}>