Categorias

This commit is contained in:
2021-07-12 16:52:59 -04:00
parent 4766b56062
commit c0fdba3072
7 changed files with 163 additions and 31 deletions

View 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);
}
}

View File

@@ -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();

View File

@@ -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);

View File

@@ -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);

View File

@@ -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');
}
}

View File

@@ -2,9 +2,22 @@
use Laravel\Lumen\Routing\Router;
function registerRestaurantApi($router) {
$router->get('/', function () use ($router) {
return 'Public View';
});
$router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']], function () use ($router) {
$router->group(['prefix' => '/users'], function () use ($router) {
$router->get( '/', ['as' => 'users.all', 'uses' => 'UsuariosController@all']);
$router->get( '/{id}', ['as' => 'users.get', 'uses' => 'UsuariosController@get']);
$router->post( '/', ['as' => 'users.create', 'uses' => 'UsuariosController@create']);
$router->put( '/{id}', ['as' => 'users.update', 'uses' => 'UsuariosController@update']);
$router->delete('/{id}', ['as' => 'users.delete', 'uses' => 'UsuariosController@delete']);
$router->put( '/{id}/restaurantes/{restaurant}', ['as' => 'users.add_to_restaurant', 'uses' => 'UsuariosController@addToRestaurant']);
$router->delete('/{id}/restaurantes/{restaurant}', ['as' => 'users.remove_from_restaurant', 'uses' => 'UsuariosController@removeFromRestaurant']);
});
$router->group(['prefix' => '/restaurantes'], function () use ($router) {
// Rutas del recurso
$router->get( '/', ['as' => 'restaurant.all', 'uses' => 'RestaurantesController@all']);
$router->get( '/{id}', ['as' => 'restaurant.get', 'uses' => 'RestaurantesController@get']);
$router->post( '/', ['as' => 'restaurant.create', 'uses' => 'RestaurantesController@create']);
@@ -28,31 +41,11 @@ function registerRestaurantApi($router) {
$router->post( '/{restaurante_id}/zonas-produccion', ['as' => 'zonas-produccion.create', 'uses' => 'ZonasProduccionController@create']);
$router->put( '/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.update', 'uses' => 'ZonasProduccionController@update']);
$router->delete('/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.delete', 'uses' => 'ZonasProduccionController@delete']);
$router->get( '/{restaurante_id}/categorias', ['as' => 'categorias.all', 'uses' => 'CategoriasController@all']);
$router->get( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.get', 'uses' => 'CategoriasController@get']);
$router->post( '/{restaurante_id}/categorias', ['as' => 'categorias.create', 'uses' => 'CategoriasController@create']);
$router->put( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.update', 'uses' => 'CategoriasController@update']);
$router->delete('/{restaurante_id}/categorias/{id}', ['as' => 'categorias.delete', 'uses' => 'CategoriasController@delete']);
});
}
function registerUserApi($router) {
$router->group(['prefix' => '/users'], function () use ($router) {
// Rutas del recurso
$router->get( '/', ['as' => 'users.all', 'uses' => 'UsuariosController@all']);
$router->get( '/{id}', ['as' => 'users.get', 'uses' => 'UsuariosController@get']);
$router->post( '/', ['as' => 'users.create', 'uses' => 'UsuariosController@create']);
$router->put( '/{id}', ['as' => 'users.update', 'uses' => 'UsuariosController@update']);
$router->delete('/{id}', ['as' => 'users.delete', 'uses' => 'UsuariosController@delete']);
// Rutas de acciones
$router->put( '/{id}/restaurantes/{restaurant}', ['as' => 'users.add_to_restaurant', 'uses' => 'UsuariosController@addToRestaurant']);
$router->delete('/{id}/restaurantes/{restaurant}', ['as' => 'users.remove_from_restaurant', 'uses' => 'UsuariosController@removeFromRestaurant']);
});
}
/** @var Router $router */
$router->get('/', function () use ($router) {
return 'Public View';
});
$router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']], function () use ($router) {
registerUserApi($router);
registerRestaurantApi($router);
});