API de restaurante

This commit is contained in:
2021-05-02 19:15:35 -04:00
parent b20d95cb81
commit 1632a2e51f
6 changed files with 313 additions and 13 deletions

View File

@@ -0,0 +1,137 @@
<?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);
}
}

View File

@@ -8,7 +8,6 @@ use App\Services\Auth0Service;
use App\Services\PaginatorService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Ramsey\Uuid\Uuid;
@@ -24,7 +23,7 @@ class UsuariosController extends Controller {
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
page: $request->input('page', 1),
count: Usuario::all()->count(),
total: Usuario::all()->count(),
route: 'users.all',
);
@@ -67,7 +66,7 @@ class UsuariosController extends Controller {
'restaurant' => 'required|exists:restaurantes,id',
]);
$restaurant = Restaurante::find($request->input('restaurant'));
$restaurant = Restaurante::findOrNull($request->input('restaurant'));
$cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant);
if ($cantManageUsersOrRestaurant) {
@@ -214,7 +213,7 @@ class UsuariosController extends Controller {
], 404);
}
$restaurant = Restaurante::find($restaurant);
$restaurant = Restaurante::findOrNull($restaurant);
if (!$restaurant) {
return response()->json([
'error' => 'not_found',
@@ -257,7 +256,7 @@ class UsuariosController extends Controller {
], 404);
}
$restaurant = Restaurante::find($restaurant);
$restaurant = Restaurante::findOrNull($restaurant);
if (!$restaurant) {
return response()->json([
'error' => 'not_found',

View File

@@ -5,9 +5,12 @@ namespace App\Models;
use App\Traits\UuidPrimaryKey;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
/**
* @method static find(mixed $restaurant)
* @method static create(array $array)
* @method static find($id)
* @property mixed id
*/
class Restaurante extends Model {
@@ -15,6 +18,17 @@ class Restaurante extends Model {
protected $table = 'restaurantes';
protected $fillable = ['id', 'nombre'];
public static function findOrNull($id) {
try {
return Restaurante::find($id);
} catch (QueryException $ex) {
Log::warning('Se intento obtener un restaurante con un id invalido', ['id' => $id]);
return null;
}
}
public function usuarios() {
return $this->belongsToMany(Usuario::class, 'usuarios_restaurantes', 'restaurante_id', 'usuario_id');
}

View File

@@ -51,6 +51,11 @@ class Usuario extends Model {
}
}
public function canManageRestaurants() {
if (in_array('global_admin', $this->roles)) return true;
return false;
}
/**
* Valida que el usuario tiene permisos sobre otro usuario
*