From f7d3aa68b9f0778ba3bf3e211307ca6cc8f61d4c Mon Sep 17 00:00:00 2001 From: Artur Nowakowski Date: Thu, 5 Dec 2019 10:25:36 +0100 Subject: [PATCH] [CLEAR-29] Retrieving list of products --- api/app/Http/Controllers/ProductsController.php | 9 +++++++++ api/app/Repositories/ProductsRepository.php | 5 +++++ api/app/Repositories/ProductsRepositoryInterface.php | 1 + api/routes/api.php | 5 +++-- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api/app/Http/Controllers/ProductsController.php b/api/app/Http/Controllers/ProductsController.php index 625083f..61237ce 100644 --- a/api/app/Http/Controllers/ProductsController.php +++ b/api/app/Http/Controllers/ProductsController.php @@ -4,10 +4,12 @@ namespace App\Http\Controllers; use App\Http\Requests\AddProductRequest; use App\Repositories\ProductsRepositoryInterface; +use Illuminate\Http\Request; class ProductsController extends Controller { private ProductsRepositoryInterface $productsRepository; + private int $paginationSize = 10; public function __construct(ProductsRepositoryInterface $productsRepository) { @@ -20,4 +22,11 @@ class ProductsController extends Controller return response()->json(['success' => true, 'data' => ['product' => $product]], 200); } + + public function index(Request $request) + { + $products = $this->productsRepository->paginate($this->paginationSize); + + return response()->json(['success' => true, 'data' => ['products' => $products]], 200); + } } diff --git a/api/app/Repositories/ProductsRepository.php b/api/app/Repositories/ProductsRepository.php index a29a715..4f0e5ec 100644 --- a/api/app/Repositories/ProductsRepository.php +++ b/api/app/Repositories/ProductsRepository.php @@ -12,4 +12,9 @@ class ProductsRepository implements ProductsRepositoryInterface { return Product::create($data); } + + public function paginate($chunkSize) + { + return Product::paginate($chunkSize); + } } diff --git a/api/app/Repositories/ProductsRepositoryInterface.php b/api/app/Repositories/ProductsRepositoryInterface.php index 61bc3d2..29b1361 100644 --- a/api/app/Repositories/ProductsRepositoryInterface.php +++ b/api/app/Repositories/ProductsRepositoryInterface.php @@ -6,4 +6,5 @@ namespace App\Repositories; interface ProductsRepositoryInterface { public function create($data); + public function paginate($chunkSize); } diff --git a/api/routes/api.php b/api/routes/api.php index 711434c..6be8601 100644 --- a/api/routes/api.php +++ b/api/routes/api.php @@ -8,6 +8,7 @@ Route::group(['prefix' => 'user', 'middleware' => ['assign.guard:users']], funct Route::get('/me', 'UsersController@me'); }); -Route::group(['prefix' => 'product', 'middleware' => ['assign.guard:users']], function () { - Route::post('/', 'ProductsController@add'); +Route::prefix('product')->group(function () { + Route::post('/', 'ProductsController@add')->middleware(['assign.guard:users']); + Route::get('/', 'ProductsController@index'); });