Proveedores
This commit is contained in:
142
backend/app/Http/Controllers/ProveedoresController.php
Normal file
142
backend/app/Http/Controllers/ProveedoresController.php
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Restaurante;
|
||||||
|
use App\Models\Proveedor;
|
||||||
|
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 ProveedoresController extends Controller {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtiene de forma paginada los proveedores registrados en el backend
|
||||||
|
*/
|
||||||
|
public function all(Request $request, $restaurante_id) {
|
||||||
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
|
|
||||||
|
$proveedores = $restaurante->proveedores();
|
||||||
|
|
||||||
|
$paginate = app(PaginatorService::class)->paginate(
|
||||||
|
perPage: $request->input('per_page', 15),
|
||||||
|
page: $request->input('page', 1),
|
||||||
|
total: $proveedores->count(),
|
||||||
|
route: 'restaurant.all',
|
||||||
|
data: ['restaurante_id' => $restaurante_id]
|
||||||
|
);
|
||||||
|
|
||||||
|
$data = $proveedores->get()
|
||||||
|
->skip($paginate['from'] - 1)
|
||||||
|
->take($paginate['per_page'])
|
||||||
|
->all();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'pagination' => $paginate,
|
||||||
|
'data' => $data
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtiene un proveedor 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);
|
||||||
|
$proveedor = Proveedor::findOrFail($id);
|
||||||
|
|
||||||
|
if($proveedor->restaurante != $restaurante) {
|
||||||
|
throw new ModelNotFoundException("proveedor", $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($proveedor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crea un nuevo proveedor
|
||||||
|
*/
|
||||||
|
public function create(Request $request, $restaurante_id) {
|
||||||
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
|
|
||||||
|
$this->validate($request, [
|
||||||
|
'rut' => 'required',
|
||||||
|
'nombre' => 'required',
|
||||||
|
'descripcion' => 'required',
|
||||||
|
'direccion' => 'sometimes|required',
|
||||||
|
'telefono' => 'sometimes|required'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$proveedor = Proveedor::create([
|
||||||
|
'id' => Uuid::uuid4(),
|
||||||
|
'rut' => $request->input('rut'),
|
||||||
|
'nombre' => $request->input('nombre'),
|
||||||
|
'descripcion' => $request->input('descripcion'),
|
||||||
|
'direccion' => $request->input('direccion', ''),
|
||||||
|
'telefono' => $request->input('telefono', ''),
|
||||||
|
'restaurante_id' => $restaurante->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json($proveedor, 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Actualiza un proveedor
|
||||||
|
*/
|
||||||
|
public function update(Request $request, $restaurante_id, $id) {
|
||||||
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
|
app(UuidService::class)->validOrFail($id);
|
||||||
|
|
||||||
|
$this->validate($request, [
|
||||||
|
'rut' => 'required',
|
||||||
|
'nombre' => 'required',
|
||||||
|
'descripcion' => 'required',
|
||||||
|
'direccion' => 'sometimes|required',
|
||||||
|
'telefono' => 'sometimes|required'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
|
$proveedor = Proveedor::findOrFail($id);
|
||||||
|
|
||||||
|
if($proveedor->restaurante != $restaurante) {
|
||||||
|
throw new ModelNotFoundException("proveedor", $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$proveedor->rut = $request->input('rut');
|
||||||
|
$proveedor->nombre = $request->input('nombre');
|
||||||
|
$proveedor->descripcion = $request->input('descripcion');
|
||||||
|
$proveedor->direccion = $request->input('direccion');
|
||||||
|
$proveedor->telefono = $request->input('telefono');
|
||||||
|
|
||||||
|
$proveedor->save();
|
||||||
|
|
||||||
|
return response()->json($proveedor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Elimina un proveedor
|
||||||
|
*/
|
||||||
|
public function delete(Request $request, $restaurante_id, $id) {
|
||||||
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
|
app(UuidService::class)->validOrFail($id);
|
||||||
|
|
||||||
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
|
$proveedor = Proveedor::findOrFail($id);
|
||||||
|
|
||||||
|
if($proveedor->restaurante != $restaurante) {
|
||||||
|
throw new ModelNotFoundException("proveedor", $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$proveedor->delete();
|
||||||
|
|
||||||
|
return response()->json([], 204);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,16 @@ class Proveedor extends Model {
|
|||||||
use UuidPrimaryKey, SoftDeletes;
|
use UuidPrimaryKey, SoftDeletes;
|
||||||
|
|
||||||
protected $table = 'proveedores';
|
protected $table = 'proveedores';
|
||||||
|
protected $fillable = [
|
||||||
|
'id', 'rut', 'nombre', 'descripcion',
|
||||||
|
'direccion', 'telefono', 'restaurante_id'
|
||||||
|
];
|
||||||
|
|
||||||
|
public static function findOrFail($id) {
|
||||||
|
$proveedor = Proveedor::find($id);
|
||||||
|
if(!$proveedor) throw new ModelNotFoundException("proveedor", $id);
|
||||||
|
return $proveedor;
|
||||||
|
}
|
||||||
|
|
||||||
public function restaurante() {
|
public function restaurante() {
|
||||||
return $this->belongsTo(Restaurante::class);
|
return $this->belongsTo(Restaurante::class);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use App\Models\CanalVenta;
|
|||||||
use App\Models\Sector;
|
use App\Models\Sector;
|
||||||
use App\Models\ZonaProduccion;
|
use App\Models\ZonaProduccion;
|
||||||
use App\Models\Categoria;
|
use App\Models\Categoria;
|
||||||
|
use App\Models\Proveedor;
|
||||||
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;
|
||||||
@@ -45,4 +46,8 @@ class Restaurante extends Model {
|
|||||||
public function categorias() {
|
public function categorias() {
|
||||||
return $this->hasMany(Categoria::class, 'restaurante_id');
|
return $this->hasMany(Categoria::class, 'restaurante_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function proveedores() {
|
||||||
|
return $this->hasMany(Proveedor::class, 'restaurante_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,5 +47,11 @@ $router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'log_endpoint']],
|
|||||||
$router->post( '/{restaurante_id}/categorias', ['as' => 'categorias.create', 'uses' => 'CategoriasController@create']);
|
$router->post( '/{restaurante_id}/categorias', ['as' => 'categorias.create', 'uses' => 'CategoriasController@create']);
|
||||||
$router->put( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.update', 'uses' => 'CategoriasController@update']);
|
$router->put( '/{restaurante_id}/categorias/{id}', ['as' => 'categorias.update', 'uses' => 'CategoriasController@update']);
|
||||||
$router->delete('/{restaurante_id}/categorias/{id}', ['as' => 'categorias.delete', 'uses' => 'CategoriasController@delete']);
|
$router->delete('/{restaurante_id}/categorias/{id}', ['as' => 'categorias.delete', 'uses' => 'CategoriasController@delete']);
|
||||||
|
|
||||||
|
$router->get( '/{restaurante_id}/proveedores', ['as' => 'proveedores.all', 'uses' => 'ProveedoresController@all']);
|
||||||
|
$router->get( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.get', 'uses' => 'ProveedoresController@get']);
|
||||||
|
$router->post( '/{restaurante_id}/proveedores', ['as' => 'proveedores.create', 'uses' => 'ProveedoresController@create']);
|
||||||
|
$router->put( '/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.update', 'uses' => 'ProveedoresController@update']);
|
||||||
|
$router->delete('/{restaurante_id}/proveedores/{id}', ['as' => 'proveedores.delete', 'uses' => 'ProveedoresController@delete']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user