diff --git a/backend/app/Http/Controllers/IngredientesController.php b/backend/app/Http/Controllers/IngredientesController.php new file mode 100644 index 0000000..d066b47 --- /dev/null +++ b/backend/app/Http/Controllers/IngredientesController.php @@ -0,0 +1,129 @@ +validOrFail($restaurante_id); + $restaurante = Restaurante::findOrFail($restaurante_id); + + $ingredientes = $restaurante->ingredientes(); + + $paginate = app(PaginatorService::class)->paginate( + perPage: $request->input('per_page', 15), + page: $request->input('page', 1), + total: $ingredientes->count(), + route: 'restaurant.all', + data: ['restaurante_id' => $restaurante_id] + ); + + $data = $ingredientes->get() + ->skip($paginate['from'] - 1) + ->take($paginate['per_page']) + ->all(); + + return response()->json([ + 'pagination' => $paginate, + 'data' => $data + ]); + } + + /** + * Obtiene un ingrediente 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); + $ingrediente = Ingrediente::findOrFail($id); + + if($ingrediente->restaurante != $restaurante) { + throw new ModelNotFoundException("ingrediente", $id); + } + + return response()->json($ingrediente); + } + + /** + * Crea un nuevo ingrediente + */ + public function create(Request $request, $restaurante_id) { + app(UuidService::class)->validOrFail($restaurante_id); + $restaurante = Restaurante::findOrFail($restaurante_id); + + $this->validate($request, [ + 'nombre' => 'required', + 'medida' => 'required' + ]); + + $ingrediente = Ingrediente::create([ + 'id' => Uuid::uuid4(), + 'nombre' => $request->input('nombre'), + 'medida' => $request->input('medida'), + 'restaurante_id' => $restaurante->id + ]); + + return response()->json($ingrediente, 201); + } + + /** + * Actualiza un ingrediente + */ + public function update(Request $request, $restaurante_id, $id) { + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($id); + + $this->validate($request, [ + 'nombre' => 'required', + 'medida' => 'required' + ]); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $ingrediente = Ingrediente::findOrFail($id); + + if($ingrediente->restaurante != $restaurante) { + throw new ModelNotFoundException("ingrediente", $id); + } + + $ingrediente->nombre = $request->input('nombre'); + $ingrediente->medida = $request->input('medida'); + $ingrediente->save(); + + return response()->json($ingrediente); + } + + /** + * Elimina un ingrediente + */ + public function delete(Request $request, $restaurante_id, $id) { + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($id); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $ingrediente = Ingrediente::findOrFail($id); + + if($ingrediente->restaurante != $restaurante) { + throw new ModelNotFoundException("ingrediente", $id); + } + + $ingrediente->delete(); + + return response()->json([], 204); + } +} diff --git a/backend/app/Models/Administrador.php b/backend/app/Models/Administrador.php index 5e2d545..cc3a484 100644 --- a/backend/app/Models/Administrador.php +++ b/backend/app/Models/Administrador.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/BoletaElectronica.php b/backend/app/Models/BoletaElectronica.php index 2cf0fb8..dccffcd 100644 --- a/backend/app/Models/BoletaElectronica.php +++ b/backend/app/Models/BoletaElectronica.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/BoletaExenta.php b/backend/app/Models/BoletaExenta.php index 4c2e8f5..5499930 100644 --- a/backend/app/Models/BoletaExenta.php +++ b/backend/app/Models/BoletaExenta.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Caja.php b/backend/app/Models/Caja.php index 62373fa..120ee75 100644 --- a/backend/app/Models/Caja.php +++ b/backend/app/Models/Caja.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Categoria.php b/backend/app/Models/Categoria.php index eeced5c..4af8929 100644 --- a/backend/app/Models/Categoria.php +++ b/backend/app/Models/Categoria.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Compra.php b/backend/app/Models/Compra.php index 983dffd..575a4bf 100644 --- a/backend/app/Models/Compra.php +++ b/backend/app/Models/Compra.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/CompraIngrediente.php b/backend/app/Models/CompraIngrediente.php index 574cc9a..ce2907f 100644 --- a/backend/app/Models/CompraIngrediente.php +++ b/backend/app/Models/CompraIngrediente.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Efectivo.php b/backend/app/Models/Efectivo.php index 07da79f..d929fbb 100644 --- a/backend/app/Models/Efectivo.php +++ b/backend/app/Models/Efectivo.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/EstadoProduccion.php b/backend/app/Models/EstadoProduccion.php index 4c99045..4038757 100644 --- a/backend/app/Models/EstadoProduccion.php +++ b/backend/app/Models/EstadoProduccion.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Factura.php b/backend/app/Models/Factura.php index 1f2b048..a4f7550 100644 --- a/backend/app/Models/Factura.php +++ b/backend/app/Models/Factura.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Ingrediente.php b/backend/app/Models/Ingrediente.php index 4879f00..c68612c 100644 --- a/backend/app/Models/Ingrediente.php +++ b/backend/app/Models/Ingrediente.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -10,7 +11,13 @@ class Ingrediente extends Model { use UuidPrimaryKey, SoftDeletes; protected $table = 'ingredientes'; + protected $fillable = ['id', 'nombre', 'medida', 'restaurante_id']; + public static function findOrFail($id) { + $ingrediente = Ingrediente::find($id); + if(!$ingrediente) throw new ModelNotFoundException("ingrediente", $id); + return $ingrediente; + } public function recetas() { return $this->hasMany(Receta::class, 'ingrediente_id'); diff --git a/backend/app/Models/MedioPago.php b/backend/app/Models/MedioPago.php index 879bf43..0a34c9a 100644 --- a/backend/app/Models/MedioPago.php +++ b/backend/app/Models/MedioPago.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Mesero.php b/backend/app/Models/Mesero.php index ddccf8f..f787e2a 100644 --- a/backend/app/Models/Mesero.php +++ b/backend/app/Models/Mesero.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Producto.php b/backend/app/Models/Producto.php index f63b4a3..ff5c38c 100644 --- a/backend/app/Models/Producto.php +++ b/backend/app/Models/Producto.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Productor.php b/backend/app/Models/Productor.php index af13f64..4fb02a2 100644 --- a/backend/app/Models/Productor.php +++ b/backend/app/Models/Productor.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Proveedor.php b/backend/app/Models/Proveedor.php index e1c412c..b43788a 100644 --- a/backend/app/Models/Proveedor.php +++ b/backend/app/Models/Proveedor.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Recaudador.php b/backend/app/Models/Recaudador.php index 8e8f90c..44ad161 100644 --- a/backend/app/Models/Recaudador.php +++ b/backend/app/Models/Recaudador.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Receta.php b/backend/app/Models/Receta.php index 55d6468..619024b 100644 --- a/backend/app/Models/Receta.php +++ b/backend/app/Models/Receta.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; class Receta extends Model { diff --git a/backend/app/Models/Restaurante.php b/backend/app/Models/Restaurante.php index 5626855..33ce630 100644 --- a/backend/app/Models/Restaurante.php +++ b/backend/app/Models/Restaurante.php @@ -7,6 +7,7 @@ use App\Models\Sector; use App\Models\ZonaProduccion; use App\Models\Categoria; use App\Models\Proveedor; +use App\Models\Ingrediente; use App\Traits\UuidPrimaryKey; use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; @@ -50,4 +51,8 @@ class Restaurante extends Model { public function proveedores() { return $this->hasMany(Proveedor::class, 'restaurante_id'); } + + public function ingredientes() { + return $this->hasMany(Ingrediente::class, 'restaurante_id'); + } } diff --git a/backend/app/Models/TipoCanal.php b/backend/app/Models/TipoCanal.php index c47afda..d08f5df 100644 --- a/backend/app/Models/TipoCanal.php +++ b/backend/app/Models/TipoCanal.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/Venta.php b/backend/app/Models/Venta.php index 0357c70..4ba7854 100644 --- a/backend/app/Models/Venta.php +++ b/backend/app/Models/Venta.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/app/Models/VentaProducto.php b/backend/app/Models/VentaProducto.php index 4a73ab9..5b3c929 100644 --- a/backend/app/Models/VentaProducto.php +++ b/backend/app/Models/VentaProducto.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; diff --git a/backend/routes/web.php b/backend/routes/web.php index 6e66e4e..f9c06c7 100644 --- a/backend/routes/web.php +++ b/backend/routes/web.php @@ -53,5 +53,11 @@ $router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']], $router->post( '/{restaurante_id}/proveedores', ['as' => 'proveedores.create', 'uses' => 'ProveedoresController@create']); $router->put( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.update', 'uses' => 'ProveedoresController@update']); $router->delete('/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.delete', 'uses' => 'ProveedoresController@delete']); + + $router->get( '/{restaurante_id}/ingredientes', ['as' => 'ingredientes.all', 'uses' => 'IngredientesController@all']); + $router->get( '/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.get', 'uses' => 'IngredientesController@get']); + $router->post( '/{restaurante_id}/ingredientes', ['as' => 'ingredientes.create', 'uses' => 'IngredientesController@create']); + $router->put( '/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.update', 'uses' => 'IngredientesController@update']); + $router->delete('/{restaurante_id}/ingredientes/{id}', ['as' => 'ingredientes.delete', 'uses' => 'IngredientesController@delete']); }); });