Validaciones y Zonas de produccion
This commit is contained in:
23
backend/app/Exceptions/CantDeleteHasChildException.php
Normal file
23
backend/app/Exceptions/CantDeleteHasChildException.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class CantDeleteHasChildException extends Exception {
|
||||||
|
|
||||||
|
protected $parent;
|
||||||
|
protected $child;
|
||||||
|
|
||||||
|
public function __construct($parent, $child) {
|
||||||
|
$this->parent = $parent;
|
||||||
|
$this->child = $child;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render($request) {
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'cant_delete',
|
||||||
|
'message' => $this->parent . ' tiene ' . $this->child . ' asociados'
|
||||||
|
], 400);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,8 +18,8 @@ class GenericException extends Exception {
|
|||||||
|
|
||||||
public function render($request) {
|
public function render($request) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'error' => $this->error
|
'error' => $this->error,
|
||||||
'message' => $this->message
|
'message' => $this->message
|
||||||
], $status);
|
], $this->status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use App\Models\Restaurante;
|
|||||||
use App\Services\PaginatorService;
|
use App\Services\PaginatorService;
|
||||||
use App\Services\UuidService;
|
use App\Services\UuidService;
|
||||||
use App\Exceptions\GenericException;
|
use App\Exceptions\GenericException;
|
||||||
|
use App\Exceptions\CantDeleteHasChildException;
|
||||||
use App\Exceptions\ModelNotFoundException;
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -92,6 +93,13 @@ class RestaurantesController extends Controller {
|
|||||||
app(UuidService::class)->validOrFail($id);
|
app(UuidService::class)->validOrFail($id);
|
||||||
|
|
||||||
$restaurant = Restaurante::findOrFail($id);
|
$restaurant = Restaurante::findOrFail($id);
|
||||||
|
|
||||||
|
if($restaurant->usuarios()->count() > 0) throw new CantDeleteHasChildException("restaurant", "usuario");
|
||||||
|
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");
|
||||||
|
|
||||||
|
|
||||||
$restaurant->delete();
|
$restaurant->delete();
|
||||||
return response()->json([], 204);
|
return response()->json([], 204);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use App\Models\Sector;
|
|||||||
use App\Services\PaginatorService;
|
use App\Services\PaginatorService;
|
||||||
use App\Services\UuidService;
|
use App\Services\UuidService;
|
||||||
use App\Exceptions\GenericException;
|
use App\Exceptions\GenericException;
|
||||||
|
use App\Exceptions\CantdeletehasChild;
|
||||||
use App\Exceptions\ModelNotFoundException;
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -118,6 +119,10 @@ class SectoresController extends Controller {
|
|||||||
throw new ModelNotFoundException("sector", $id);
|
throw new ModelNotFoundException("sector", $id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($sector->canalesVenta()->count() > 0) {
|
||||||
|
throw new CantDeleteHasChildException("sector", "canal_venta");
|
||||||
|
}
|
||||||
|
|
||||||
$sector->delete();
|
$sector->delete();
|
||||||
|
|
||||||
return response()->json([], 204);
|
return response()->json([], 204);
|
||||||
|
|||||||
125
backend/app/Http/Controllers/ZonasProduccionController.php
Normal file
125
backend/app/Http/Controllers/ZonasProduccionController.php
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Models\CanalVenta;
|
use App\Models\CanalVenta;
|
||||||
use App\Models\Sector;
|
use App\Models\Sector;
|
||||||
|
use App\Models\ZonaProduccion;
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
use App\Exceptions\ModelNotFoundException;
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@@ -35,4 +36,8 @@ class Restaurante extends Model {
|
|||||||
public function sectores() {
|
public function sectores() {
|
||||||
return $this->hasMany(Sector::class, 'restaurante_id');
|
return $this->hasMany(Sector::class, 'restaurante_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function zonasProduccion() {
|
||||||
|
return $this->hasMany(ZonaProduccion::class, 'restaurante_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,4 +22,8 @@ class Sector extends Model {
|
|||||||
public function restaurante() {
|
public function restaurante() {
|
||||||
return $this->belongsTo(Restaurante::class);
|
return $this->belongsTo(Restaurante::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function canalesVenta() {
|
||||||
|
return $this->hasMany(CanalVenta::class, 'sector_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
@@ -10,6 +11,13 @@ class ZonaProduccion extends Model {
|
|||||||
use UuidPrimaryKey, SoftDeletes;
|
use UuidPrimaryKey, SoftDeletes;
|
||||||
|
|
||||||
protected $table = 'zonas_produccion';
|
protected $table = 'zonas_produccion';
|
||||||
|
protected $fillable = ['id', 'nombre', 'restaurante_id'];
|
||||||
|
|
||||||
|
public static function findOrFail($id) {
|
||||||
|
$zonaProduccion = ZonaProduccion::find($id);
|
||||||
|
if(!$zonaProduccion) throw new ModelNotFoundException("zona_produccion", $id);
|
||||||
|
return $zonaProduccion;
|
||||||
|
}
|
||||||
|
|
||||||
public function restaurante() {
|
public function restaurante() {
|
||||||
return $this->belongsTo(Restaurante::class);
|
return $this->belongsTo(Restaurante::class);
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ function registerRestaurantApi($router) {
|
|||||||
$router->post( '/{restaurante_id}/sectores', ['as' => 'sectores.create', 'uses' => 'SectoresController@create']);
|
$router->post( '/{restaurante_id}/sectores', ['as' => 'sectores.create', 'uses' => 'SectoresController@create']);
|
||||||
$router->put( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.update', 'uses' => 'SectoresController@update']);
|
$router->put( '/{restaurante_id}/sectores/{id}', ['as' => 'sectores.update', 'uses' => 'SectoresController@update']);
|
||||||
$router->delete('/{restaurante_id}/sectores/{id}', ['as' => 'sectores.delete', 'uses' => 'SectoresController@delete']);
|
$router->delete('/{restaurante_id}/sectores/{id}', ['as' => 'sectores.delete', 'uses' => 'SectoresController@delete']);
|
||||||
|
|
||||||
|
$router->get( '/{restaurante_id}/zonas-produccion', ['as' => 'zonas-produccion.all', 'uses' => 'ZonasProduccionController@all']);
|
||||||
|
$router->get( '/{restaurante_id}/zonas-produccion/{id}', ['as' => 'zonas-produccion.get', 'uses' => 'ZonasProduccionController@get']);
|
||||||
|
$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']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user