From 4f1dfd1221690ca03425ccebc4cc522648eb8cec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Cort=C3=A9s?= Date: Mon, 12 Jul 2021 10:23:15 -0400 Subject: [PATCH] crud de canales de venta y sectores --- .../Controllers/CanalesVentaController.php | 90 +++++++++++-- .../Controllers/RestaurantesController.php | 2 +- .../Http/Controllers/SectoresController.php | 125 ++++++++++++++++++ backend/app/Models/CanalVenta.php | 1 + backend/app/Models/Restaurante.php | 5 + backend/app/Models/Sector.php | 8 ++ backend/app/Services/PaginatorService.php | 2 +- backend/routes/web.php | 6 + 8 files changed, 228 insertions(+), 11 deletions(-) create mode 100644 backend/app/Http/Controllers/SectoresController.php diff --git a/backend/app/Http/Controllers/CanalesVentaController.php b/backend/app/Http/Controllers/CanalesVentaController.php index 42e85c4..552fa17 100644 --- a/backend/app/Http/Controllers/CanalesVentaController.php +++ b/backend/app/Http/Controllers/CanalesVentaController.php @@ -4,6 +4,8 @@ namespace App\Http\Controllers; use App\Models\Restaurante; use App\Models\CanalVenta; +use App\Models\Sector; +use App\Models\TipoCanal; use App\Services\UuidService; use App\Services\PaginatorService; use Illuminate\Http\JsonResponse; @@ -44,35 +46,105 @@ class CanalesVentaController extends Controller { /** * Obtiene un canal de venta por su id */ - public function get($restaurante_id, $id) { + public function get(Request $request, $restaurante_id, $id) { app(UuidService::class)->validOrFail($id); app(UuidService::class)->validOrFail($restaurante_id); $restaurante = Restaurante::findOrFail($restaurante_id); $canalVenta = CanalVenta::findOrFail($id); + if($canalVenta->restaurante != $restaurante) { + throw new ModelNotFoundException("canal_venta", $id); + } + return response()->json($canalVenta); } /** * Crea un nuevo canal de venta - * @param Request $request - * @return JsonResponse - * @throws ValidationException */ - public function create(Request $request) { + public function create(Request $request, $restaurante_id) { $this->validate($request, [ 'nombre' => 'required', 'sector_id' => 'required|exists:sectores,id', - 'restaurante_id' => 'required|exists:restaurantes,id', 'tipo_canal_id' => 'required|exists:tipos_canal,id' ]); - $restaurant = Restaurante::create([ + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($request->input('sector_id')); + app(UuidService::class)->validOrFail($request->input('tipo_canal_id')); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $sector = Sector::findOrFail($request->input('sector_id')); + $tipo_canal = TipoCanal::findOrFail($request->input('tipo_canal_id')); + + if($sector->restaurante != $restaurante) { + throw new ModelNotFoundException("sector", $id); + } + + $canalVenta = CanalVenta::create([ 'id' => Uuid::uuid4(), - 'nombre' => $request->input('nombre') + 'nombre' => $request->input('nombre'), + 'sector_id' => $sector->id, + 'tipo_canal_id' => $tipo_canal->id, + 'restaurante_id' => $restaurante_id ]); - return response()->json($restaurant, 201); + return response()->json($canalVenta, 201); + } + + /** + * Actualiza un canal de venta + */ + public function update(Request $request, $restaurante_id, $id) { + $this->validate($request, [ + 'nombre' => 'required', + 'sector_id' => 'required|exists:sectores,id', + 'tipo_canal_id' => 'required|exists:tipos_canal,id' + ]); + + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($request->input('sector_id')); + app(UuidService::class)->validOrFail($request->input('tipo_canal_id')); + app(UuidService::class)->validOrFail($id); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $sector = Sector::findOrFail($request->input('sector_id')); + $tipo_canal = TipoCanal::findOrFail($request->input('tipo_canal_id')); + $canalVenta = CanalVenta::findOrFail($id); + + if($sector->restaurante != $restaurante) { + throw new ModelNotFoundException("sector", $id); + } + + if($canalVenta->restaurante != $restaurante) { + throw new ModelNotFoundException("canal_venta", $id); + } + + $canalVenta->nombre = $request->input('nombre'); + $canalVenta->sector_id = $request->input('sector_id'); + $canalVenta->tipo_canal_id = $request->input('tipo_canal_id'); + $canalVenta->save(); + + return response()->json($canalVenta); + } + + /** + * Elimina un canal de venta + */ + public function delete(Request $request, $restaurante_id, $id) { + app(UuidService::class)->validOrFail($restaurante_id); + app(UuidService::class)->validOrFail($id); + + $restaurante = Restaurante::findOrFail($restaurante_id); + $canalVenta = CanalVenta::findOrFail($id); + + if($canalVenta->restaurante != $restaurante) { + throw new ModelNotFoundException("canal_venta", $id); + } + + $canalVenta->delete(); + + return response()->json([], 204); } } diff --git a/backend/app/Http/Controllers/RestaurantesController.php b/backend/app/Http/Controllers/RestaurantesController.php index 92b9155..c78ee92 100644 --- a/backend/app/Http/Controllers/RestaurantesController.php +++ b/backend/app/Http/Controllers/RestaurantesController.php @@ -93,6 +93,6 @@ class RestaurantesController extends Controller { $restaurant = Restaurante::findOrFail($id); $restaurant->delete(); - return response()->json($restaurant); + return response()->json([], 204); } } diff --git a/backend/app/Http/Controllers/SectoresController.php b/backend/app/Http/Controllers/SectoresController.php new file mode 100644 index 0000000..a1ea34f --- /dev/null +++ b/backend/app/Http/Controllers/SectoresController.php @@ -0,0 +1,125 @@ +validOrFail($restaurante_id); + $restaurante = Restaurante::findOrFail($restaurante_id); + + $sectores = $restaurante->sectores(); + + $paginate = app(PaginatorService::class)->paginate( + perPage: $request->input('per_page', 15), + page: $request->input('page', 1), + total: $sectores->count(), + route: 'restaurant.all', + data: ['restaurante_id' => $restaurante_id] + ); + + $data = $sectores->get() + ->skip($paginate['from'] - 1) + ->take($paginate['per_page']) + ->all(); + + return response()->json([ + 'pagination' => $paginate, + 'data' => $data + ]); + } + + /** + * Obtiene un sector 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); + $sector = Sector::findOrFail($id); + + if($sector->restaurante != $restaurante) { + throw new ModelNotFoundException("sector", $id); + } + + return response()->json($sector); + } + + /** + * Crea un nuevo sector + */ + public function create(Request $request, $restaurante_id) { + app(UuidService::class)->validOrFail($restaurante_id); + $restaurante = Restaurante::findOrFail($restaurante_id); + + $this->validate($request, [ + 'nombre' => 'required' + ]); + + $sector = Sector::create([ + 'id' => Uuid::uuid4(), + 'nombre' => $request->input('nombre'), + 'restaurante_id' => $restaurante->id + ]); + + return response()->json($sector, 201); + } + + /** + * Actualiza un sector + */ + 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); + $sector = Sector::findOrFail($id); + + if($sector->restaurante != $restaurante) { + throw new ModelNotFoundException("sector", $id); + } + + $sector->nombre = $request->input('nombre'); + $sector->save(); + + return response()->json($sector); + } + + /** + * 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); + $sector = Sector::findOrFail($id); + + if($sector->restaurante != $restaurante) { + throw new ModelNotFoundException("sector", $id); + } + + $sector->delete(); + + return response()->json([], 204); + } +} diff --git a/backend/app/Models/CanalVenta.php b/backend/app/Models/CanalVenta.php index 178183a..7818eea 100644 --- a/backend/app/Models/CanalVenta.php +++ b/backend/app/Models/CanalVenta.php @@ -11,6 +11,7 @@ class CanalVenta extends Model { use UuidPrimaryKey, SoftDeletes; protected $table = 'canales_venta'; + protected $fillable = ['id', 'nombre', 'restaurante_id', 'sector_id', 'tipo_canal_id']; public static function findOrFail($id) { $canal_venta = CanalVenta::find($id); diff --git a/backend/app/Models/Restaurante.php b/backend/app/Models/Restaurante.php index 7e56aed..d81070d 100644 --- a/backend/app/Models/Restaurante.php +++ b/backend/app/Models/Restaurante.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Models\CanalVenta; +use App\Models\Sector; use App\Traits\UuidPrimaryKey; use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; @@ -30,4 +31,8 @@ class Restaurante extends Model { public function canalesVenta() { return $this->hasMany(CanalVenta::class, 'restaurante_id'); } + + public function sectores() { + return $this->hasMany(Sector::class, 'restaurante_id'); + } } diff --git a/backend/app/Models/Sector.php b/backend/app/Models/Sector.php index bb34502..d0c4b4e 100644 --- a/backend/app/Models/Sector.php +++ b/backend/app/Models/Sector.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -10,6 +11,13 @@ class Sector extends Model { use UuidPrimaryKey, SoftDeletes; protected $table = 'sectores'; + protected $fillable = ['id', 'nombre', 'restaurante_id']; + + public static function findOrFail($id) { + $sector = Sector::find($id); + if(!$sector) throw new ModelNotFoundException("sector", $id); + return $sector; + } public function restaurante() { return $this->belongsTo(Restaurante::class); diff --git a/backend/app/Services/PaginatorService.php b/backend/app/Services/PaginatorService.php index b1e761c..2ba629f 100644 --- a/backend/app/Services/PaginatorService.php +++ b/backend/app/Services/PaginatorService.php @@ -38,7 +38,7 @@ class PaginatorService extends ServiceProvider { return [ 'per_page' => $perPage, - 'from' => $skip + 1, + 'from' => max(0, $skip + 1), 'to' => min($total, $skip + $perPage), 'total' => $total, 'current_page' => $currentPage, diff --git a/backend/routes/web.php b/backend/routes/web.php index 5529b93..f84f7a9 100644 --- a/backend/routes/web.php +++ b/backend/routes/web.php @@ -16,6 +16,12 @@ function registerRestaurantApi($router) { $router->post( '/{restaurante_id}/canales-venta', ['as' => 'canales-venta.create', 'uses' => 'CanalesVentaController@create']); $router->put( '/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.update', 'uses' => 'CanalesVentaController@update']); $router->delete('/{restaurante_id}/canales-venta/{id}', ['as' => 'canales-venta.delete', 'uses' => 'CanalesVentaController@delete']); + + $router->get( '/{restaurante_id}/sectores', ['as' => 'sectores.all', 'uses' => 'SectoresController@all']); + $router->get( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.get', 'uses' => 'SectoresController@get']); + $router->post( '/{restaurante_id}/sectores', ['as' => 'sectores.create', 'uses' => 'SectoresController@create']); + $router->put( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.update', 'uses' => 'SectoresController@update']); + $router->delete('/{restaurante_id}/sectores/{id}', ['as' => 'sectores.delete', 'uses' => 'SectoresController@delete']); }); }