Comienzo de api de canales de venta
This commit is contained in:
184
backend/app/Http/Controllers/CanalesVentaController.php
Normal file
184
backend/app/Http/Controllers/CanalesVentaController.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Restaurante;
|
||||
use App\Models\CanalVenta;
|
||||
use App\Services\UuidService;
|
||||
use App\Services\PaginatorService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class CanalesVentaController extends Controller {
|
||||
|
||||
/**
|
||||
* Obtiene de forma paginada los canales de venta
|
||||
* de un restaurant registrados en el backend
|
||||
* @param Request $request
|
||||
* @param $restaurant
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function all(Request $request, $restaurante_id) {
|
||||
if (!app(UuidService::class)->is_valid($restaurante_id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $restaurante_id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$restaurante = Restaurante::findOrNull($restaurante_id);
|
||||
|
||||
if (!$restaurante) {
|
||||
return response()->json([
|
||||
'error' => 'restaurant_not_found',
|
||||
'message' => 'El restaurant con id ' . $restaurante_id . ' no existe'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$canalesVenta = $restaurante->canalesVenta();
|
||||
|
||||
$paginate = app(PaginatorService::class)->paginate(
|
||||
perPage: $request->input('per_page', 15),
|
||||
page: $request->input('page', 1),
|
||||
total: $canalesVenta->count(),
|
||||
route: 'canales-venta.all',
|
||||
data: [$restaurante_id],
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'pagination' => $paginate,
|
||||
'data' => array_values($canalesVenta->get()->skip($paginate['from'] - 1)->take($paginate['per_page'])->all())
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene un canal de venta por su id
|
||||
* @param $restaurante_id
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function get($restaurante_id, $id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!app(UuidService::class)->is_valid($restaurante_id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $restaurante_id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$restaurante = Restaurante::findOrNull($restaurante_id);
|
||||
|
||||
if (!$restaurante) {
|
||||
return response()->json([
|
||||
'error' => 'restaurant_not_found',
|
||||
'message' => 'El restaurant con id ' . $restaurante_id . ' no existe'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$canalVenta = CanalVenta::findOrNull($id);
|
||||
if (!$canalVenta) {
|
||||
return response()->json([
|
||||
'error' => 'canal_venta_not_found',
|
||||
'message' => 'El canal de venta con id ' . $id . ' no existe'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json($canalVenta);
|
||||
}
|
||||
|
||||
///**
|
||||
// * Crea un nuevo restaurant
|
||||
// * @param Request $request
|
||||
// * @return JsonResponse
|
||||
// * @throws ValidationException
|
||||
// */
|
||||
//public function create(Request $request) {
|
||||
// $this->validate($request, [
|
||||
// 'nombre' => 'required'
|
||||
// ]);
|
||||
|
||||
// if (!$request->user->canManageRestaurants()) {
|
||||
// return response()->json([
|
||||
// 'error' => 'cant_manage_restaurants',
|
||||
// 'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
||||
// ], 403);
|
||||
// }
|
||||
|
||||
// $restaurant = Restaurante::create([
|
||||
// 'id' => Uuid::uuid4(),
|
||||
// 'nombre' => $request->input('nombre')
|
||||
// ]);
|
||||
|
||||
// return response()->json($restaurant, 201);
|
||||
//}
|
||||
|
||||
///**
|
||||
// * Actualiza un restaurante
|
||||
// * @param Request $request
|
||||
// * @param $id
|
||||
// * @return JsonResponse
|
||||
// * @throws ValidationException
|
||||
// */
|
||||
//public function update(Request $request, $id) {
|
||||
// $this->validate($request, [
|
||||
// 'nombre' => 'required'
|
||||
// ]);
|
||||
|
||||
// if (!$request->user->canManageRestaurants()) {
|
||||
// return response()->json([
|
||||
// 'error' => 'cant_manage_restaurants',
|
||||
// 'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
||||
// ], 403);
|
||||
// }
|
||||
|
||||
// $restaurant = Restaurante::findOrNull($id);
|
||||
// if(!$restaurant) {
|
||||
// return response()->json([
|
||||
// 'error' => 'not_found',
|
||||
// 'message' => 'El restaurante con id ' . $id . ' no existe'
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// $restaurant->nombre = $request->input('nombre');
|
||||
// $restaurant->save();
|
||||
|
||||
// return response()->json($restaurant);
|
||||
//}
|
||||
|
||||
///**
|
||||
// * Elimina un restaurante
|
||||
// * @param Request $request
|
||||
// * @param $id
|
||||
// * @return JsonResponse
|
||||
// * @throws ValidationException
|
||||
// */
|
||||
//public function delete(Request $request, $id) {
|
||||
// if (!$request->user->canManageRestaurants()) {
|
||||
// return response()->json([
|
||||
// 'error' => 'cant_manage_restaurants',
|
||||
// 'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
||||
// ], 403);
|
||||
// }
|
||||
|
||||
// $restaurant = Restaurante::findOrNull($id);
|
||||
// if(!$restaurant) {
|
||||
// return response()->json([
|
||||
// 'error' => 'not_found',
|
||||
// 'message' => 'El restaurante con id ' . $id . ' no existe'
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// $restaurant->delete();
|
||||
|
||||
// return response()->json($restaurant);
|
||||
//}
|
||||
}
|
||||
@@ -36,6 +36,13 @@ class RestaurantesController extends Controller {
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function get($id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$restaurante = Restaurante::findOrNull($id);
|
||||
|
||||
if (!$restaurante) {
|
||||
@@ -82,6 +89,13 @@ class RestaurantesController extends Controller {
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function update(Request $request, $id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$this->validate($request, [
|
||||
'nombre' => 'required'
|
||||
]);
|
||||
@@ -115,6 +129,13 @@ class RestaurantesController extends Controller {
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function delete(Request $request, $id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!$request->user->canManageRestaurants()) {
|
||||
return response()->json([
|
||||
'error' => 'cant_manage_restaurants',
|
||||
|
||||
@@ -39,6 +39,13 @@ class UsuariosController extends Controller {
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function get($id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$usuario = Usuario::findByIdOrAuth0Id($id);
|
||||
|
||||
if (!$usuario) {
|
||||
@@ -108,6 +115,13 @@ class UsuariosController extends Controller {
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function update(Request $request, $id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$this->validate($request, [
|
||||
'nombre' => 'sometimes',
|
||||
'email' => 'sometimes|email',
|
||||
@@ -163,6 +177,13 @@ class UsuariosController extends Controller {
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function delete(Request $request, $id) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
/** @var Usuario $logged_user */
|
||||
$logged_user = $request->user;
|
||||
$usuario = Usuario::findByIdOrAuth0Id($id);
|
||||
@@ -202,6 +223,20 @@ class UsuariosController extends Controller {
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function addToRestaurant(Request $request, $id, $restaurant) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!app(UuidService::class)->is_valid($restaurant)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $restaurant. ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
/** @var Usuario $logged_user */
|
||||
$logged_user = $request->user;
|
||||
|
||||
@@ -213,6 +248,7 @@ class UsuariosController extends Controller {
|
||||
], 404);
|
||||
}
|
||||
|
||||
|
||||
$restaurant = Restaurante::findOrNull($restaurant);
|
||||
if (!$restaurant) {
|
||||
return response()->json([
|
||||
@@ -247,6 +283,20 @@ class UsuariosController extends Controller {
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function removeFromRestaurant(Request $request, $id, $restaurant) {
|
||||
if (!app(UuidService::class)->is_valid($id)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!app(UuidService::class)->is_valid($restaurant)) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_id',
|
||||
'message' => 'El id ' . $restaurant. ' no es un UUID valido'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$usuario = Usuario::findByIdOrAuth0Id($id);
|
||||
|
||||
if (!$usuario) {
|
||||
|
||||
@@ -11,6 +11,15 @@ class CanalVenta extends Model {
|
||||
|
||||
protected $table = 'canales_venta';
|
||||
|
||||
public static function findOrNull($id) {
|
||||
try {
|
||||
return CanalVenta::find($id);
|
||||
} catch (QueryException $ex) {
|
||||
Log::warning('Se intento obtener un canal de venta con un id invalido', ['id' => $id]);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function tipoCanal() {
|
||||
return $this->belongsTo(TipoCanal::class);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\CanalVenta;
|
||||
use App\Traits\UuidPrimaryKey;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
@@ -32,4 +33,8 @@ class Restaurante extends Model {
|
||||
public function usuarios() {
|
||||
return $this->belongsToMany(Usuario::class, 'usuarios_restaurantes', 'restaurante_id', 'usuario_id');
|
||||
}
|
||||
|
||||
public function canalesVenta() {
|
||||
return $this->hasMany(CanalVenta::class, 'restaurante_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Providers;
|
||||
|
||||
use App\Services\Auth0Service;
|
||||
use App\Services\PaginatorService;
|
||||
use App\Services\UuidService;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider {
|
||||
@@ -14,5 +15,8 @@ class AppServiceProvider extends ServiceProvider {
|
||||
$this->app->singleton(PaginatorService::class, function($app) {
|
||||
return new PaginatorService($app);
|
||||
});
|
||||
$this->app->singleton(UuidService::class, function($app) {
|
||||
return new UuidService($app);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
12
backend/app/Services/UuidService.php
Normal file
12
backend/app/Services/UuidService.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class UuidService extends ServiceProvider {
|
||||
public function is_valid(string $uuid) {
|
||||
$regex = '/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i';
|
||||
return preg_match($regex, $uuid) === 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user