Categorias
This commit is contained in:
125
backend/app/Http/Controllers/CategoriasController.php
Normal file
125
backend/app/Http/Controllers/CategoriasController.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Restaurante;
|
||||
use App\Models\Categoria;
|
||||
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 CategoriasController extends Controller {
|
||||
|
||||
/**
|
||||
* Obtiene de forma paginada las categorias registradas en el backend
|
||||
*/
|
||||
public function all(Request $request, $restaurante_id) {
|
||||
app(UuidService::class)->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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user