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\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.');

View File

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

View File

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