From 682360c140965a7fec249c5dc18fa44dcc2656ab Mon Sep 17 00:00:00 2001 From: ro Date: Tue, 20 Feb 2024 12:57:33 -0600 Subject: [PATCH] Added Pagination Service class Pagination is going to need some additional features in the near future, so it made sense to give it it's own seperate class. --- app/Http/Controllers/FrontIndexController.php | 20 +++++---- app/Providers/AppServiceProvider.php | 4 ++ app/Repositories/LocationRepository.php | 23 ---------- app/Services/PaginationService.php | 43 +++++++++++++++++++ 4 files changed, 59 insertions(+), 31 deletions(-) create mode 100644 app/Services/PaginationService.php diff --git a/app/Http/Controllers/FrontIndexController.php b/app/Http/Controllers/FrontIndexController.php index 96723e5..55cb514 100644 --- a/app/Http/Controllers/FrontIndexController.php +++ b/app/Http/Controllers/FrontIndexController.php @@ -5,18 +5,22 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Repositories\LocationRepository; use App\Repositories\SourceRepository; +use App\Services\PaginationService; class FrontIndexController extends Controller { protected $location; protected $source; + protected $pagination; public function __construct( LocationRepository $locationRepository, - SourceRepository $sourceRepository + SourceRepository $sourceRepository, + PaginationService $paginationService ) { - $this->location = $locationRepository; - $this->source = $sourceRepository; + $this->location = $locationRepository; + $this->source = $sourceRepository; + $this->pagination = $paginationService; } public function start() @@ -79,16 +83,16 @@ class FrontIndexController extends Controller public function listings(int $pageNum = 1) { - $listing = $this->location->getPage($pageNum); + $page = $this->pagination->getPage($pageNum); return view('front.listing', [ 'title' => "Listings", 'sources' => count($this->source->getActive()), - "totalPages" => $listing[1], - "prev" => $listing[2], - "next" => $listing[3], + "totalPages" => $page['pageCount'], + "prev" => $page['prev'], + "next" => $page['next'], 'pageNum' => $pageNum, - 'locations' => $listing[0] + 'locations' => $page['locations'] ]); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index c3940bf..f639e0e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -35,6 +35,10 @@ class AppServiceProvider extends ServiceProvider $this->app->bind(MaintenanceService::class, function ($app) { return new MaintenanceService(new Location()); }); + + $this->app->bind(PaginationService::class, function ($app) { + return new PaginationService(new Location()); + }); } /** diff --git a/app/Repositories/LocationRepository.php b/app/Repositories/LocationRepository.php index 8545691..09d785a 100644 --- a/app/Repositories/LocationRepository.php +++ b/app/Repositories/LocationRepository.php @@ -9,7 +9,6 @@ use Illuminate\Support\Facades\Auth; class LocationRepository { protected $model; - private $limit = 15; public function __construct(Location $model) { @@ -54,28 +53,6 @@ class LocationRepository ->orderByDesc('updated_at')->limit(10)->get(); } - public function getPage($pageNum) - { - $range = $pageNum * $this->limit - $this->limit; - $active = $this->model::where("active", true)->where('actions_count', '>=', 2)->get(); - $locations = $this->model::where("active", true)->where('actions_count', '>=', 2) - ->limit($this->limit)->offset($range)->orderBy('id', 'asc')->get(); - $pageCount = ceil(count($active) / $this->limit); - - $next = $pageNum + 1; - if ($next > $pageCount) { - $next = 1; - } - - $prev = $pageNum - 1; - - if ($prev <= 0) { - $prev = $pageCount; - } - - return $result = [$locations, $pageCount, $prev, $next]; - } - public function addLocation(Request $request) { $fields = $request->validate([ diff --git a/app/Services/PaginationService.php b/app/Services/PaginationService.php new file mode 100644 index 0000000..1906e1d --- /dev/null +++ b/app/Services/PaginationService.php @@ -0,0 +1,43 @@ +location = $location; + } + + public function getPage($pageNum) + { + $range = $pageNum * $this->limit - $this->limit; + $active = $this->location::where("active", true)->where('actions_count', '>=', 2)->get(); + $locations = $this->location::where("active", true)->where('actions_count', '>=', 2) + ->limit($this->limit)->offset($range)->orderBy('id', 'asc')->get(); + $pageCount = ceil(count($active) / $this->limit); + + $next = $pageNum + 1; + if ($next > $pageCount) { + $next = 1; + } + + $prev = $pageNum - 1; + + if ($prev <= 0) { + $prev = $pageCount; + } + + return $result = [ + 'locations' => $locations, + 'pageCount' => $pageCount, + 'prev' => $prev, + 'next' => $next]; + } +}