126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Restaurante;
|
|
use App\Models\ZonaProduccion;
|
|
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 ZonasProduccionController extends Controller {
|
|
|
|
/**
|
|
* Obtiene de forma paginada las zonas de produccion registradas en el backend
|
|
*/
|
|
public function all(Request $request, $restaurante_id) {
|
|
app(UuidService::class)->validOrFail($restaurante_id);
|
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
|
|
|
$zonasProduccion = $restaurante->zonasProduccion();
|
|
|
|
$paginate = app(PaginatorService::class)->paginate(
|
|
perPage: $request->input('per_page', 15),
|
|
page: $request->input('page', 1),
|
|
total: $zonasProduccion->count(),
|
|
route: 'restaurant.all',
|
|
data: ['restaurante_id' => $restaurante_id]
|
|
);
|
|
|
|
$data = $zonasProduccion->get()
|
|
->skip($paginate['from'] - 1)
|
|
->take($paginate['per_page'])
|
|
->all();
|
|
|
|
return response()->json([
|
|
'pagination' => $paginate,
|
|
'data' => $data
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Obtiene una zonas de produccion 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);
|
|
$zonaProduccion = ZonaProduccion::findOrFail($id);
|
|
|
|
if($zonaProduccion->restaurante != $restaurante) {
|
|
throw new ModelNotFoundException("zona_produccion", $id);
|
|
}
|
|
|
|
return response()->json($zonaProduccion);
|
|
}
|
|
|
|
/**
|
|
* Crea una nueva zona de produccion
|
|
*/
|
|
public function create(Request $request, $restaurante_id) {
|
|
app(UuidService::class)->validOrFail($restaurante_id);
|
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
|
|
|
$this->validate($request, [
|
|
'nombre' => 'required'
|
|
]);
|
|
|
|
$zonaProduccion = ZonaProduccion::create([
|
|
'id' => Uuid::uuid4(),
|
|
'nombre' => $request->input('nombre'),
|
|
'restaurante_id' => $restaurante->id
|
|
]);
|
|
|
|
return response()->json($zonaProduccion, 201);
|
|
}
|
|
|
|
/**
|
|
* Actualiza un zona de produccion
|
|
*/
|
|
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);
|
|
$zonaProduccion = ZonaProduccion::findOrFail($id);
|
|
|
|
if($zonaProduccion->restaurante != $restaurante) {
|
|
throw new ModelNotFoundException("sector", $id);
|
|
}
|
|
|
|
$zonaProduccion->nombre = $request->input('nombre');
|
|
$zonaProduccion->save();
|
|
|
|
return response()->json($zonaProduccion);
|
|
}
|
|
|
|
/**
|
|
* Elimina un sector
|
|
*/
|
|
public function delete(Request $request, $restaurante_id, $id) {
|
|
app(UuidService::class)->validOrFail($restaurante_id);
|
|
app(UuidService::class)->validOrFail($id);
|
|
|
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
|
$zonaProduccion = ZonaProduccion::findOrFail($id);
|
|
|
|
if($zonaProduccion->restaurante != $restaurante) {
|
|
throw new ModelNotFoundException("zona_produccion", $id);
|
|
}
|
|
|
|
$zonaProduccion->delete();
|
|
|
|
return response()->json([], 204);
|
|
}
|
|
}
|