Reservation Controller
This commit is contained in:
103
app/src/Controller/API/Ventas/Reservation.php
Normal file
103
app/src/Controller/API/Ventas/Reservation.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user