1
0
mirror of https://github.com/cp6/my-idlers.git synced 2024-11-16 23:42:32 +01:00

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
{
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')

View File

@ -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');
}
}

View File

@ -1,7 +1,7 @@
@section('title') {{'Edit domain'}} @endsection
<x-app-layout>
<x-slot name="header">
Edit {{ $domain->domain }}.{{ $domain->extension }}
Edit {{ $domain_info->domain }}.{{ $domain_info->extension }}
</x-slot>
<div class="container">
<x-card class="shadow mt-3">
@ -11,7 +11,7 @@
Go back
</x-back-button>
<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
@method('PUT')
<div class="row mt-4">
@ -22,7 +22,7 @@
<input type="text"
class="form-control"
name="domain"
value="{{ $domain->domain }}">
value="{{ $domain_info->domain }}">
@error('name') <span class="text-red-500">{{ $message }}
</span>@enderror
</div>
@ -31,7 +31,7 @@
<x-text-input>
<x-slot name="title">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>
</div>
</div>
@ -41,7 +41,7 @@
<x-slot name="title">NS1</x-slot>
<x-slot name="name">ns1</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>
</div>
<div class="col-12 col-lg-4 mb-4">
@ -49,7 +49,7 @@
<x-slot name="title">NS2</x-slot>
<x-slot name="name">ns2</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>
</div>
<div class="col-12 col-lg-4 mb-4">
@ -57,7 +57,7 @@
<x-slot name="title">NS3</x-slot>
<x-slot name="name">ns3</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>
</div>
</div>
@ -65,7 +65,7 @@
<div class="col-md-3 mb-3">
<x-providers-select>
<x-slot name="current">
{{$domain->provider_id}}
{{$domain_info->provider->id}}
</x-slot>
</x-providers-select>
</div>
@ -74,17 +74,17 @@
<x-slot name="title">Price</x-slot>
<x-slot name="name">price</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>
</div>
<div class="col-md-3 mb-3">
<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>
</div>
<div class="col-md-3 mb-3">
<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>
</div>
</div>
@ -93,14 +93,14 @@
<x-date-input>
<x-slot name="title">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>
</div>
<div class="col-12 col-md-4 mb-3">
<x-date-input>
<x-slot name="title">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>
</div>
</div>
@ -110,8 +110,8 @@
<x-labels-select>
<x-slot name="title">label</x-slot>
<x-slot name="name">label1</x-slot>
@if(isset($labels[0]->id))
<x-slot name="current">{{$labels[0]->id}}</x-slot>
@if(isset($domain_info->labels[0]->label))
<x-slot name="current">{{$domain_info->labels[0]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>
@ -119,8 +119,8 @@
<x-labels-select>
<x-slot name="title">label</x-slot>
<x-slot name="name">label2</x-slot>
@if(isset($labels[1]->id))
<x-slot name="current">{{$labels[1]->id}}</x-slot>
@if(isset($domain_info->labels[1]->label))
<x-slot name="current">{{$domain_info->labels[1]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>
@ -128,8 +128,8 @@
<x-labels-select>
<x-slot name="title">label</x-slot>
<x-slot name="name">label3</x-slot>
@if(isset($labels[2]->id))
<x-slot name="current">{{$labels[2]->id}}</x-slot>
@if(isset($domain_info->labels[2]->label))
<x-slot name="current">{{$domain_info->labels[2]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>
@ -137,8 +137,8 @@
<x-labels-select>
<x-slot name="title">label</x-slot>
<x-slot name="name">label4</x-slot>
@if(isset($labels[3]->id))
<x-slot name="current">{{$labels[3]->id}}</x-slot>
@if(isset($domain_info->labels[3]->label))
<x-slot name="current">{{$domain_info->labels[3]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>
@ -146,7 +146,7 @@
<div class="form-check mt-2">
<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">
I still have this service
</label>

View File

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