Add pagination to GET /insurances

This commit is contained in:
Djery-Tom 2022-02-17 15:13:52 +01:00
parent a0858d891d
commit 6af9b094e7
1 changed files with 40 additions and 2 deletions

View File

@ -141,6 +141,36 @@ class InsuranceController extends Controller
* enum={"ALL","EDITABLE","STOPPED"} * enum={"ALL","EDITABLE","STOPPED"}
* ) * )
* ), * ),
* @OA\Parameter(
* parameter="page",
* name="page",
* description="Page",
* in="query",
* required=false,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* parameter="perPage",
* name="perPage",
* description="Pas de pagination",
* in="query",
* required=false,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* parameter="pagination",
* name="pagination",
* description="pagination",
* in="query",
* required=false,
* @OA\Schema(
* type="boolean",
* )
* ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="OK", * description="OK",
@ -167,10 +197,12 @@ class InsuranceController extends Controller
$this->validate($request, [ $this->validate($request, [
'user_id' => 'required|integer|exists:users,id', 'user_id' => 'required|integer|exists:users,id',
'type' => 'nullable|in:ALL,EDITABLE,STOPPED', 'type' => 'nullable|in:ALL,EDITABLE,STOPPED',
'pagination' => 'nullable|boolean'
]); ]);
$userId = $request->input('user_id'); $userId = $request->input('user_id');
$type = $request->input('type'); $type = $request->input('type');
$pagination = $request->input('pagination');
$query = NhInsurance::with(['network:id,name', 'beneficiaries'])->where('user_id', $userId); $query = NhInsurance::with(['network:id,name', 'beneficiaries'])->where('user_id', $userId);
@ -187,9 +219,15 @@ class InsuranceController extends Controller
} }
} }
$insurances = $query->orderBy('created_at', 'DESC')->get(); $query = $query->orderBy('created_at', 'DESC');
if ($pagination) {
$insurances = $query->paginate($request->input('perPage', 10));
} else {
$insurances = $query->get();
}
foreach ($insurances as $insurance) { $array = $pagination ? $insurances->items() : $insurances;
foreach ($array as $insurance) {
$insurance->state = trans('states.' . $insurance->state); $insurance->state = trans('states.' . $insurance->state);
if ($type == 'EDITABLE') { if ($type == 'EDITABLE') {