Mantenedor Ingredientes
This commit is contained in:
129
backend/app/Http/Controllers/IngredientesController.php
Normal file
129
backend/app/Http/Controllers/IngredientesController.php
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Restaurante;
|
||||||
|
use App\Models\Ingrediente;
|
||||||
|
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;
|
||||||
|
use Ramsey\Uuid\Uuid;
|
||||||
|
|
||||||
|
class IngredientesController extends Controller {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtiene de forma paginada los ingredientes registrados en el backend
|
||||||
|
*/
|
||||||
|
public function all(Request $request, $restaurante_id) {
|
||||||
|
app(UuidService::class)->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
@@ -10,7 +11,13 @@ class Ingrediente extends Model {
|
|||||||
use UuidPrimaryKey, SoftDeletes;
|
use UuidPrimaryKey, SoftDeletes;
|
||||||
|
|
||||||
protected $table = 'ingredientes';
|
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() {
|
public function recetas() {
|
||||||
return $this->hasMany(Receta::class, 'ingrediente_id');
|
return $this->hasMany(Receta::class, 'ingrediente_id');
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class Receta extends Model {
|
class Receta extends Model {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use App\Models\Sector;
|
|||||||
use App\Models\ZonaProduccion;
|
use App\Models\ZonaProduccion;
|
||||||
use App\Models\Categoria;
|
use App\Models\Categoria;
|
||||||
use App\Models\Proveedor;
|
use App\Models\Proveedor;
|
||||||
|
use App\Models\Ingrediente;
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
use App\Exceptions\ModelNotFoundException;
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@@ -50,4 +51,8 @@ class Restaurante extends Model {
|
|||||||
public function proveedores() {
|
public function proveedores() {
|
||||||
return $this->hasMany(Proveedor::class, 'restaurante_id');
|
return $this->hasMany(Proveedor::class, 'restaurante_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function ingredientes() {
|
||||||
|
return $this->hasMany(Ingrediente::class, 'restaurante_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
|||||||
@@ -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->post( '/{restaurante_id}/proveedores', ['as' => 'proveedores.create', 'uses' => 'ProveedoresController@create']);
|
||||||
$router->put( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.update', 'uses' => 'ProveedoresController@update']);
|
$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->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']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user