8000 add order paginate · thaivanan/Laravel-API-Server@ebe8a8d · GitHub
[go: up one dir, main page]

Skip to content

Commit ebe8a8d

Browse files
committed
add order paginate
1 parent 6c8d5b0 commit ebe8a8d

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

app/Http/Controllers/JuegoController.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public function index()
7474
* example="8"
7575
* ),
7676
* ),
77+
* @OA\Parameter(
78+
* name="order",
79+
* description="ordenar paginación de juegos (no es obligatorio). Opciones: [nombreAsc, nombrDesc, fechaAsc, fechaDesc]",
80+
* in="query",
81+
* required=false,
82+
* @OA\Schema(
83+
* type="string",
84+
* example=""
85+
* ),
86+
* ),
7787
* @OA\Response(response=200, description="Success"),
7888
* @OA\Response(response=401, description="No autorizado"),
7989
* @OA\Response(response=500, description="Error interno del servidor")
@@ -82,7 +92,24 @@ public function index()
8292
*/
8393
public function paginate(Request $request)
8494
{
85-
$juegos = Juego::orderBy('id', 'DESC')->paginate($request->input('items'));
95+
switch($request->input('order')) {
96+
case 'nombreAsc':
97+
$juegos = Juego::orderBy('nombre', 'ASC')->paginate($request->input('items'));
98+
break;
99+
case 'nombreDesc':
100+
$juegos = Juego::orderBy('nombre', 'DESC')->paginate($request->input('items'));
101+
break;
102+
case 'fechaAsc':
103+
$juegos = Juego::orderBy('fecha', 'ASC')->paginate($request->input('items'));
104+
break;
105+
case 'fechaDesc':
106+
$juegos = Juego::orderBy('fecha', 'DESC')->paginate($request->input('items'));
107+
break;
108+
default:
109+
$juegos = Juego::orderBy('id', 'DESC')->paginate($request->input('items'));
110+
break;
111+
}
112+
86113
return response()->json(JuegoResource::collection(($juegos)), 200);
87114
}
88115

app/Http/Resources/JuegoResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function toArray($request)
2121
"generos" => GeneroResource::collection($this->generos),
2222
"fecha" => $this->fecha,
2323
"slug" => $this->slug,
24-
"imagen" => (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . '/media/juegos/' .$this->id. '-' .$this->slug. '.png',
24+
"imagen" => (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . '/media/juegos/' .$this->id. '-' .$this->slug. '.png?t='.time(),
2525
];
2626
}
2727
}

storage/api-docs/api-docs.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@
370370
"type": "integer",
371371
"example": "8"
372372
}
373+
},
374+
{
375+
"name": "order",
376+
"in": "query",
377+
"description": "ordenar paginación de juegos (no es obligatorio). Opciones: [nombreAsc, nombrDesc, fechaAsc, fechaDesc]",
378+
"required": false,
379+
"schema": {
380+
"type": "string",
381+
"example": ""
382+
}
373383
}
374384
],
375385
"responses": {

0 commit comments

Comments
 (0)
0