Reservation Controller

This commit is contained in:
Juan Pablo Vial
2025-03-03 15:21:18 -03:00
parent 9ccf53fa4e
commit 2a0335f834

View File

@ -0,0 +1,103 @@
<?php
namespace Incoviba\Controller\API\Ventas;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;
class Reservation
{
use withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService): ResponseInterface
{
$reservations = [];
try {
$reservations = $reservationService->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);
}
}