crud de canales de venta y sectores
This commit is contained in:
@@ -4,6 +4,8 @@ namespace App\Http\Controllers;
|
|||||||
|
|
||||||
use App\Models\Restaurante;
|
use App\Models\Restaurante;
|
||||||
use App\Models\CanalVenta;
|
use App\Models\CanalVenta;
|
||||||
|
use App\Models\Sector;
|
||||||
|
use App\Models\TipoCanal;
|
||||||
use App\Services\UuidService;
|
use App\Services\UuidService;
|
||||||
use App\Services\PaginatorService;
|
use App\Services\PaginatorService;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
@@ -44,35 +46,105 @@ class CanalesVentaController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* Obtiene un canal de venta por su id
|
* 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($id);
|
||||||
app(UuidService::class)->validOrFail($restaurante_id);
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
|
|
||||||
$restaurante = Restaurante::findOrFail($restaurante_id);
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
$canalVenta = CanalVenta::findOrFail($id);
|
$canalVenta = CanalVenta::findOrFail($id);
|
||||||
|
|
||||||
|
if($canalVenta->restaurante != $restaurante) {
|
||||||
|
throw new ModelNotFoundException("canal_venta", $id);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($canalVenta);
|
return response()->json($canalVenta);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crea un nuevo canal de venta
|
* 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, [
|
$this->validate($request, [
|
||||||
'nombre' => 'required',
|
'nombre' => 'required',
|
||||||
'sector_id' => 'required|exists:sectores,id',
|
'sector_id' => 'required|exists:sectores,id',
|
||||||
'restaurante_id' => 'required|exists:restaurantes,id',
|
|
||||||
'tipo_canal_id' => 'required|exists:tipos_canal,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(),
|
'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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,6 +93,6 @@ class RestaurantesController extends Controller {
|
|||||||
|
|
||||||
$restaurant = Restaurante::findOrFail($id);
|
$restaurant = Restaurante::findOrFail($id);
|
||||||
$restaurant->delete();
|
$restaurant->delete();
|
||||||
return response()->json($restaurant);
|
return response()->json([], 204);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
125
backend/app/Http/Controllers/SectoresController.php
Normal file
125
backend/app/Http/Controllers/SectoresController.php
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Restaurante;
|
||||||
|
use App\Models\Sector;
|
||||||
|
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 SectoresController extends Controller {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtiene de forma paginada los sectores registrados en el backend
|
||||||
|
*/
|
||||||
|
public function all(Request $request, $restaurante_id) {
|
||||||
|
app(UuidService::class)->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ class CanalVenta extends Model {
|
|||||||
use UuidPrimaryKey, SoftDeletes;
|
use UuidPrimaryKey, SoftDeletes;
|
||||||
|
|
||||||
protected $table = 'canales_venta';
|
protected $table = 'canales_venta';
|
||||||
|
protected $fillable = ['id', 'nombre', 'restaurante_id', 'sector_id', 'tipo_canal_id'];
|
||||||
|
|
||||||
public static function findOrFail($id) {
|
public static function findOrFail($id) {
|
||||||
$canal_venta = CanalVenta::find($id);
|
$canal_venta = CanalVenta::find($id);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Models\CanalVenta;
|
use App\Models\CanalVenta;
|
||||||
|
use App\Models\Sector;
|
||||||
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;
|
||||||
@@ -30,4 +31,8 @@ class Restaurante extends Model {
|
|||||||
public function canalesVenta() {
|
public function canalesVenta() {
|
||||||
return $this->hasMany(CanalVenta::class, 'restaurante_id');
|
return $this->hasMany(CanalVenta::class, 'restaurante_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function sectores() {
|
||||||
|
return $this->hasMany(Sector::class, 'restaurante_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 Sector extends Model {
|
|||||||
use UuidPrimaryKey, SoftDeletes;
|
use UuidPrimaryKey, SoftDeletes;
|
||||||
|
|
||||||
protected $table = 'sectores';
|
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() {
|
public function restaurante() {
|
||||||
return $this->belongsTo(Restaurante::class);
|
return $this->belongsTo(Restaurante::class);
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class PaginatorService extends ServiceProvider {
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'per_page' => $perPage,
|
'per_page' => $perPage,
|
||||||
'from' => $skip + 1,
|
'from' => max(0, $skip + 1),
|
||||||
'to' => min($total, $skip + $perPage),
|
'to' => min($total, $skip + $perPage),
|
||||||
'total' => $total,
|
'total' => $total,
|
||||||
'current_page' => $currentPage,
|
'current_page' => $currentPage,
|
||||||
|
|||||||
@@ -16,6 +16,12 @@ function registerRestaurantApi($router) {
|
|||||||
$router->post( '/{restaurante_id}/canales-venta', ['as' => 'canales-venta.create', 'uses' => 'CanalesVentaController@create']);
|
$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->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->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']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user