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

View file

@ -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());
});
}
/**

View file

@ -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([

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];
}
}