Updated Shared controller

Moved DB calls to model
Used existing DB call functions
This commit is contained in:
cp6 2022-05-15 12:01:48 +10:00
parent 70622f827b
commit a054e83d1e
3 changed files with 58 additions and 53 deletions

View File

@ -4,9 +4,7 @@ namespace App\Http\Controllers;
use App\Models\IPs; use App\Models\IPs;
use App\Models\Labels; use App\Models\Labels;
use App\Models\Locations;
use App\Models\Pricing; use App\Models\Pricing;
use App\Models\Providers;
use App\Models\Shared; use App\Models\Shared;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
@ -17,20 +15,14 @@ class SharedController extends Controller
{ {
public function index() public function index()
{ {
$shared = DB::table('shared_hosting as s') $shared = Shared::sharedDataIndexPage();
->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']);
return view('shared.index', compact(['shared'])); return view('shared.index', compact(['shared']));
} }
public function create() public function create()
{ {
$Providers = Providers::allProviders(); return view('shared.create');
$Locations = Locations::allLocations();
return view('shared.create', compact(['Providers', 'Locations']));
} }
public function store(Request $request) public function store(Request $request)
@ -95,9 +87,7 @@ class SharedController extends Controller
'db__limit' => $request->db 'db__limit' => $request->db
]); ]);
Cache::forget('services_count');//Main page services_count cache Shared::sharedRelatedCacheForget();
Cache::forget('due_soon');//Main page due_soon cache
Cache::forget('recently_added');//Main page recently_added cache
return redirect()->route('shared.index') return redirect()->route('shared.index')
->with('success', 'Shared hosting created Successfully.'); ->with('success', 'Shared hosting created Successfully.');
@ -105,44 +95,24 @@ class SharedController extends Controller
public function show(Shared $shared) public function show(Shared $shared)
{ {
$shared_extras = DB::table('shared_hosting as s') $shared_extras = Shared::sharedDataShowPage($shared->id);
->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.*']);
$labels = DB::table('labels_assigned as l') $labels = Labels::labelsForService($shared->id);
->join('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $shared->id)
->get(['labels.label']);
$ip_address = DB::table('ips as i') $ip_address = IPs::ipsForServer($shared->id);
->where('i.service_id', '=', $shared->id)
->get();
return view('shared.show', compact(['shared', 'shared_extras', 'labels', 'ip_address'])); return view('shared.show', compact(['shared', 'shared_extras', 'labels', 'ip_address']));
} }
public function edit(Shared $shared) public function edit(Shared $shared)
{ {
$locations = DB::table('locations')->get(['*']); $labels = Labels::labelsForService($shared->id);
$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']);
$ip_address = json_decode(DB::table('ips as i') $ip_address = IPs::ipsForServer($shared->id);
->where('i.service_id', '=', $shared->id)
->get(), true);
$shared = DB::table('shared_hosting as s') $shared = Shared::sharedEditDataPage($shared->id);
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $shared->id)
->get(['s.*', 'p.*']);
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) 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); Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $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('services_count');//Main page services_count cache Shared::sharedRelatedCacheForget();
Cache::forget('due_soon');//Main page due_soon cache
Cache::forget('recently_added');//Main page recently_added cache
return redirect()->route('shared.index') return redirect()->route('shared.index')
->with('success', 'Shared hosting updated Successfully.'); ->with('success', 'Shared hosting updated Successfully.');
@ -215,21 +185,19 @@ class SharedController extends Controller
public function destroy(Shared $shared) public function destroy(Shared $shared)
{ {
$id = $shared->id; $shared_id = $shared->id;
$items = Shared::find($id); $items = Shared::find($shared_id);
$items->delete(); $items->delete();
$p = new Pricing(); $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 Shared::sharedRelatedCacheForget();
Cache::forget('due_soon');//Main page due_soon cache
Cache::forget('recently_added');//Main page recently_added cache
return redirect()->route('shared.index') return redirect()->route('shared.index')
->with('success', 'Shared hosting was deleted Successfully.'); ->with('success', 'Shared hosting was deleted Successfully.');

View File

@ -43,11 +43,11 @@ class Labels extends Model
public static function labelsForService(string $service_id) 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') return DB::table('labels_assigned as l')
->join('labels', 'l.label_id', '=', 'labels.id') ->join('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $service_id) ->where('l.service_id', '=', $service_id)
->get(['labels.label']); ->get(['labels.id', 'labels.label']);
}); });
} }

View File

@ -4,6 +4,8 @@ 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;
class Shared extends Model 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']; 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 $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.*']);
}
} }