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

View File

@ -17,30 +17,44 @@ class Shared extends Model
public $incrementing = false;
public static function sharedDataIndexPage()
{
return DB::table('shared_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 allSharedHosting()
{//All shared hosting and relationships (no using joins)
return Cache::remember("all_shared", now()->addMonth(1), function () {
return Shared::with(['location', 'provider', 'price', 'ips', 'labels', 'labels.label'])->get();
});
}
public static function sharedDataShowPage(string $shared_id)
{
return DB::table('shared_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', '=', $shared_id)
->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*']);
public static function sharedHosting(string $shared_id)
{//Single shared hosting and relationships (no using joins)
return Cache::remember("shared_hosting.$shared_id", now()->addMonth(1), function () use ($shared_id) {
return Shared::where('id', $shared_id)
->with(['location', 'provider', 'price', 'ips', 'labels', 'labels.label'])->get();
});
}
public static function sharedEditDataPage(string $shared_id)
public function ips()
{
return DB::table('shared_hosting as s')
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $shared_id)
->get(['s.*', 'p.*']);
return $this->hasMany(IPs::class, 'service_id', 'id');
}
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
</a>
<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
@method('PUT')
<div class="row mt-3">
@ -27,7 +27,7 @@
</div>
</div>
<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-prepend"><span class="input-group-text">Type</span></div>
<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="name">dedicated_ip</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>
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<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>
</div>
<div class="col-md-3 mb-3">
<x-number-input>
<x-slot name="title">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="step">0.01</x-slot>
<x-slot name="required"></x-slot>
@ -102,19 +102,19 @@
</div>
<div class="col-md-3 mb-3">
<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>
</div>
<div class="col-md-3 mb-3">
<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>
</div>
</div>
<div class="row mb-2">
<div class="col-12 col-md-3 mb-3">
<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>
</div>
<div class="col-12 col-md-3 mb-3">
@ -135,7 +135,7 @@
<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">{{$shared[0]->next_due_date }}</x-slot>
<x-slot name="value">{{$shared[0]->price->next_due_date }}</x-slot>
</x-date-input>
</div>
</div>
@ -219,8 +219,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($shared[0]->labels[0]->label->id))
<x-slot name="current">{{$shared[0]->labels[0]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>
@ -228,8 +228,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($shared[0]->labels[1]->label->id))
<x-slot name="current">{{$shared[0]->labels[1]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>
@ -237,8 +237,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($shared[0]->labels[2]->label->id))
<x-slot name="current">{{$shared[0]->labels[2]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>
@ -246,8 +246,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($shared[0]->labels[3]->label->id))
<x-slot name="current">{{$shared[0]->labels[3]->label->id}}</x-slot>
@endif
</x-labels-select>
</div>

View File

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

View File

@ -8,13 +8,9 @@
<div class="row">
<div class="col-12 col-md-6 mb-2">
<h2>{{ $shared->main_domain }}</h2>
<code>@foreach($labels as $label)
@if($loop->last)
{{$label->label}}
@else
{{$label->label}},
@endif
@endforeach</code>
@foreach($shared->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">{{ $shared->id }}</h6>
@ -34,26 +30,26 @@
</tr>
<tr>
<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>
<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>
<td class="px-2 py-2 font-bold text-muted">Price</td>
<td>{{ $shared_extras[0]->price }} {{ $shared_extras[0]->currency }}
<small>{{\App\Process::paymentTermIntToString($shared_extras[0]->term)}}</small>
<td>{{ $shared->price->price }} {{ $shared->price->currency }}
<small>{{\App\Process::paymentTermIntToString($shared->price->term)}}</small>
</td>
</tr>
<tr>
<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>
<td class="px-2 py-2 font-bold text-muted">Has dedicated IP?</td>
<td>
@if(isset($ip_address[0]->address))
@if(isset($shared->ips[0]->address))
Yes
@else
No
@ -62,8 +58,8 @@
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">IP</td>
<td><code>@if(isset($ip_address[0]->address))
{{$ip_address[0]->address}}
<td><code>@if(isset($shared->ips[0]->address))
{{$shared->ips[0]->address}}
@endif
</code></td>
</tr>
@ -77,8 +73,8 @@
</tr>
<tr>
<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()}}
({{Carbon\Carbon::parse($shared_extras[0]->next_due_date)->format('d/m/Y')}})
<td>{{Carbon\Carbon::parse($shared->price->next_due_date)->diffForHumans()}}
({{Carbon\Carbon::parse($shared->price->next_due_date)->format('d/m/Y')}})
</td>
</tr>
<tr>