From 88636b88c7e58f292c826b94dcb3d925073692ab Mon Sep 17 00:00:00 2001 From: cp6 Date: Wed, 20 Jul 2022 00:19:05 +1000 Subject: [PATCH] Updated Domains model for relationships Updated Domains model for relationships Added caching --- app/Http/Controllers/DomainsController.php | 31 ++++------ app/Models/Domains.php | 46 +++++++++------ resources/views/domains/edit.blade.php | 44 +++++++-------- resources/views/domains/index.blade.php | 14 ++--- resources/views/domains/show.blade.php | 66 +++++++++++----------- 5 files changed, 100 insertions(+), 101 deletions(-) diff --git a/app/Http/Controllers/DomainsController.php b/app/Http/Controllers/DomainsController.php index 1467c63..89b6ef7 100644 --- a/app/Http/Controllers/DomainsController.php +++ b/app/Http/Controllers/DomainsController.php @@ -14,20 +14,16 @@ use Illuminate\Support\Str; class DomainsController extends Controller { - public function index() { - $domains = Domains::domainsDataIndexPage(); - + $domains = Domains::allDomains(); return view('domains.index', compact(['domains'])); } public function show(Domains $domain) - { - $service_extras = Domains::domainsDataShowPage($domain->id); - $labels = Labels::labelsForService($domain->id); - - return view('domains.show', compact(['domain', 'service_extras', 'labels'])); + {//Need to modern + $domain_info = Domains::domain($domain->id)[0]; + return view('domains.show', compact(['domain_info'])); } public function create() @@ -46,11 +42,8 @@ class DomainsController extends Controller ]); $domain_id = Str::random(8); - $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - $pricing->insertPricing(4, $domain_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); Domains::create([ @@ -66,6 +59,7 @@ class DomainsController extends Controller Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $domain_id); + Cache::forget("all_domains"); Home::homePageCacheForget(); return redirect()->route('domains.index') @@ -74,11 +68,8 @@ class DomainsController extends Controller public function edit(Domains $domain) { - $domain_info = Domains::domainsDataEditPage($domain->id); - - $labels = Labels::labelsForService($domain->id); - - return view('domains.edit', compact(['domain', 'domain_info', 'labels'])); + $domain_info = Domains::domain($domain->id)[0]; + return view('domains.edit', compact(['domain_info'])); } public function update(Request $request, Domains $domain) @@ -91,9 +82,7 @@ class DomainsController extends Controller ]); $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - $pricing->updatePricing($domain->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); $domain->update([ @@ -108,9 +97,10 @@ class DomainsController extends Controller ]); Labels::deleteLabelsAssignedTo($domain->id); - Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $domain->id); + Cache::forget("all_domains"); + Cache::forget("domain.{$domain->id}"); Cache::forget("labels_for_service.{$domain->id}"); Home::homePageCacheForget(); @@ -121,7 +111,6 @@ class DomainsController extends Controller public function destroy(Domains $domain) { $items = Domains::find($domain->id); - $items->delete(); $p = new Pricing(); @@ -129,6 +118,8 @@ class DomainsController extends Controller Labels::deleteLabelsAssignedTo($domain->id); + Cache::forget("all_domains"); + Cache::forget("domain.{$domain->id}"); Home::homePageCacheForget(); return redirect()->route('domains.index') diff --git a/app/Models/Domains.php b/app/Models/Domains.php index 4e17810..72001eb 100644 --- a/app/Models/Domains.php +++ b/app/Models/Domains.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class Domains extends Model @@ -12,30 +13,39 @@ class Domains extends Model public $incrementing = false; + protected $table = 'domains'; + protected $fillable = ['id', 'domain', 'extension', 'ns1', 'ns2', 'ns3', 'price', 'currency', 'payment_term', 'owned_since', 'provider_id', 'next_due_date']; - public static function domainsDataIndexPage() - { - return DB::table('domains as d') - ->join('providers as p', 'd.provider_id', '=', 'p.id') - ->join('pricings as pr', 'd.id', '=', 'pr.service_id') - ->get(['d.*', 'p.name as provider_name', 'pr.*']); + + public static function allDomains() + {//All domains and relationships (no using joins) + return Cache::remember("all_domains", now()->addMonth(1), function () { + return Domains::with(['provider', 'price', 'labels', 'labels.label'])->get(); + }); } - public static function domainsDataShowPage(string $domain_id) - { - return DB::table('domains as d') - ->join('providers as p', 'd.provider_id', '=', 'p.id') - ->join('pricings as pr', 'd.id', '=', 'pr.service_id') - ->where('d.id', '=', $domain_id) - ->get(['d.*', 'p.name as provider_name', 'pr.*']); + public static function domain(string $domain_id) + {//Single domains and relationships (no using joins) + return Cache::remember("domain.$domain_id", now()->addMonth(1), function () use ($domain_id) { + return Domains::where('id', $domain_id) + ->with(['provider', 'price', 'labels', 'labels.label'])->get(); + }); } - public static function domainsDataEditPage(string $domain_id) + public function provider() { - return DB::table('domains as d') - ->join('pricings as pr', 'd.id', '=', 'pr.service_id') - ->where('d.id', '=', $domain_id) - ->get(['d.*', 'pr.*']); + 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/domains/edit.blade.php b/resources/views/domains/edit.blade.php index f9ae1c4..d3d5681 100644 --- a/resources/views/domains/edit.blade.php +++ b/resources/views/domains/edit.blade.php @@ -1,7 +1,7 @@ @section('title') {{'Edit domain'}} @endsection - Edit {{ $domain->domain }}.{{ $domain->extension }} + Edit {{ $domain_info->domain }}.{{ $domain_info->extension }}
@@ -11,7 +11,7 @@ Go back -
+ @csrf @method('PUT')
@@ -22,7 +22,7 @@ + value="{{ $domain_info->domain }}"> @error('name') {{ $message }} @enderror
@@ -31,7 +31,7 @@ Extension extension - {{ $domain->extension }} + {{ $domain_info->extension }}
@@ -41,7 +41,7 @@ NS1 ns1 255 - {{$domain->ns1}} + {{$domain_info->ns1}}
@@ -49,7 +49,7 @@ NS2 ns2 255 - {{$domain->ns2}} + {{$domain_info->ns2}}
@@ -57,7 +57,7 @@ NS3 ns3 255 - {{$domain->ns3}} + {{$domain_info->ns3}}
@@ -65,7 +65,7 @@
- {{$domain->provider_id}} + {{$domain_info->provider->id}}
@@ -74,17 +74,17 @@ Price price 0.01 - {{ $domain_info[0]->price }} + {{ $domain_info->price->price }}
- {{$domain_info[0]->term}} + {{$domain_info->price->term}}
- {{$domain_info[0]->currency}} + {{$domain_info->price->currency}}
@@ -93,14 +93,14 @@ Owned since owned_since - {{$domain_info[0]->owned_since }} + {{$domain_info->owned_since }}
Next due date next_due_date - {{$domain_info[0]->next_due_date}} + {{$domain_info->price->next_due_date}}
@@ -110,8 +110,8 @@ label label1 - @if(isset($labels[0]->id)) - {{$labels[0]->id}} + @if(isset($domain_info->labels[0]->label)) + {{$domain_info->labels[0]->label->id}} @endif @@ -119,8 +119,8 @@ label label2 - @if(isset($labels[1]->id)) - {{$labels[1]->id}} + @if(isset($domain_info->labels[1]->label)) + {{$domain_info->labels[1]->label->id}} @endif @@ -128,8 +128,8 @@ label label3 - @if(isset($labels[2]->id)) - {{$labels[2]->id}} + @if(isset($domain_info->labels[2]->label)) + {{$domain_info->labels[2]->label->id}} @endif @@ -137,8 +137,8 @@ label label4 - @if(isset($labels[3]->id)) - {{$labels[3]->id}} + @if(isset($domain_info->labels[3]->label)) + {{$domain_info->labels[3]->label->id}} @endif @@ -146,7 +146,7 @@
active === 1) ? 'checked' : '' }}> + value="1" {{ ($domain_info->active === 1) ? 'checked' : '' }}> diff --git a/resources/views/domains/index.blade.php b/resources/views/domains/index.blade.php index a252d68..95d5bd5 100644 --- a/resources/views/domains/index.blade.php +++ b/resources/views/domains/index.blade.php @@ -34,22 +34,22 @@ {{ $domain->domain }}.{{$domain->extension}} {{ $domain->owned_since}} - {{ now()->diffInDays($domain->next_due_date) }} days + {{ now()->diffInDays($domain->price->next_due_date) }} days - {{ $domain->provider_name}} - {{ $domain->price }} {{$domain->currency}} + {{ $domain->provider->name}} + {{ $domain->price->price }} {{$domain->price->currency}} - - + - @csrf @method('DELETE') + id="btn-{{$domain->domain}}" title="{{$domain->id}}"> diff --git a/resources/views/domains/show.blade.php b/resources/views/domains/show.blade.php index 4e9faa2..0a72b69 100644 --- a/resources/views/domains/show.blade.php +++ b/resources/views/domains/show.blade.php @@ -1,4 +1,6 @@ -@section('title') {{ $domain->domain }}.{{$domain->extension}} {{'domain'}} @endsection +@section('title') + {{ $domain_info->domain }}.{{$domain_info->extension}} {{'domain'}} +@endsection {{ __('Domain details') }} @@ -7,18 +9,14 @@
-

