130 lines
3.9 KiB
PHP
130 lines
3.9 KiB
PHP
<?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);
|
|
}
|
|
}
|