Updated Reseller controller

Cleaned up, Moved DB calls to model
This commit is contained in:
cp6 2022-05-15 16:49:54 +10:00
parent 6d1cbef740
commit f5cc42c0fe
2 changed files with 52 additions and 43 deletions

View File

@ -17,11 +17,7 @@ class ResellerController extends Controller
{ {
public function index() public function index()
{ {
$resellers = DB::table('reseller_hosting as s') $resellers = Reseller::resellerDataIndexPage();
->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('reseller.index', compact(['resellers'])); return view('reseller.index', compact(['resellers']));
} }
@ -94,9 +90,7 @@ class ResellerController extends Controller
'db_limit' => $request->db 'db_limit' => $request->db
]); ]);
Cache::forget('services_count');//Main page services_count cache Reseller::resllerRelatedCacheForget();
Cache::forget('due_soon');//Main page due_soon cache
Cache::forget('recently_added');//Main page recently_added cache
return redirect()->route('reseller.index') return redirect()->route('reseller.index')
->with('success', 'Reseller hosting created Successfully.'); ->with('success', 'Reseller hosting created Successfully.');
@ -105,45 +99,23 @@ class ResellerController extends Controller
public function show(Reseller $reseller) public function show(Reseller $reseller)
{ {
$reseller_extras = DB::table('reseller_hosting as s') $reseller_extras = Reseller::resellerDataShowPage($reseller->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', '=', $reseller->id)
->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*']);
$labels = DB::table('labels_assigned as l') $labels = Labels::labelsForService($reseller->id);
->LeftJoin('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $reseller->id)
->get(['labels.label']);
$ip_address = DB::table('ips as i')
->where('i.service_id', '=', $reseller->id)
->get();
$ip_address = IPs::ipsForServer($reseller->id);
return view('reseller.show', compact(['reseller', 'reseller_extras', 'labels', 'ip_address'])); return view('reseller.show', compact(['reseller', 'reseller_extras', 'labels', 'ip_address']));
} }
public function edit(Reseller $reseller) public function edit(Reseller $reseller)
{ {
$locations = DB::table('locations')->get(['*']); $labels = Labels::labelsForService($reseller->id);
$providers = json_decode(DB::table('providers')->get(['*']), true);
$labels = DB::table('labels_assigned as l') $ip_address = IPs::ipsForServer($reseller->id);
->join('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $reseller->id)
->get(['labels.id', 'labels.label']);
$ip_address = json_decode(DB::table('ips as i') $reseller = Reseller::resellerDataEditPage($reseller->id);
->where('i.service_id', '=', $reseller->id)
->get(), true);
$reseller = DB::table('reseller_hosting as s') return view('reseller.edit', compact(['reseller', 'ip_address', 'labels']));
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $reseller->id)
->get(['s.*', 'p.*']);
return view('reseller.edit', compact(['reseller', 'locations', 'providers', 'ip_address', 'labels']));
} }
public function update(Request $request, Reseller $reseller) public function update(Request $request, Reseller $reseller)
@ -206,9 +178,9 @@ class ResellerController extends Controller
IPs::insertIP($request->id, $request->dedicated_ip); IPs::insertIP($request->id, $request->dedicated_ip);
} }
Cache::forget('services_count');//Main page services_count cache Cache::forget("labels_for_service.{$request->id}");
Cache::forget('due_soon');//Main page due_soon cache
Cache::forget('recently_added');//Main page recently_added cache Reseller::resllerRelatedCacheForget();
return redirect()->route('reseller.index') return redirect()->route('reseller.index')
->with('success', 'Reseller hosting updated Successfully.'); ->with('success', 'Reseller hosting updated Successfully.');
@ -228,9 +200,7 @@ class ResellerController extends Controller
IPs::deleteIPsAssignedTo($id); IPs::deleteIPsAssignedTo($id);
Cache::forget('services_count');//Main page services_count cache Reseller::resllerRelatedCacheForget();
Cache::forget('due_soon');//Main page due_soon cache
Cache::forget('recently_added');//Main page recently_added cache
return redirect()->route('reseller.index') return redirect()->route('reseller.index')
->with('success', 'Reseller hosting was deleted Successfully.'); ->with('success', 'Reseller hosting was deleted Successfully.');

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 Reseller extends Model class Reseller extends Model
{ {
@ -14,4 +16,41 @@ class Reseller extends Model
protected $fillable = ['id', 'active', 'accounts', 'main_domain', 'has_dedicated_ip', 'ip', 'reseller_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', 'accounts', 'main_domain', 'has_dedicated_ip', 'ip', 'reseller_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 resllerRelatedCacheForget(): 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 resellerDataIndexPage()
{
return DB::table('reseller_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 resellerDataShowPage(string $reseller_id)
{
return DB::table('reseller_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', '=', $reseller_id)
->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*']);
}
public static function resellerDataEditPage(string $reseller_id)
{
return DB::table('reseller_hosting as s')
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $reseller_id)
->get(['s.*', 'p.*']);
}
} }