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.
This commit is contained in:
ro 2024-02-20 12:57:33 -06:00
parent f96707f256
commit 682360c140
4 changed files with 59 additions and 31 deletions

View file

@ -5,18 +5,22 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Repositories\LocationRepository; use App\Repositories\LocationRepository;
use App\Repositories\SourceRepository; use App\Repositories\SourceRepository;
use App\Services\PaginationService;
class FrontIndexController extends Controller class FrontIndexController extends Controller
{ {
protected $location; protected $location;
protected $source; protected $source;
protected $pagination;
public function __construct( public function __construct(
LocationRepository $locationRepository, LocationRepository $locationRepository,
SourceRepository $sourceRepository SourceRepository $sourceRepository,
PaginationService $paginationService
) { ) {
$this->location = $locationRepository; $this->location = $locationRepository;
$this->source = $sourceRepository; $this->source = $sourceRepository;
$this->pagination = $paginationService;
} }
public function start() public function start()
@ -79,16 +83,16 @@ class FrontIndexController extends Controller
public function listings(int $pageNum = 1) public function listings(int $pageNum = 1)
{ {
$listing = $this->location->getPage($pageNum); $page = $this->pagination->getPage($pageNum);
return view('front.listing', [ return view('front.listing', [
'title' => "Listings", 'title' => "Listings",
'sources' => count($this->source->getActive()), 'sources' => count($this->source->getActive()),
"totalPages" => $listing[1], "totalPages" => $page['pageCount'],
"prev" => $listing[2], "prev" => $page['prev'],
"next" => $listing[3], "next" => $page['next'],
'pageNum' => $pageNum, 'pageNum' => $pageNum,
'locations' => $listing[0] 'locations' => $page['locations']
]); ]);
} }
} }

View file

@ -35,6 +35,10 @@ class AppServiceProvider extends ServiceProvider
$this->app->bind(MaintenanceService::class, function ($app) { $this->app->bind(MaintenanceService::class, function ($app) {
return new MaintenanceService(new Location()); return new MaintenanceService(new Location());
}); });
$this->app->bind(PaginationService::class, function ($app) {
return new PaginationService(new Location());
});
} }
/** /**

View file

@ -9,7 +9,6 @@ use Illuminate\Support\Facades\Auth;
class LocationRepository class LocationRepository
{ {
protected $model; protected $model;
private $limit = 15;
public function __construct(Location $model) public function __construct(Location $model)
{ {
@ -54,28 +53,6 @@ class LocationRepository
->orderByDesc('updated_at')->limit(10)->get(); ->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) public function addLocation(Request $request)
{ {
$fields = $request->validate([ $fields = $request->validate([

View file

@ -0,0 +1,43 @@
<?php
namespace App\Services;
use App\Models\Location;
class PaginationService
{
protected $location;
protected $limit = 15;
public function __construct(
Location $location,
) {
$this->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];
}
}