From a054e83d1ee454c771643daffd0df7bf81780c13 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 15 May 2022 12:01:48 +1000 Subject: [PATCH] Updated Shared controller Moved DB calls to model Used existing DB call functions --- app/Http/Controllers/SharedController.php | 70 ++++++----------------- app/Models/Labels.php | 4 +- app/Models/Shared.php | 37 ++++++++++++ 3 files changed, 58 insertions(+), 53 deletions(-) diff --git a/app/Http/Controllers/SharedController.php b/app/Http/Controllers/SharedController.php index 2f9dd78..32c5ad0 100644 --- a/app/Http/Controllers/SharedController.php +++ b/app/Http/Controllers/SharedController.php @@ -4,9 +4,7 @@ namespace App\Http\Controllers; use App\Models\IPs; use App\Models\Labels; -use App\Models\Locations; use App\Models\Pricing; -use App\Models\Providers; use App\Models\Shared; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; @@ -17,20 +15,14 @@ class SharedController extends Controller { public function index() { - $shared = 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']); + $shared = Shared::sharedDataIndexPage(); return view('shared.index', compact(['shared'])); } public function create() { - $Providers = Providers::allProviders(); - $Locations = Locations::allLocations(); - return view('shared.create', compact(['Providers', 'Locations'])); + return view('shared.create'); } public function store(Request $request) @@ -95,9 +87,7 @@ class SharedController extends Controller 'db__limit' => $request->db ]); - Cache::forget('services_count');//Main page services_count cache - Cache::forget('due_soon');//Main page due_soon cache - Cache::forget('recently_added');//Main page recently_added cache + Shared::sharedRelatedCacheForget(); return redirect()->route('shared.index') ->with('success', 'Shared hosting created Successfully.'); @@ -105,44 +95,24 @@ class SharedController extends Controller public function show(Shared $shared) { - $shared_extras = 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.*']); + $shared_extras = Shared::sharedDataShowPage($shared->id); - $labels = DB::table('labels_assigned as l') - ->join('labels', 'l.label_id', '=', 'labels.id') - ->where('l.service_id', '=', $shared->id) - ->get(['labels.label']); + $labels = Labels::labelsForService($shared->id); - $ip_address = DB::table('ips as i') - ->where('i.service_id', '=', $shared->id) - ->get(); + $ip_address = IPs::ipsForServer($shared->id); return view('shared.show', compact(['shared', 'shared_extras', 'labels', 'ip_address'])); } public function edit(Shared $shared) { - $locations = DB::table('locations')->get(['*']); - $providers = json_decode(DB::table('providers')->get(['*']), true); - $labels = DB::table('labels_assigned as l') - ->join('labels', 'l.label_id', '=', 'labels.id') - ->where('l.service_id', '=', $shared->id) - ->get(['labels.id', 'labels.label']); + $labels = Labels::labelsForService($shared->id); - $ip_address = json_decode(DB::table('ips as i') - ->where('i.service_id', '=', $shared->id) - ->get(), true); + $ip_address = IPs::ipsForServer($shared->id); - $shared = DB::table('shared_hosting as s') - ->join('pricings as p', 's.id', '=', 'p.service_id') - ->where('s.id', '=', $shared->id) - ->get(['s.*', 'p.*']); + $shared = Shared::sharedEditDataPage($shared->id); - return view('shared.edit', compact(['shared', 'locations', 'providers', 'labels', 'ip_address'])); + return view('shared.edit', compact(['shared', 'labels', 'ip_address'])); } public function update(Request $request, Shared $shared) @@ -199,15 +169,15 @@ class SharedController extends Controller 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('services_count');//Main page services_count cache - Cache::forget('due_soon');//Main page due_soon cache - Cache::forget('recently_added');//Main page recently_added cache + Shared::sharedRelatedCacheForget(); return redirect()->route('shared.index') ->with('success', 'Shared hosting updated Successfully.'); @@ -215,21 +185,19 @@ class SharedController extends Controller public function destroy(Shared $shared) { - $id = $shared->id; - $items = Shared::find($id); + $shared_id = $shared->id; + $items = Shared::find($shared_id); $items->delete(); $p = new Pricing(); - $p->deletePricing($shared->id); + $p->deletePricing($shared_id); - Labels::deleteLabelsAssignedTo($shared->id); + Labels::deleteLabelsAssignedTo($shared_id); - IPs::deleteIPsAssignedTo($shared->id); + IPs::deleteIPsAssignedTo($shared_id); - Cache::forget('services_count');//Main page services_count cache - Cache::forget('due_soon');//Main page due_soon cache - Cache::forget('recently_added');//Main page recently_added cache + Shared::sharedRelatedCacheForget(); return redirect()->route('shared.index') ->with('success', 'Shared hosting was deleted Successfully.'); diff --git a/app/Models/Labels.php b/app/Models/Labels.php index 945a682..8c7a2fc 100644 --- a/app/Models/Labels.php +++ b/app/Models/Labels.php @@ -43,11 +43,11 @@ class Labels extends Model public static function labelsForService(string $service_id) { - return Cache::remember("labels_for_service.$service_id", now()->addMinute(1), function () use ($service_id) { + return Cache::remember("labels_for_service.$service_id", now()->addMinute(5), function () use ($service_id) { return DB::table('labels_assigned as l') ->join('labels', 'l.label_id', '=', 'labels.id') ->where('l.service_id', '=', $service_id) - ->get(['labels.label']); + ->get(['labels.id', 'labels.label']); }); } diff --git a/app/Models/Shared.php b/app/Models/Shared.php index b0805a0..d3dac0a 100644 --- a/app/Models/Shared.php +++ b/app/Models/Shared.php @@ -4,6 +4,8 @@ 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 Shared extends Model { @@ -14,4 +16,39 @@ class Shared extends Model protected $fillable = ['id', 'active', 'main_domain', 'has_dedicated_ip', 'ip', 'shared_type', 'provider_id', 'location_id', 'bandwidth', 'disk', 'disk_type', 'disk_as_gb', 'domains_limit', 'subdomains_limit', 'ftp_limit', 'email_limit', 'db_limit', 'was_promo', 'owned_since']; public $incrementing = false; + + + public static function sharedRelatedCacheForget(): void + { + Cache::forget('services_count');//Main page services_count cache + Cache::forget('due_soon');//Main page due_soon cache + Cache::forget('recently_added');//Main page recently_added cache + } + + 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 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 sharedEditDataPage(string $shared_id) + { + return DB::table('shared_hosting as s') + ->join('pricings as p', 's.id', '=', 'p.service_id') + ->where('s.id', '=', $shared_id) + ->get(['s.*', 'p.*']); + } }