From 2a0335f8348241b5183eb6d353f6adadf8a7670a Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 3 Mar 2025 15:21:18 -0300 Subject: [PATCH] Reservation Controller --- app/src/Controller/API/Ventas/Reservation.php | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 app/src/Controller/API/Ventas/Reservation.php diff --git a/app/src/Controller/API/Ventas/Reservation.php b/app/src/Controller/API/Ventas/Reservation.php new file mode 100644 index 0000000..42cec23 --- /dev/null +++ b/app/src/Controller/API/Ventas/Reservation.php @@ -0,0 +1,103 @@ +getAll(); + } catch (ServiceAction\Read $exception) { + return $this->withError($response, $exception); + } + + return $this->withJson($response, compact('reservations')); + } + public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface + { + $output = [ + 'reservation_id' => $reservation_id, + 'reservation' => null, + ]; + + try { + $output['reservation'] = $reservationService->get($reservation_id); + } catch (ServiceAction\Read $exception) { + return $this->withError($response, $exception); + } + + return $this->withJson($response, $output); + } + public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService): ResponseInterface + { + $input = $request->getParsedBody(); + $output = [ + 'input' => $input, + 'reservations' => [], + 'success' => false, + 'partial' => false, + 'errors' => [], + ]; + + try { + $output['reservations'] []= [ + 'reservation' => $reservationService->add($input), + 'success' => true + ]; + $output['partial'] = true; + } catch (ServiceAction\Create $exception) { + $output['errors'] []= $this->parseError($exception); + } + + if (count($input['reservations']) === count($output['reservations'])) { + $output['success'] = true; + } + + return $this->withJson($response, $output); + } + public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface + { + $input = $request->getParsedBody(); + $output = [ + 'input' => $input, + 'reservation_id' => $reservation_id, + 'reservations' => null, + 'success' => false, + ]; + + try { + $reservation = $reservationService->get($reservation_id); + $output['reservations'] = $reservationService->edit($reservation, $input); + $output['success'] = true; + } catch (ServiceAction\Read | ServiceAction\Update $exception) { + return $this->withError($response, $exception); + } + + return $this->withJson($response, $output); + } + public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface + { + $output = [ + 'reservation_id' => $reservation_id, + 'success' => false, + ]; + + try { + $reservationService->delete($reservation_id); + $output['success'] = true; + } catch (ServiceAction\Delete $exception) { + return $this->withError($response, $exception); + } + + return $this->withJson($response, $output); + } +}