{{ $domain->domain }}.{{$domain->extension}}

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

{{ $domain_info->domain }}.{{$domain_info->extension}}

+ @foreach($domain_info->labels as $label) + {{$label->label->label}} + @endforeach
-
{{ $domain->id }}
- @if($domain->active !== 1) +
{{ $domain_info->id }}
+ @if($domain_info->active !== 1)
not active
@endif
@@ -30,57 +28,59 @@ Domain - {{ $domain->domain }}.{{$domain->extension}} + {{ $domain_info->domain }} + .{{$domain_info->extension}} Provider - {{ $service_extras[0]->provider_name }} + {{ $domain_info->provider->name }} Price - {{ $service_extras[0]->price }} {{ $service_extras[0]->currency }} - {{\App\Process::paymentTermIntToString($service_extras[0]->term)}} + {{ $domain_info->price->price }} {{ $domain_info->price->currency }} + {{\App\Process::paymentTermIntToString($domain_info->price->term)}} NS1 - {{ $domain->ns1 }} + {{ $domain_info->ns1 }} NS2 - {{ $domain->ns2 }} + {{ $domain_info->ns2 }} NS3 - {{ $domain->ns3 }} + {{ $domain_info->ns3 }} Owned since - @if(!is_null($domain->owned_since)) - {{ date_format(new DateTime($domain->owned_since), 'jS F Y') }} + @if(!is_null($domain_info->owned_since)) + {{ date_format(new DateTime($domain_info->owned_since), 'jS F Y') }} @endif Next due date - {{Carbon\Carbon::parse($service_extras[0]->next_due_date)->diffForHumans()}} - ({{Carbon\Carbon::parse($service_extras[0]->next_due_date)->format('d/m/Y')}}) + {{Carbon\Carbon::parse($domain_info->price->next_due_date)->diffForHumans()}} + ({{Carbon\Carbon::parse($domain_info->price->next_due_date)->format('d/m/Y')}}) Inserted - @if(!is_null($domain->created_at)) - {{ date_format(new DateTime($domain->created_at), 'jS M y g:i a') }} + @if(!is_null($domain_info->created_at)) + {{ date_format(new DateTime($domain_info->created_at), 'jS M y g:i a') }} @endif Updated - @if(!is_null($domain->updated_at)) - {{ date_format(new DateTime($domain->updated_at), 'jS M y g:i a') }} + @if(!is_null($domain_info->updated_at)) + {{ date_format(new DateTime($domain_info->updated_at), 'jS M y g:i a') }} @endif @@ -89,14 +89,12 @@
- - Go back - - - Edit - + + {{ route('domains.index') }} + + + {{ route('domains.edit', $domain_info->id) }} +