Updated Domains model for relationships

Updated Domains model for relationships
Added caching
This commit is contained in:
cp6 2022-07-20 00:19:05 +10:00
parent 36194efbd8
commit 88636b88c7
5 changed files with 100 additions and 101 deletions

View File

@ -14,20 +14,16 @@ use Illuminate\Support\Str;
class DomainsController extends Controller class DomainsController extends Controller
{ {
public function index() public function index()
{ {
$domains = Domains::domainsDataIndexPage(); $domains = Domains::allDomains();
return view('domains.index', compact(['domains'])); return view('domains.index', compact(['domains']));
} }
public function show(Domains $domain) public function show(Domains $domain)
{ {//Need to modern
$service_extras = Domains::domainsDataShowPage($domain->id); $domain_info = Domains::domain($domain->id)[0];
$labels = Labels::labelsForService($domain->id); return view('domains.show', compact(['domain_info']));
return view('domains.show', compact(['domain', 'service_extras', 'labels']));
} }
public function create() public function create()
@ -46,11 +42,8 @@ class DomainsController extends Controller
]); ]);
$domain_id = Str::random(8); $domain_id = Str::random(8);
$pricing = new Pricing(); $pricing = new Pricing();
$as_usd = $pricing->convertToUSD($request->price, $request->currency); $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); $pricing->insertPricing(4, $domain_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date);
Domains::create([ Domains::create([
@ -66,6 +59,7 @@ class DomainsController extends Controller
Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $domain_id); Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $domain_id);
Cache::forget("all_domains");
Home::homePageCacheForget(); Home::homePageCacheForget();
return redirect()->route('domains.index') return redirect()->route('domains.index')
@ -74,11 +68,8 @@ class DomainsController extends Controller
public function edit(Domains $domain) public function edit(Domains $domain)
{ {
$domain_info = Domains::domainsDataEditPage($domain->id); $domain_info = Domains::domain($domain->id)[0];
return view('domains.edit', compact(['domain_info']));
$labels = Labels::labelsForService($domain->id);
return view('domains.edit', compact(['domain', 'domain_info', 'labels']));
} }
public function update(Request $request, Domains $domain) public function update(Request $request, Domains $domain)
@ -91,9 +82,7 @@ class DomainsController extends Controller
]); ]);
$pricing = new Pricing(); $pricing = new Pricing();
$as_usd = $pricing->convertToUSD($request->price, $request->currency); $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); $pricing->updatePricing($domain->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date);
$domain->update([ $domain->update([
@ -108,9 +97,10 @@ class DomainsController extends Controller
]); ]);
Labels::deleteLabelsAssignedTo($domain->id); Labels::deleteLabelsAssignedTo($domain->id);
Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $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}"); Cache::forget("labels_for_service.{$domain->id}");
Home::homePageCacheForget(); Home::homePageCacheForget();
@ -121,7 +111,6 @@ class DomainsController extends Controller
public function destroy(Domains $domain) public function destroy(Domains $domain)
{ {
$items = Domains::find($domain->id); $items = Domains::find($domain->id);
$items->delete(); $items->delete();
$p = new Pricing(); $p = new Pricing();
@ -129,6 +118,8 @@ class DomainsController extends Controller
Labels::deleteLabelsAssignedTo($domain->id); Labels::deleteLabelsAssignedTo($domain->id);
Cache::forget("all_domains");
Cache::forget("domain.{$domain->id}");
Home::homePageCacheForget(); Home::homePageCacheForget();
return redirect()->route('domains.index') return redirect()->route('domains.index')

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class Domains extends Model class Domains extends Model
@ -12,30 +13,39 @@ class Domains extends Model
public $incrementing = false; 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']; protected $fillable = ['id', 'domain', 'extension', 'ns1', 'ns2', 'ns3', 'price', 'currency', 'payment_term', 'owned_since', 'provider_id', 'next_due_date'];
public static function domainsDataIndexPage()
{ public static function allDomains()
return DB::table('domains as d') {//All domains and relationships (no using joins)
->join('providers as p', 'd.provider_id', '=', 'p.id') return Cache::remember("all_domains", now()->addMonth(1), function () {
->join('pricings as pr', 'd.id', '=', 'pr.service_id') return Domains::with(['provider', 'price', 'labels', 'labels.label'])->get();
->get(['d.*', 'p.name as provider_name', 'pr.*']); });
} }
public static function domainsDataShowPage(string $domain_id) public static function domain(string $domain_id)
{ {//Single domains and relationships (no using joins)
return DB::table('domains as d') return Cache::remember("domain.$domain_id", now()->addMonth(1), function () use ($domain_id) {
->join('providers as p', 'd.provider_id', '=', 'p.id') return Domains::where('id', $domain_id)
->join('pricings as pr', 'd.id', '=', 'pr.service_id') ->with(['provider', 'price', 'labels', 'labels.label'])->get();
->where('d.id', '=', $domain_id) });
->get(['d.*', 'p.name as provider_name', 'pr.*']);
} }
public static function domainsDataEditPage(string $domain_id) public function provider()
{ {
return DB::table('domains as d') return $this->hasOne(Providers::class, 'id', 'provider_id');
->join('pricings as pr', 'd.id', '=', 'pr.service_id')
->where('d.id', '=', $domain_id)
->get(['d.*', 'pr.*']);
} }
public function price()
{
return $this->hasOne(Pricing::class, 'service_id', 'id');
}
public function labels()
{
return $this->hasMany(LabelsAssigned::class, 'service_id', 'id');
}
} }

