Updated Shared class to use relationships

Updated Shared class to use relationships
No more joins
Added cache for all shared and single shared
This commit is contained in:
cp6 2022-07-19 15:21:13 +10:00
parent 0491f84ed4
commit aaac697774
5 changed files with 82 additions and 89 deletions

View File

@ -16,8 +16,7 @@ class SharedController extends Controller
{ {
public function index() public function index()
{ {
$shared = Shared::sharedDataIndexPage(); $shared = Shared::allSharedHosting();
return view('shared.index', compact(['shared'])); return view('shared.index', compact(['shared']));
} }
@ -28,7 +27,6 @@ class SharedController extends Controller
public function store(Request $request) public function store(Request $request)
{ {
$request->validate([ $request->validate([
'domain' => 'required|min:4', 'domain' => 'required|min:4',
'shared_type' => 'required', 'shared_type' => 'required',
@ -54,17 +52,13 @@ class SharedController extends Controller
$shared_id = Str::random(8); $shared_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(2, $shared_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); $pricing->insertPricing(2, $shared_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date);
Labels::deleteLabelsAssignedTo($shared_id); Labels::deleteLabelsAssignedTo($shared_id);
Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $shared_id); Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $shared_id);
IPs::deleteIPsAssignedTo($shared_id); IPs::deleteIPsAssignedTo($shared_id);
if (!is_null($request->dedicated_ip)) { if (!is_null($request->dedicated_ip)) {
IPs::insertIP($shared_id, $request->dedicated_ip); IPs::insertIP($shared_id, $request->dedicated_ip);
} }
@ -88,6 +82,7 @@ class SharedController extends Controller
'db__limit' => $request->db 'db__limit' => $request->db
]); ]);
Cache::forget('all_shared');
Home::homePageCacheForget(); Home::homePageCacheForget();
return redirect()->route('shared.index') return redirect()->route('shared.index')
@ -96,24 +91,14 @@ class SharedController extends Controller
public function show(Shared $shared) public function show(Shared $shared)
{ {
$shared_extras = Shared::sharedDataShowPage($shared->id); $shared = Shared::sharedHosting($shared->id)[0];
return view('shared.show', compact(['shared']));
$labels = Labels::labelsForService($shared->id);
$ip_address = IPs::ipsForServer($shared->id);
return view('shared.show', compact(['shared', 'shared_extras', 'labels', 'ip_address']));
} }
public function edit(Shared $shared) public function edit(Shared $shared)
{ {
$labels = Labels::labelsForService($shared->id); $shared = Shared::sharedHosting($shared->id);
return view('shared.edit', compact(['shared']));
$ip_address = IPs::ipsForServer($shared->id);
$shared = Shared::sharedEditDataPage($shared->id);
return view('shared.edit', compact(['shared', 'labels', 'ip_address']));
} }
public function update(Request $request, Shared $shared) public function update(Request $request, Shared $shared)
@ -161,23 +146,20 @@ class SharedController 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($request->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); $pricing->updatePricing($request->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date);
Labels::deleteLabelsAssignedTo($request->id); Labels::deleteLabelsAssignedTo($request->id);
Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $request->id); Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $request->id);
Cache::forget("labels_for_service.{$request->id}"); Cache::forget("labels_for_service.{$request->id}");
IPs::deleteIPsAssignedTo($request->id); IPs::deleteIPsAssignedTo($request->id);
if (isset($request->dedicated_ip)) { if (isset($request->dedicated_ip)) {
IPs::insertIP($request->id, $request->dedicated_ip); IPs::insertIP($request->id, $request->dedicated_ip);
} }
Cache::forget("shared_hosting.{$request->id}");
Cache::forget('all_shared');
Home::homePageCacheForget(); Home::homePageCacheForget();
return redirect()->route('shared.index') return redirect()->route('shared.index')
@ -188,7 +170,6 @@ class SharedController extends Controller
{ {
$shared_id = $shared->id; $shared_id = $shared->id;
$items = Shared::find($shared_id); $items = Shared::find($shared_id);
$items->delete(); $items->delete();
$p = new Pricing(); $p = new Pricing();
@ -198,6 +179,8 @@ class SharedController extends Controller
IPs::deleteIPsAssignedTo($shared_id); IPs::deleteIPsAssignedTo($shared_id);
Cache::forget("shared_hosting.$shared_id");
Cache::forget('all_shared');
Home::homePageCacheForget(); Home::homePageCacheForget();
return redirect()->route('shared.index') return redirect()->route('shared.index')

View File

@ -17,30 +17,44 @@ class Shared extends Model
public $incrementing = false; public $incrementing = false;
public static function sharedDataIndexPage() public static function allSharedHosting()
{ {//All shared hosting and relationships (no using joins)
return DB::table('shared_hosting as s') return Cache::remember("all_shared", now()->addMonth(1), function () {
->join('providers as p', 's.provider_id', '=', 'p.id') return Shared::with(['location', 'provider', 'price', 'ips', 'labels', 'labels.label'])->get();
->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 sharedDataShowPage(string $shared_id) public static function sharedHosting(string $shared_id)
{ {//Single shared hosting and relationships (no using joins)
return DB::table('shared_hosting as s') return Cache::remember("shared_hosting.$shared_id", now()->addMonth(1), function () use ($shared_id) {
->join('pricings as pr', 's.id', '=', 'pr.service_id') return Shared::where('id', $shared_id)
->join('providers as p', 's.provider_id', '=', 'p.id') ->with(['location', 'provider', 'price', 'ips', 'labels', 'labels.label'])->get();
->join('locations as l', 's.location_id', '=', 'l.id') });
->where('s.id', '=', $shared_id)
->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*']);
} }
public static function sharedEditDataPage(string $shared_id) public function ips()
{ {
return DB::table('shared_hosting as s') return $this->hasMany(IPs::class, 'service_id', 'id');
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $shared_id)
->get(['s.*', 'p.*']);
} }
public function location()
{
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');
}
} }

View File

@ -12,7 +12,7 @@
Back to shared hosting Back to shared hosting
</a> </a>
<x-auth-validation-errors></x-auth-validation-errors> <x-auth-validation-errors></x-auth-validation-errors>
<form action="{{ route('shared.update', $shared[0]->service_id) }}" method="POST"> <form action="{{ route('shared.update', $shared[0]->id) }}" method="POST">
@csrf @csrf
@method('PUT') @method('PUT')
<div class="row mt-3"> <div class="row mt-3">
@ -27,7 +27,7 @@
</div> </div>
</div> </div>
<div class="col-12 col-lg-3 mb-4"> <div class="col-12 col-lg-3 mb-4">
<input type="hidden" name="id" value="{{$shared[0]->service_id}}"> <input type="hidden" name="id" value="{{$shared[0]->id}}">
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">Type</span></div> <div class="input-group-prepend"><span class="input-group-text">Type</span></div>
<select class="form-control" id="shared_type" name="shared_type"> <select class="form-control" id="shared_type" name="shared_type">
@ -80,21 +80,21 @@
<x-slot name="title">Dedicated IP</x-slot> <x-slot name="title">Dedicated IP</x-slot>
<x-slot name="name">dedicated_ip</x-slot> <x-slot name="name">dedicated_ip</x-slot>
<x-slot name="max">255</x-slot> <x-slot name="max">255</x-slot>
<x-slot name="value">@if(isset($ip_address[0]['address'])) {{$ip_address[0]['address']}}@endif</x-slot> <x-slot name="value">@if(isset($shared[0]->ips[0]->address)) {{$shared[0]->ips[0]->address}}@endif</x-slot>
</x-text-input> </x-text-input>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-md-3 mb-3"> <div class="col-md-3 mb-3">
<x-providers-select> <x-providers-select>
<x-slot name="current">{{$shared[0]->provider_id}}</x-slot> <x-slot name="current">{{$shared[0]->provider->id}}</x-slot>
</x-providers-select> </x-providers-select>
</div> </div>
<div class="col-md-3 mb-3"> <div class="col-md-3 mb-3">
<x-number-input> <x-number-input>
<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="value">{{$shared[0]->price}}</x-slot> <x-slot name="value">{{$shared[0]->price->price}}</x-slot>
<x-slot name="max">9999</x-slot> <x-slot name="max">9999</x-slot>
<x-slot name="step">0.01</x-slot> <x-slot name="step">0.01</x-slot>
<x-slot name="required"></x-slot> <x-slot name="required"></x-slot>
@ -102,19 +102,19 @@
</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">{{$shared[0]->term}}</x-slot> <x-slot name="current">{{$shared[0]->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">{{$shared[0]->currency}}</x-slot> <x-slot name="current">{{$shared[0]->price->currency}}</x-slot>
</x-currency-select> </x-currency-select>
</div> </div>
</div> </div>
<div class="row mb-2"> <div class="row mb-2">
<div class="col-12 col-md-3 mb-3"> <div class="col-12 col-md-3 mb-3">
<x-locations-select> <x-locations-select>
<x-slot name="current">{{$shared[0]->location_id}}</x-slot> <x-slot name="current">{{$shared[0]->location->id}}</x-slot>
</x-locations-select> </x-locations-select>
</div> </div>
<div class="col-12 col-md-3 mb-3"> <div class="col-12 col-md-3 mb-3">
@ -135,7 +135,7 @@
<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">{{$shared[0]->next_due_date }}</x-slot> <x-slot name="value">{{$shared[0]->price->next_due_date }}</x-slot>
</x-date-input> </x-date-input>
</div> </div>
</div> </div>
@ -219,8 +219,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($shared[0]->labels[0]->label->id))
<x-slot name="current">{{$labels[0]->id}}</x-slot> <x-slot name="current">{{$shared[0]->labels[0]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>
@ -228,8 +228,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($shared[0]->labels[1]->label->id))
<x-slot name="current">{{$labels[1]->id}}</x-slot> <x-slot name="current">{{$shared[0]->labels[1]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>
@ -237,8 +237,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($shared[0]->labels[2]->label->id))
<x-slot name="current">{{$labels[2]->id}}</x-slot> <x-slot name="current">{{$shared[0]->labels[2]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>
@ -246,8 +246,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($shared[0]->labels[3]->label->id))
<x-slot name="current">{{$labels[3]->id}}</x-slot> <x-slot name="current">{{$shared[0]->labels[3]->label->id}}</x-slot>
@endif @endif
</x-labels-select> </x-labels-select>
</div> </div>

View File

@ -41,25 +41,25 @@
<tr> <tr>
<td>{{ $row->main_domain }}</td> <td>{{ $row->main_domain }}</td>
<td>{{ $row->shared_type }}</td> <td>{{ $row->shared_type }}</td>
<td class="text-nowrap">{{ $row->location }}</td> <td class="text-nowrap">{{ $row->location->name }}</td>
<td class="text-nowrap">{{ $row->provider_name }}</td> <td class="text-nowrap">{{ $row->provider->name }}</td>
<td>{{ $row->disk_as_gb }} <small>GB</small></td> <td>{{ $row->disk_as_gb }} <small>GB</small></td>
<td>{{ $row->price }} {{$row->currency}} {{\App\Process::paymentTermIntToString($row->term)}}</td> <td>{{ $row->price->price }} {{$row->price->currency}} {{\App\Process::paymentTermIntToString($row->price->term)}}</td>
<td>{{Carbon\Carbon::parse($row->next_due_date)->diffForHumans()}}</td> <td>{{Carbon\Carbon::parse($row->price->next_due_date)->diffForHumans()}}</td>
<td class="text-nowrap">{{ $row->owned_since }}</td> <td class="text-nowrap">{{ $row->owned_since }}</td>
<td class="text-nowrap"> <td class="text-nowrap">
<form action="{{ route('shared.destroy', $row->service_id) }}" method="POST"> <form action="{{ route('shared.destroy', $row->id) }}" method="POST">
<a href="{{ route('shared.show', $row->service_id) }}" <a href="{{ route('shared.show', $row->id) }}"
class="text-body mx-1"> class="text-body mx-1">
<i class="fas fa-eye" title="view"></i> <i class="fas fa-eye" title="view"></i>
</a> </a>
<a href="{{ route('shared.edit', $row->service_id) }}" <a href="{{ route('shared.edit', $row->id) }}"
class="text-body mx-1"> class="text-body mx-1">
<i class="fas fa-pen" title="edit"></i> <i class="fas fa-pen" title="edit"></i>
</a> </a>
<i class="fas fa-trash text-danger ms-3" @click="modalForm" <i class="fas fa-trash text-danger ms-3" @click="modalForm"
id="btn-{{$row->main_domain}}" title="{{$row->service_id}}"></i> id="btn-{{$row->main_domain}}" title="{{$row->id}}"></i>
</form> </form>
</td> </td>
</tr> </tr>

View File

@ -8,13 +8,9 @@
<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>{{ $shared->main_domain }}</h2> <h2>{{ $shared->main_domain }}</h2>
<code>@foreach($labels as $label) @foreach($shared->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">{{ $shared->id }}</h6> <h6 class="text-muted pe-lg-4">{{ $shared->id }}</h6>
@ -34,26 +30,26 @@
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Location</td> <td class="px-2 py-2 font-bold text-muted">Location</td>
<td>{{ $shared_extras[0]->location }}</td> <td>{{ $shared->location->name }}</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>{{ $shared_extras[0]->provider_name }}</td> <td>{{ $shared->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>{{ $shared_extras[0]->price }} {{ $shared_extras[0]->currency }} <td>{{ $shared->price->price }} {{ $shared->price->currency }}
<small>{{\App\Process::paymentTermIntToString($shared_extras[0]->term)}}</small> <small>{{\App\Process::paymentTermIntToString($shared->price->term)}}</small>
</td> </td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Was promo</td> <td class="px-2 py-2 font-bold text-muted">Was promo</td>
<td>{{ ($shared_extras[0]->was_promo === 1) ? 'Yes' : 'No' }}</td> <td>{{ ($shared->was_promo === 1) ? 'Yes' : 'No' }}</td>
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Has dedicated IP?</td> <td class="px-2 py-2 font-bold text-muted">Has dedicated IP?</td>
<td> <td>
@if(isset($ip_address[0]->address)) @if(isset($shared->ips[0]->address))
Yes Yes
@else @else
No No
@ -62,8 +58,8 @@
</tr> </tr>
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">IP</td> <td class="px-2 py-2 font-bold text-muted">IP</td>
<td><code>@if(isset($ip_address[0]->address)) <td><code>@if(isset($shared->ips[0]->address))
{{$ip_address[0]->address}} {{$shared->ips[0]->address}}
@endif @endif
</code></td> </code></td>
</tr> </tr>
@ -77,8 +73,8 @@
</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($shared_extras[0]->next_due_date)->diffForHumans()}} <td>{{Carbon\Carbon::parse($shared->price->next_due_date)->diffForHumans()}}
({{Carbon\Carbon::parse($shared_extras[0]->next_due_date)->format('d/m/Y')}}) ({{Carbon\Carbon::parse($shared->price->next_due_date)->format('d/m/Y')}})
</td> </td>
</tr> </tr>
<tr> <tr>