138 lines
3.9 KiB
PHP
138 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Restaurante;
|
|
use App\Services\PaginatorService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
class RestaurantesController extends Controller {
|
|
|
|
/**
|
|
* Obtiene de forma paginada los restaurantes registrados en el backend
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function all(Request $request) {
|
|
$paginate = app(PaginatorService::class)->paginate(
|
|
perPage: $request->input('per_page', 15),
|
|
page: $request->input('page', 1),
|
|
total: Restaurante::all()->count(),
|
|
route: 'restaurant.all',
|
|
);
|
|
|
|
return response()->json([
|
|
'pagination' => $paginate,
|
|
'data' => array_values(Restaurante::all()->skip($paginate['from'] - 1)->take($paginate['per_page'])->all())
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Obtiene un restaurante por su id
|
|
* @param $id
|
|
* @return JsonResponse
|
|
*/
|
|
public function get($id) {
|
|
$restaurante = Restaurante::findOrNull($id);
|
|
|
|
if (!$restaurante) {
|
|
return response()->json([
|
|
'error' => 'restaurant_not_found',
|
|
'message' => 'El restaurant con id ' . $id . ' no existe'
|
|
], 404);
|
|
}
|
|
|
|
return response()->json($restaurante);
|
|
}
|
|
|
|
/**
|
|
* Crea un nuevo restaurant
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function create(Request $request) {
|
|
$this->validate($request, [
|
|
'nombre' => 'required'
|
|
]);
|
|
|
|
if (!$request->user->canManageRestaurants()) {
|
|
return response()->json([
|
|
'error' => 'cant_manage_restaurants',
|
|
'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
], 403);
|
|
}
|
|
|
|
$restaurant = Restaurante::create([
|
|
'id' => Uuid::uuid4(),
|
|
'nombre' => $request->input('nombre')
|
|
]);
|
|
|
|
return response()->json($restaurant, 201);
|
|
}
|
|
|
|
/**
|
|
* Actualiza un restaurante
|
|
* @param Request $request
|
|
* @param $id
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function update(Request $request, $id) {
|
|
$this->validate($request, [
|
|
'nombre' => 'required'
|
|
]);
|
|
|
|
if (!$request->user->canManageRestaurants()) {
|
|
return response()->json([
|
|
'error' => 'cant_manage_restaurants',
|
|
'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
], 403);
|
|
}
|
|
|
|
$restaurant = Restaurante::findOrNull($id);
|
|
if(!$restaurant) {
|
|
return response()->json([
|
|
'error' => 'not_found',
|
|
'message' => 'El restaurante con id ' . $id . ' no existe'
|
|
], 404);
|
|
}
|
|
|
|
$restaurant->nombre = $request->input('nombre');
|
|
$restaurant->save();
|
|
|
|
return response()->json($restaurant);
|
|
}
|
|
|
|
/**
|
|
* Elimina un restaurante
|
|
* @param Request $request
|
|
* @param $id
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function delete(Request $request, $id) {
|
|
if (!$request->user->canManageRestaurants()) {
|
|
return response()->json([
|
|
'error' => 'cant_manage_restaurants',
|
|
'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
], 403);
|
|
}
|
|
|
|
$restaurant = Restaurante::findOrNull($id);
|
|
if(!$restaurant) {
|
|
return response()->json([
|
|
'error' => 'not_found',
|
|
'message' => 'El restaurante con id ' . $id . ' no existe'
|
|
], 404);
|
|
}
|
|
|
|
$restaurant->delete();
|
|
|
|
return response()->json($restaurant);
|
|
}
|
|
}
|