View File

@ -1,7 +1,7 @@
@section('title') {{'Edit domain'}} @endsection @section('title') {{'Edit domain'}} @endsection
<x-app-layout> <x-app-layout>
<x-slot name="header"> <x-slot name="header">
Edit {{ $domain->domain }}.{{ $domain->extension }} Edit {{ $domain_info->domain }}.{{ $domain_info->extension }}
</x-slot> </x-slot>
<div class="container"> <div class="container">
<x-card class="shadow mt-3"> <x-card class="shadow mt-3">
@ -11,7 +11,7 @@
Go back Go back
</x-back-button> </x-back-button>
<x-errors-alert></x-errors-alert> <x-errors-alert></x-errors-alert>
<form action="{{ route('domains.update', $domain->id) }}" method="POST"> <form action="{{ route('domains.update', $domain_info->id) }}" method="POST">
@csrf @csrf
@method('PUT') @method('PUT')
<div class="row mt-4"> <div class="row mt-4">
@ -22,7 +22,7 @@
<input type="text" <input type="text"
class="form-control" class="form-control"
name="domain" name="domain"
value="{{ $domain->domain }}"> value="{{ $domain_info->domain }}">
@error('name') <span class="text-red-500">{{ $message }} @error('name') <span class="text-red-500">{{ $message }}
</span>@enderror </span>@enderror
</div> </div>
@ -31,7 +31,7 @@
<x-text-input> <x-text-input>
<x-slot name="title">Extension</x-slot> <x-slot name="title">Extension</x-slot>
<x-slot name="name">extension</x-slot> <x-slot name="name">extension</x-slot>
<x-slot name="value">{{ $domain->extension }}</x-slot> <x-slot name="value">{{ $domain_info->extension }}</x-slot>
</x-text-input> </x-text-input>
</div> </div>
</div> </div>
@ -41,7 +41,7 @@
<x-slot name="title">NS1</x-slot> <x-slot name="title">NS1</x-slot>
<x-slot name="name">ns1</x-slot> <x-slot name="name">ns1</x-slot>
<x-slot name="max">255</x-slot> <x-slot name="max">255</x-slot>
<x-slot name="value">{{$domain->ns1}}</x-slot> <x-slot name="value">{{$domain_info->ns1}}</x-slot>
</x-text-input> </x-text-input>
</div> </div>
<div class="col-12 col-lg-4 mb-4"> <div class="col-12 col-lg-4 mb-4">
@ -49,7 +49,7 @@
<x-slot name="title">NS2</x-slot> <x-slot name="title">NS2</x-slot>
<x-slot name="name">ns2</x-slot> <x-slot name="name">ns2</x-slot>
<x-slot name="max">255</x-slot> <x-slot name="max">255</x-slot>
<x-slot name="value">{{$domain->ns2}}</x-slot> <x-slot name="value">{{$domain_info->ns2}}</x-slot>
</x-text-input> </x-text-input>
</div> </div>
<div class="col-12 col-lg-4 mb-4"> <div class="col-12 col-lg-4 mb-4">
@ -57,7 +57,7 @@
<x-slot name="title">NS3</x-slot> <x-slot name="title">NS3</x-slot>
<x-slot name="name">ns3</x-slot> <x-slot name="name">ns3</x-slot>
<x-slot name="max">255</x-slot> <x-slot name="max">255</x-slot>
<x-slot name="value">{{$domain->ns3}}</x-slot> <x-slot name="value">{{$domain_info->ns3}}</x-slot>
</x-text-input> </x-text-input>
</div> </div>
</div> </div>
@ -65,7 +65,7 @@
<div class="col-md-3 mb-3"> <div class="col-md-3 mb-3">
<x-providers-select> <x-providers-select>
<x-slot name="current"> <x-slot name="current">
{{$domain->provider_id}} {{$domain_info->provider->id}}
</x-slot> </x-slot>
</x-providers-select> </x-providers-select>
</div> </div>
@ -74,17 +74,17 @@
<x-slot name="title">Price</x-slot> <x-slot name="title">Price</x-slot>
<x-slot name="name">price</x-slot> <x-slot name="name">price</x-slot>
<x-slot name="step">0.01</x-slot> <x-slot name="step">0.01</x-slot>
<x-slot name="value">{{ $domain_info[0]->price }}</x-slot> <x-slot name="value">{{ $domain_info->price->price }}</x-slot>
</x-number-input> </x-number-input>
</div> </div>
<div class="col-md-3 mb-3"> <div class="col-md-3 mb-3">
<x-term-select> <x-term-select>
<x-slot name="current">{{$domain_info[0]->term}}</x-slot> <x-slot name="current">{{$domain_info->price->term}}</x-slot>
</x-term-select> </x-term-select>
</div> </div>
<div class="col-md-3 mb-3"> <div class="col-md-3 mb-3">
<x-currency-select> <x-currency-select>
<x-slot name="current">{{$domain_info[0]->currency}}</x-slot> <x-slot name="current">{{$domain_info->price->currency}}</x-slot>
</x-currency-select> </x-currency-select>
</div> </div>
</div> </div>
@ -93,14 +93,14 @@
<x-date-input> <x-date-input>
<x-slot name="title">Owned since</x-slot> <x-slot name="title">Owned since</x-slot>
<x-slot name="name">owned_since</x-slot> <x-slot name="name">owned_since</x-slot>
<x-slot name="value">{{$domain_info[0]->owned_since }}</x-slot> <x-slot name="value">{{$domain_info->owned_since }}</x-slot>
</x-date-input> </x-date-input>
</div> </div>
<div class="col-12 col-md-4 mb-3"> <div class="col-12 col-md-4 mb-3">
<x-date-input> <x-date-input>
<x-slot name="title">Next due date</x-slot> <x-slot name="title">Next due date</x-slot>
<x-slot name="name">next_due_date</x-slot> <x-slot name="name">next_due_date</x-slot>
<x-slot name="value">{{$domain_info[0]->next_due_date}}</x-slot> <x-slot name="value">{{$domain_info->price->next_due_date}}</x-slot>
</x-date-input> </x-date-input>
</div> </div>
</div> </div>
@ -110,8 +110,8 @@
<x-labels-select> <x-labels-select>
<x-slot name="title">label</x-slot> <x-slot name="title">label</x-slot>
<x-slot name="name">label1</x-slot> <x-slot name="name">label1</x-slot>
@if(isset($labels[0]->id)) @if(isset($domain_info->labels[0]->label))
<x-slot name="current">{{$labels[0]->id}}</x-slot> <x-slot name="current">{{$domain_info->labels[0]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>
@ -119,8 +119,8 @@
<x-labels-select> <x-labels-select>
<x-slot name="title">label</x-slot> <x-slot name="title">label</x-slot>
<x-slot name="name">label2</x-slot> <x-slot name="name">label2</x-slot>
@if(isset($labels[1]->id)) @if(isset($domain_info->labels[1]->label))
<x-slot name="current">{{$labels[1]->id}}</x-slot> <x-slot name="current">{{$domain_info->labels[1]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>
@ -128,8 +128,8 @@
<x-labels-select> <x-labels-select>
<x-slot name="title">label</x-slot> <x-slot name="title">label</x-slot>
<x-slot name="name">label3</x-slot> <x-slot name="name">label3</x-slot>
@if(isset($labels[2]->id)) @if(isset($domain_info->labels[2]->label))
<x-slot name="current">{{$labels[2]->id}}</x-slot> <x-slot name="current">{{$domain_info->labels[2]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>
@ -137,8 +137,8 @@
<x-labels-select> <x-labels-select>
<x-slot name="title">label</x-slot> <x-slot name="title">label</x-slot>
<x-slot name="name">label4</x-slot> <x-slot name="name">label4</x-slot>
@if(isset($labels[3]->id)) @if(isset($domain_info->labels[3]->label))
<x-slot name="current">{{$labels[3]->id}}</x-slot> <x-slot name="current">{{$domain_info->labels[3]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>
@ -146,7 +146,7 @@
<div class="form-check mt-2"> <div class="form-check mt-2">
<input class="form-check-input" name="is_active" type="checkbox" <input class="form-check-input" name="is_active" type="checkbox"
value="1" {{ ($domain_info[0]->active === 1) ? 'checked' : '' }}> value="1" {{ ($domain_info->active === 1) ? 'checked' : '' }}>
<label class="form-check-label"> <label class="form-check-label">
I still have this service I still have this service
</label> </label>

View File

@ -34,22 +34,22 @@
<td class="text-nowrap"><a href="https://{{ $domain->domain }}.{{$domain->extension}}" <td class="text-nowrap"><a href="https://{{ $domain->domain }}.{{$domain->extension}}"
class="text-decoration-none">{{ $domain->domain }}.{{$domain->extension}}</a></td> class="text-decoration-none">{{ $domain->domain }}.{{$domain->extension}}</a></td>
<td class="text-nowrap">{{ $domain->owned_since}}</td> <td class="text-nowrap">{{ $domain->owned_since}}</td>
<td class="text-nowrap">{{ now()->diffInDays($domain->next_due_date) }} <small>days</small> <td class="text-nowrap">{{ now()->diffInDays($domain->price->next_due_date) }} <small>days</small>
</td> </td>
<td class="text-nowrap">{{ $domain->provider_name}}</td> <td class="text-nowrap">{{ $domain->provider->name}}</td>
<td class="text-nowrap">{{ $domain->price }} <small>{{$domain->currency}}</small></td> <td class="text-nowrap">{{ $domain->price->price }} <small>{{$domain->price->currency}}</small></td>
<td class="text-nowrap"> <td class="text-nowrap">
<form action="{{ route('domains.destroy', $domain->service_id) }}" method="POST"> <form action="{{ route('domains.destroy', $domain->id) }}" method="POST">
<a href="{{ route('domains.show', $domain->service_id) }}" <a href="{{ route('domains.show', $domain->id) }}"
class="text-body mx-1"> class="text-body mx-1">
<i class="fas fa-eye" title="view"></i></a> <i class="fas fa-eye" title="view"></i></a>
<a href="{{ route('domains.edit', $domain->service_id) }}" <a href="{{ route('domains.edit', $domain->id) }}"
class="text-body mx-1"> class="text-body mx-1">
<i class="fas fa-pen" title="edit"></i></a> <i class="fas fa-pen" title="edit"></i></a>
@csrf @csrf
@method('DELETE') @method('DELETE')
<i class="fas fa-trash text-danger ms-3" @click="modalForm" <i class="fas fa-trash text-danger ms-3" @click="modalForm"
id="btn-{{$domain->domain}}" title="{{$domain->service_id}}"></i> id="btn-{{$domain->domain}}" title="{{$domain->id}}"></i>
</form> </form>
</td> </td>
</tr> </tr>

View File

@ -1,4 +1,6 @@
@section('title') {{ $domain->domain }}.{{$domain->extension}} {{'domain'}} @endsection @section('title')
{{ $domain_info->domain }}.{{$domain_info->extension}} {{'domain'}}
@endsection
<x-app-layout> <x-app-layout>
<x-slot name="header"> <x-slot name="header">
{{ __('Domain details') }} {{ __('Domain details') }}
@ -7,18 +9,14 @@
<x-card class="shadow mt-3"> <x-card class="shadow mt-3">
<div class="row"> <div class="row">
<div class="col-12 col-md-6 mb-2"> <div class="col-12 col-md-6 mb-2">
<h2>{{ $domain->domain }}.{{$domain->extension}}</h2> <h2>{{ $domain_info->domain }}.{{$domain_info->extension}}</h2>
<code>@foreach($labels as $label) @foreach($domain_info->labels as $label)
@if($loop->last) <span class="badge bg-primary mx-1">{{$label->label->label}}</span>
{{$label->label}} @endforeach
@else
{{$label->label}},
@endif
@endforeach</code>
</div> </div>
<div class="col-12 col-md-6 text-md-end"> <div class="col-12 col-md-6 text-md-end">
<h6 class="text-muted pe-lg-4">{{ $domain->id }}</h6> <h6 class="text-muted pe-lg-4">{{ $domain_info->id }}</h6>
@if($domain->active !== 1) @if($domain_info->active !== 1)
<h6 class="text-danger pe-lg-4">not active</h6> <h6 class="text-danger pe-lg-4">not active</h6>
@endif @endif
</div> </div>
@ -30,57 +28,59 @@
<tbody> <tbody>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Domain</td> <td class="px-2 py-2 font-bold text-muted">Domain</td>
<td><a href="https://{{ $domain->domain }}.{{$domain->extension}}" class="text-decoration-none">{{ $domain->domain }}.{{$domain->extension}}</a></td> <td><a href="https://{{ $domain_info->domain }}.{{$domain_info->extension}}"
class="text-decoration-none">{{ $domain_info->domain }}
.{{$domain_info->extension}}</a></td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Provider</td> <td class="px-2 py-2 font-bold text-muted">Provider</td>
<td>{{ $service_extras[0]->provider_name }}</td> <td>{{ $domain_info->provider->name }}</td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Price</td> <td class="px-2 py-2 font-bold text-muted">Price</td>
<td>{{ $service_extras[0]->price }} {{ $service_extras[0]->currency }} <td>{{ $domain_info->price->price }} {{ $domain_info->price->currency }}
<small>{{\App\Process::paymentTermIntToString($service_extras[0]->term)}}</small> <small>{{\App\Process::paymentTermIntToString($domain_info->price->term)}}</small>
</td> </td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">NS1</td> <td class="px-2 py-2 font-bold text-muted">NS1</td>
<td>{{ $domain->ns1 }}</td> <td>{{ $domain_info->ns1 }}</td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">NS2</td> <td class="px-2 py-2 font-bold text-muted">NS2</td>
<td>{{ $domain->ns2 }}</td> <td>{{ $domain_info->ns2 }}</td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">NS3</td> <td class="px-2 py-2 font-bold text-muted">NS3</td>
<td>{{ $domain->ns3 }}</td> <td>{{ $domain_info->ns3 }}</td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Owned since</td> <td class="px-2 py-2 font-bold text-muted">Owned since</td>
<td> <td>
@if(!is_null($domain->owned_since)) @if(!is_null($domain_info->owned_since))
{{ date_format(new DateTime($domain->owned_since), 'jS F Y') }} {{ date_format(new DateTime($domain_info->owned_since), 'jS F Y') }}
@endif @endif
</td> </td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Next due date</td> <td class="px-2 py-2 font-bold text-muted">Next due date</td>
<td>{{Carbon\Carbon::parse($service_extras[0]->next_due_date)->diffForHumans()}} <td>{{Carbon\Carbon::parse($domain_info->price->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)->format('d/m/Y')}})
</td> </td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Inserted</td> <td class="px-2 py-2 font-bold text-muted">Inserted</td>
<td> <td>
@if(!is_null($domain->created_at)) @if(!is_null($domain_info->created_at))
{{ date_format(new DateTime($domain->created_at), 'jS M y g:i a') }} {{ date_format(new DateTime($domain_info->created_at), 'jS M y g:i a') }}
@endif @endif
</td> </td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Updated</td> <td class="px-2 py-2 font-bold text-muted">Updated</td>
<td> <td>
@if(!is_null($domain->updated_at)) @if(!is_null($domain_info->updated_at))
{{ date_format(new DateTime($domain->updated_at), 'jS M y g:i a') }} {{ date_format(new DateTime($domain_info->updated_at), 'jS M y g:i a') }}
@endif @endif
</td> </td>
</tr> </tr>
@ -89,14 +89,12 @@
</div> </div>
</div> </div>
</div> </div>
<a href="{{ route('domains.index') }}" <x-back-btn>
class="btn btn-success btn-sm mx-2"> <x-slot name="route">{{ route('domains.index') }}</x-slot>
Go back </x-back-btn>
</a> <x-edit-btn>
<a href="{{ route('domains.edit', $domain->id) }}" <x-slot name="route">{{ route('domains.edit', $domain_info->id) }}</x-slot>
class="btn btn-primary btn-sm mx-2"> </x-edit-btn>
Edit
</a>
</x-card> </x-card>
</div> </div>
</x-app-layout> </x-app-layout>