forked from Somnia/SomniaRooms
Updated Somnia API exceptions to use the new API error handling.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\ApiErrorException;
|
||||
use App\DTO\ApiErrorMessage;
|
||||
use App\Entity\Booking;
|
||||
use App\Entity\BookingCustomer;
|
||||
use App\Entity\BookingRoom;
|
||||
@@ -21,6 +23,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class BookingController extends AbstractController
|
||||
{
|
||||
@@ -49,20 +52,19 @@ class BookingController extends AbstractController
|
||||
): JsonResponse {
|
||||
|
||||
if (\is_null($detailsRequest)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
throw new ApiErrorException(new ApiErrorMessage(400, ApiErrorMessage::TYPE_INVALID_REQUEST_BODY_FORMAT));
|
||||
}
|
||||
|
||||
$bookingList = $repoBooking->findBy(["refNumber" => $detailsRequest->refNumber]);
|
||||
if (\is_null($bookingList) || \count($bookingList) == 0) {
|
||||
throw new HttpException(404, 'Invalid Booking');
|
||||
$selectedBooking = $repoBooking->findOneBy(["refNumber" => $detailsRequest->refNumber]);
|
||||
if (\is_null($selectedBooking)) {
|
||||
throw new NotFoundHttpException('Booking not found');
|
||||
}
|
||||
|
||||
$customerBooking = $bookingList[0];
|
||||
if ($customerBooking->getBookingCustomer()->getEmail() != $detailsRequest->email) {
|
||||
throw new HttpException(404, 'Invalid Booking');
|
||||
if ($selectedBooking->getBookingCustomer()->getEmail() != $detailsRequest->email) {
|
||||
throw new NotFoundHttpException('Booking not found');
|
||||
}
|
||||
|
||||
return $this->json($customerBooking);
|
||||
return $this->json($selectedBooking);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,18 +95,23 @@ class BookingController extends AbstractController
|
||||
#[MapRequestPayload()] BookingCreateRequest $createRequest
|
||||
): JsonResponse {
|
||||
if (\is_null($createRequest)) {
|
||||
throw new HttpException('Invalid Payload', 400);
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(400, ApiErrorMessage::TYPE_INVALID_REQUEST_BODY_FORMAT)
|
||||
);
|
||||
}
|
||||
|
||||
$booking = new Booking();
|
||||
try {
|
||||
if ($createRequest->arrivalDate >= $createRequest->departureDate) {
|
||||
throw new HttpException(400, "Arrival cant be before departure");
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
||||
'Arrival cant be before departure',
|
||||
);
|
||||
}
|
||||
|
||||
$entityManager->beginTransaction();
|
||||
|
||||
$currentTime = new \DateTime();
|
||||
$currentTime = new \DateTime("now");
|
||||
|
||||
// Create booking from request data.
|
||||
$booking->setBookingTime($currentTime);
|
||||
@@ -124,7 +131,10 @@ class BookingController extends AbstractController
|
||||
$entityManager->persist($booking);
|
||||
|
||||
if (\count($createRequest->rooms) == 0) {
|
||||
throw new HttpException(400, "Se debe indicar al menos una habitación");
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
|
||||
'Booking must contain rooms',
|
||||
);
|
||||
}
|
||||
|
||||
// Create each room.
|
||||
@@ -135,7 +145,10 @@ class BookingController extends AbstractController
|
||||
//Get Room category.
|
||||
$roomCategory = $repoCat->find($roomItem->roomCategory);
|
||||
if ($roomCategory == null) {
|
||||
throw new HttpException(400, "La RoomCategory no es válida.");
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
|
||||
'Invalid RoomCategory: ' . $roomItem->roomCategory,
|
||||
);
|
||||
}
|
||||
|
||||
//Check room availability.
|
||||
@@ -144,7 +157,10 @@ class BookingController extends AbstractController
|
||||
$selectedGuestAmount = $roomItem->guestNumber;
|
||||
|
||||
if ($amountAvailable == null || $selectedAmount > $amountAvailable) {
|
||||
throw new HttpException(400, "No se pueden reservar más habitaciones de las disponibles.");
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(400, ApiErrorMessage::TYPE_LOGICAL_ERROR),
|
||||
'Not enought rooms available for category: ' . $roomItem->roomCategory,
|
||||
);
|
||||
}
|
||||
|
||||
$room->setRoomCategory($roomCategory);
|
||||
@@ -163,9 +179,9 @@ class BookingController extends AbstractController
|
||||
}
|
||||
|
||||
if (!$priceFound) {
|
||||
throw new HttpException(
|
||||
500,
|
||||
"No se puedo obtener el precio para la categoría: " . $roomCategory->getId() . " , guests: " . $selectedGuestAmount
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_LOGICAL_ERROR),
|
||||
'Price not found for category: ' . $roomCategory->getId() . ', guests: ' . $selectedGuestAmount,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\ApiErrorException;
|
||||
use App\DTO\ApiErrorMessage;
|
||||
use App\Entity\ConfigVariable;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -91,7 +93,9 @@ class ConfigVariableController extends AbstractController
|
||||
public function getVariablesBySection(ConfigVariableRepository $repo, string $section): Response
|
||||
{
|
||||
if(\is_null($section) || $section == '') {
|
||||
return new Response("Invalid section value", 404);
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(400, ApiErrorMessage::TYPE_INVALID_REQUEST_BODY_FORMAT)
|
||||
);
|
||||
}
|
||||
|
||||
$varList = $repo->findAllBySection($section);
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\ApiErrorException;
|
||||
use App\DTO\ApiErrorMessage;
|
||||
use App\Repository\FareTableRepository;
|
||||
use App\Repository\RoomAvailabilityRepository;
|
||||
use App\Repository\RoomCategoryRepository;
|
||||
@@ -53,11 +55,16 @@ class SearchRoomsController extends AbstractController
|
||||
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
|
||||
): Response {
|
||||
if (\is_null($searchRequest)) {
|
||||
throw new HttpException('Invalid Payload', 400);
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(400, ApiErrorMessage::TYPE_INVALID_REQUEST_BODY_FORMAT)
|
||||
);
|
||||
}
|
||||
|
||||
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
||||
throw new HttpException(400, "startDate cant be before endDate");
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
||||
'startDate cant be before endDate',
|
||||
);
|
||||
}
|
||||
$rs = $repoAvailability->getAvailabilityForPeriod($searchRequest->startDate, $searchRequest->endDate);
|
||||
|
||||
@@ -118,16 +125,24 @@ class SearchRoomsController extends AbstractController
|
||||
#[MapRequestPayload()] SearchPricesForCategoryRequest $searchRequest
|
||||
): Response {
|
||||
if (\is_null($searchRequest)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(400, ApiErrorMessage::TYPE_INVALID_REQUEST_BODY_FORMAT)
|
||||
);
|
||||
}
|
||||
|
||||
$roomCategory = $repoCat->find($searchRequest->roomCategory);
|
||||
if ($roomCategory === null) {
|
||||
throw new \Exception('Invalid Category', 400);
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
|
||||
'Invalid RoomCategory: ' . $searchRequest->roomCategory,
|
||||
);
|
||||
}
|
||||
|
||||
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
||||
throw new \Exception('Invalid time interval', 400);
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
||||
'startDate cant be before endDate',
|
||||
);
|
||||
}
|
||||
|
||||
$fareList = $priceService->getFinalPricesForSearch($roomCategory, $searchRequest->startDate, $searchRequest->endDate);
|
||||
|
||||
Reference in New Issue
Block a user