From a3c273543fc50f33f29067c327bb367b468f405c Mon Sep 17 00:00:00 2001 From: nat Date: Mon, 10 Jan 2022 17:13:28 -0300 Subject: [PATCH] Endpoint crea nuevo pedido --- .../Controllers/Api/SubpedidoController.php | 29 +++++++++++++++++++ routes/api.php | 1 + 2 files changed, 30 insertions(+) diff --git a/app/Http/Controllers/Api/SubpedidoController.php b/app/Http/Controllers/Api/SubpedidoController.php index d116e34..ef1169f 100644 --- a/app/Http/Controllers/Api/SubpedidoController.php +++ b/app/Http/Controllers/Api/SubpedidoController.php @@ -6,6 +6,9 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Filtros\FiltroDeSubpedido; use App\Subpedido; +use App\GrupoDeCompra; +use Illuminate\Validation\Rule; + class SubpedidoController extends Controller { @@ -20,4 +23,30 @@ class SubpedidoController extends Controller { return Subpedido::filtrar($filtros)->get(); } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + $validado = $this->validateSubpedido(); + $s = new Subpedido(); + $s->nombre = $validado["nombre"]; + $s->grupo_de_compra_id = $validado["grupo_de_compra_id"]; + $s->save(); + return $s; + } + + protected function validateSubpedido(){ + return request()->validate([ + 'nombre' => 'required|max:2', + 'grupo_de_compra_id' => [ + 'required', + Rule::in(GrupoDeCompra::all()->pluck('id')), + ] + ]); + } } diff --git a/routes/api.php b/routes/api.php index 52aa27b..022cd17 100644 --- a/routes/api.php +++ b/routes/api.php @@ -41,5 +41,6 @@ Route::middleware('api')->group(function () { //@TO DO -> esta ruta debe estar en middleware de autenticacion Route::prefix('subpedidos')->group(function () { Route::get('/','Api\SubpedidoController@index'); + Route::post('/','Api\SubpedidoController@store'); }); });