185 lines
5.7 KiB
PHP
185 lines
5.7 KiB
PHP
<?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);
|
|
//}
|
|
}
|