filtering pagination

This commit is contained in:
Artur Nowakowski 2019-12-15 21:09:14 +01:00
parent 911434a2c8
commit 43d9bf1ba4
7 changed files with 32 additions and 10 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Validation\ValidatesRequests;
@ -10,4 +11,20 @@ use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController class Controller extends BaseController
{ {
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
private $paginationSize = 10;
protected function getFilters(Request $request){
$filters = ['limit' => $this->paginationSize];
if($request->has('limit')){
$filters['limit'] = $request->query('limit');
}
if($request->has('search')){
$filters['search'] = $request->query('search');
}
return $filters;
}
} }

View File

@ -9,7 +9,6 @@ use Illuminate\Http\Request;
class ProductsController extends Controller class ProductsController extends Controller
{ {
private $productsRepository; private $productsRepository;
private $paginationSize = 10;
public function __construct(ProductsRepositoryInterface $productsRepository) public function __construct(ProductsRepositoryInterface $productsRepository)
{ {
@ -25,7 +24,8 @@ class ProductsController extends Controller
public function index(Request $request) public function index(Request $request)
{ {
$products = $this->productsRepository->paginate($this->paginationSize); $filters = $this->getFilters($request);
$products = $this->productsRepository->paginate($filters);
return response()->json(['success' => true, 'data' => ['products' => $products]], 200); return response()->json(['success' => true, 'data' => ['products' => $products]], 200);
} }

View File

@ -9,7 +9,6 @@ use Illuminate\Http\Request;
class RecipesController extends Controller class RecipesController extends Controller
{ {
private $recipesRepository; private $recipesRepository;
private $paginationSize = 10;
public function __construct(RecipesRepositoryInterface $recipesRepository) public function __construct(RecipesRepositoryInterface $recipesRepository)
{ {
@ -25,7 +24,8 @@ class RecipesController extends Controller
public function index(Request $request) public function index(Request $request)
{ {
$recipes = $this->recipesRepository->paginate($this->paginationSize); $filters = $this->getFilters($request);
$recipes = $this->recipesRepository->paginate($filters);
return response()->json(['success' => true, 'data' => ['recipes' => $recipes]], 200); return response()->json(['success' => true, 'data' => ['recipes' => $recipes]], 200);
} }

View File

@ -13,8 +13,13 @@ class ProductsRepository implements ProductsRepositoryInterface
return Product::create($data); return Product::create($data);
} }
public function paginate($chunkSize) public function paginate(array $filters)
{ {
return Product::paginate($chunkSize); $query = Product::query();
if(isset($filters['search'])){
$query->where('name', 'like', "${filters['search']}%");
}
return $query->paginate($filters['limit']);
} }
} }

View File

@ -6,5 +6,5 @@ namespace App\Repositories;
interface ProductsRepositoryInterface interface ProductsRepositoryInterface
{ {
public function create($data); public function create($data);
public function paginate($chunkSize); public function paginate(array $filters);
} }

View File

@ -58,8 +58,8 @@ class RecipesRepository implements RecipesRepositoryInterface
} }
public function paginate($chunkSize) public function paginate(array $filters)
{ {
return Recipe::paginate($chunkSize); return Recipe::paginate($filters['limit']);
} }
} }

View File

@ -8,5 +8,5 @@ use Illuminate\Support\Collection;
interface RecipesRepositoryInterface interface RecipesRepositoryInterface
{ {
public function create(Collection $data); public function create(Collection $data);
public function paginate(int $chunkSize); public function paginate(array $filters);
} }