diff --git a/backend/app/Http/Controllers/CategoriasController.php b/backend/app/Http/Controllers/CategoriasController.php new file mode 100644 index 0000000..463d18c --- /dev/null +++ b/backend/app/Http/Controllers/CategoriasController.php @@ -0,0 +1,125 @@ +validOrFail($restaurante_id); + $restaurante = Restaurante::findOrFail($restaurante_id); + + $categorias = $restaurante->categorias(); + + $paginate = app(PaginatorService::class)->paginate( + perPage: $request->input('per_page', 15), + page: $request->input('page', 1), + total: $categorias->count(), + route: 'restaurant.all', + data: ['restaurante_id' => $restaurante_id] + ); + + $data = $categorias->get() + ->skip($paginate['from'] - 1) + ->take($paginate['per_page']) + ->all(); + + return response()->json([ + 'pagination' => $paginate, + 'data' => $data + ]); + } + + /** + * Obtiene una categoria por su id + */ + public function get(Request $request, $restaurante_id, $id) { + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($id); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $categoria = Categoria::findOrFail($id); + + if($categoria->restaurante != $restaurante) { + throw new ModelNotFoundException("categoria", $id); + } + + return response()->json($categoria); + } + + /** + * Crea una nueva categoria + */ + public function create(Request $request, $restaurante_id) { + app(UuidService::class)->validOrFail($restaurante_id); + $restaurante = Restaurante::findOrFail($restaurante_id); + + $this->validate($request, [ + 'nombre' => 'required' + ]); + + $categoria = Categoria::create([ + 'id' => Uuid::uuid4(), + 'nombre' => $request->input('nombre'), + 'restaurante_id' => $restaurante->id + ]); + + return response()->json($categoria, 201); + } + + /** + * Actualiza un categoria + */ + public function update(Request $request, $restaurante_id, $id) { + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($id); + + $this->validate($request, [ + 'nombre' => 'required' + ]); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $categoria = Categoria::findOrFail($id); + + if($categoria->restaurante != $restaurante) { + throw new ModelNotFoundException("categoria", $id); + } + + $categoria->nombre = $request->input('nombre'); + $categoria->save(); + + return response()->json($categoria); + } + + /** + * Elimina una categoria + */ + public function delete(Request $request, $restaurante_id, $id) { + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($id); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $categoria = Categoria::findOrFail($id); + + if($categoria->restaurante != $restaurante) { + throw new ModelNotFoundException("categoria", $id); + } + + $categoria->delete(); + + return response()->json([], 204); + } +} diff --git a/backend/app/Http/Controllers/RestaurantesController.php b/backend/app/Http/Controllers/RestaurantesController.php index a7e279d..43d71bb 100644 --- a/backend/app/Http/Controllers/RestaurantesController.php +++ b/backend/app/Http/Controllers/RestaurantesController.php @@ -98,6 +98,7 @@ class RestaurantesController extends Controller { if($restaurant->canalesVenta()->count() > 0) throw new CantDeleteHasChildException("restaurant", "canal_venta"); if($restaurant->sectores()->count() > 0) throw new CantDeleteHasChildException("restaurant", "sector"); if($restaurant->zonasProduccion()->count() > 0) throw new CantDeleteHasChildException("restaurant", "zona_produccion"); + if($restaurant->categorias()->count() > 0) throw new CantDeleteHasChildException("restaurant", "categoria"); $restaurant->delete(); diff --git a/backend/app/Http/Controllers/ZonasProduccionController.php b/backend/app/Http/Controllers/ZonasProduccionController.php index 8311d23..ff6ca53 100644 --- a/backend/app/Http/Controllers/ZonasProduccionController.php +++ b/backend/app/Http/Controllers/ZonasProduccionController.php @@ -81,7 +81,7 @@ class ZonasProduccionController extends Controller { } /** - * Actualiza un zona de produccion + * Actualiza una zona de produccion */ public function update(Request $request, $restaurante_id, $id) { app(UuidService::class)->validOrFail($restaurante_id); @@ -95,7 +95,7 @@ class ZonasProduccionController extends Controller { $zonaProduccion = ZonaProduccion::findOrFail($id); if($zonaProduccion->restaurante != $restaurante) { - throw new ModelNotFoundException("sector", $id); + throw new ModelNotFoundException("categoria", $id); } $zonaProduccion->nombre = $request->input('nombre'); @@ -105,7 +105,7 @@ class ZonasProduccionController extends Controller { } /** - * Elimina un sector + * Elimina una zona de produccion */ public function delete(Request $request, $restaurante_id, $id) { app(UuidService::class)->validOrFail($restaurante_id); diff --git a/backend/app/Models/Categoria.php b/backend/app/Models/Categoria.php index 71399d4..eeced5c 100644 --- a/backend/app/Models/Categoria.php +++ b/backend/app/Models/Categoria.php @@ -10,6 +10,14 @@ class Categoria extends Model { use UuidPrimaryKey, SoftDeletes; protected $table = 'categorias'; + protected $fillable = ['id', 'nombre', 'restaurante_id']; + + public static function findOrFail($id) { + $categoria = Categoria::find($id); + if(!$categoria) throw new ModelNotFoundException("categoria", $id); + return $categoria; + } + public function restaurante() { return $this->belongsTo(Restaurante::class); diff --git a/backend/app/Models/Restaurante.php b/backend/app/Models/Restaurante.php index 43fa492..adc1c62 100644 --- a/backend/app/Models/Restaurante.php +++ b/backend/app/Models/Restaurante.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Models\CanalVenta; use App\Models\Sector; use App\Models\ZonaProduccion; +use App\Models\Categoria; use App\Traits\UuidPrimaryKey; use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; @@ -40,4 +41,8 @@ class Restaurante extends Model { public function zonasProduccion() { return $this->hasMany(ZonaProduccion::class, 'restaurante_id'); } + + public function categorias() { + return $this->hasMany(Categoria::class, 'restaurante_id'); + } } diff --git a/backend/routes/web.php b/backend/routes/web.php index 7fc0311..a7ed8da 100644 --- a/backend/routes/web.php +++ b/backend/routes/web.php @@ -2,9 +2,22 @@ use Laravel\Lumen\Routing\Router; -function registerRestaurantApi($router) { +$router->get('/', function () use ($router) { + return 'Public View'; +}); + +$router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']], function () use ($router) { + $router->group(['prefix' => '/users'], function () use ($router) { + $router->get( '/', ['as' => 'users.all', 'uses' => 'UsuariosController@all']); + $router->get( '/{id}', ['as' => 'users.get', 'uses' => 'UsuariosController@get']); + $router->post( '/', ['as' => 'users.create', 'uses' => 'UsuariosController@create']); + $router->put( '/{id}', ['as' => 'users.update', 'uses' => 'UsuariosController@update']); + $router->delete('/{id}', ['as' => 'users.delete', 'uses' => 'UsuariosController@delete']); + $router->put( '/{id}/restaurantes/{restaurant}', ['as' => 'users.add_to_restaurant', 'uses' => 'UsuariosController@addToRestaurant']); + $router->delete('/{id}/restaurantes/{restaurant}', ['as' => 'users.remove_from_restaurant', 'uses' => 'UsuariosController@removeFromRestaurant']); + }); + $router->group(['prefix' => '/restaurantes'], function () use ($router) { - // Rutas del recurso $router->get( '/', ['as' => 'restaurant.all', 'uses' => 'RestaurantesController@all']); $router->get( '/{id}', ['as' => 'restaurant.get', 'uses' => 'RestaurantesController@get']); $router->post( '/', ['as' => 'restaurant.create', 'uses' => 'RestaurantesController@create']); @@ -28,31 +41,11 @@ function registerRestaurantApi($router) { $router->post( '/{restaurante_id}/zonas-produccion', ['as' => 'zonas-produccion.create', 'uses' => 'ZonasProduccionController@create']); $router->put( '/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.update', 'uses' => 'ZonasProduccionController@update']); $router->delete('/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.delete', 'uses' => 'ZonasProduccionController@delete']); + + $router->get( '/{restaurante_id}/categorias', ['as' => 'categorias.all', 'uses' => 'CategoriasController@all']); + $router->get( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.get', 'uses' => 'CategoriasController@get']); + $router->post( '/{restaurante_id}/categorias', ['as' => 'categorias.create', 'uses' => 'CategoriasController@create']); + $router->put( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.update', 'uses' => 'CategoriasController@update']); + $router->delete('/{restaurante_id}/categorias/{id}', ['as' => 'categorias.delete', 'uses' => 'CategoriasController@delete']); }); -} - -function registerUserApi($router) { - $router->group(['prefix' => '/users'], function () use ($router) { - // Rutas del recurso - $router->get( '/', ['as' => 'users.all', 'uses' => 'UsuariosController@all']); - $router->get( '/{id}', ['as' => 'users.get', 'uses' => 'UsuariosController@get']); - $router->post( '/', ['as' => 'users.create', 'uses' => 'UsuariosController@create']); - $router->put( '/{id}', ['as' => 'users.update', 'uses' => 'UsuariosController@update']); - $router->delete('/{id}', ['as' => 'users.delete', 'uses' => 'UsuariosController@delete']); - - // Rutas de acciones - $router->put( '/{id}/restaurantes/{restaurant}', ['as' => 'users.add_to_restaurant', 'uses' => 'UsuariosController@addToRestaurant']); - $router->delete('/{id}/restaurantes/{restaurant}', ['as' => 'users.remove_from_restaurant', 'uses' => 'UsuariosController@removeFromRestaurant']); - }); -} - -/** @var Router $router */ - -$router->get('/', function () use ($router) { - return 'Public View'; -}); - -$router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']], function () use ($router) { - registerUserApi($router); - registerRestaurantApi($router); }); diff --git a/database/modelo.vpp b/database/modelo.vpp index 2e5eab4..c7f1752 100644 Binary files a/database/modelo.vpp and b/database/modelo.vpp differ