Removi las validaciones por ahora :/
This commit is contained in:
@@ -4,6 +4,9 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Restaurante;
|
||||
use App\Services\PaginatorService;
|
||||
use App\Services\UuidService;
|
||||
use App\Exceptions\GenericException;
|
||||
use App\Exceptions\ModelNotFoundException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -13,43 +16,37 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* Obtiene de forma paginada los restaurantes registrados en el backend
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function all(Request $request) {
|
||||
$restaurantes = Restaurante::all();
|
||||
|
||||
$paginate = app(PaginatorService::class)->paginate(
|
||||
perPage: $request->input('per_page', 15),
|
||||
page: $request->input('page', 1),
|
||||
total: Restaurante::all()->count(),
|
||||
total: $restaurantes->count(),
|
||||
route: 'restaurant.all',
|
||||
);
|
||||
|
||||
$data = $restaurantes
|
||||
->skip($paginate['from'] - 1)
|
||||
->take($paginate['per_page'])
|
||||
->all();
|
||||
|
||||
return response()->json([
|
||||
'pagination' => $paginate,
|
||||
'data' => array_values(Restaurante::all()->skip($paginate['from'] - 1)->take($paginate['per_page'])->all())
|
||||
'data' => $data
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene un restaurante por su id
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function get($id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
public function get(Request $request, $id) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
$restaurante = Restaurante::findOrFail($id);
|
||||
|
||||
$restaurante = Restaurante::findOrNull($id);
|
||||
|
||||
if (!$restaurante) {
|
||||
return response()->json([
|
||||
'error' => 'restaurant_not_found',
|
||||
'message' => 'El restaurant con id ' . $id . ' no existe'
|
||||
], 404);
|
||||
if(!$request->user->isOnRestaurant($restaurante)){
|
||||
return ModelNotFoundException('restaurante', $restaurante->id);
|
||||
}
|
||||
|
||||
return response()->json($restaurante);
|
||||
@@ -57,22 +54,12 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* 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')
|
||||
@@ -83,38 +70,15 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* Actualiza un restaurante
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function update(Request $request, $id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
app(UuidService::class)->validOrFail($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 = Restaurante::findOrFail($id);
|
||||
$restaurant->nombre = $request->input('nombre');
|
||||
$restaurant->save();
|
||||
|
||||
@@ -123,36 +87,12 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* Elimina un restaurante
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function delete(Request $request, $id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
|
||||
$restaurant = Restaurante::findOrFail($id);
|
||||
$restaurant->delete();
|
||||
|
||||
return response()->json($restaurant);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user