Updated Somnia API exceptions to use the new API error handling.

This commit is contained in:
2024-10-18 17:58:27 +02:00
parent facd5bc9c0
commit fbee23c022
3 changed files with 58 additions and 23 deletions
+33 -17
View File
@@ -2,6 +2,8 @@
namespace App\Controller; namespace App\Controller;
use App\ApiErrorException;
use App\DTO\ApiErrorMessage;
use App\Entity\Booking; use App\Entity\Booking;
use App\Entity\BookingCustomer; use App\Entity\BookingCustomer;
use App\Entity\BookingRoom; use App\Entity\BookingRoom;
@@ -21,6 +23,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use OpenApi\Attributes as OA; use OpenApi\Attributes as OA;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BookingController extends AbstractController class BookingController extends AbstractController
{ {
@@ -49,20 +52,19 @@ class BookingController extends AbstractController
): JsonResponse { ): JsonResponse {
if (\is_null($detailsRequest)) { 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]); $selectedBooking = $repoBooking->findOneBy(["refNumber" => $detailsRequest->refNumber]);
if (\is_null($bookingList) || \count($bookingList) == 0) { if (\is_null($selectedBooking)) {
throw new HttpException(404, 'Invalid Booking'); throw new NotFoundHttpException('Booking not found');
} }
$customerBooking = $bookingList[0]; if ($selectedBooking->getBookingCustomer()->getEmail() != $detailsRequest->email) {
if ($customerBooking->getBookingCustomer()->getEmail() != $detailsRequest->email) { throw new NotFoundHttpException('Booking not found');
throw new HttpException(404, 'Invalid Booking');
} }
return $this->json($customerBooking); return $this->json($selectedBooking);
} }
/** /**
@@ -93,18 +95,23 @@ class BookingController extends AbstractController
#[MapRequestPayload()] BookingCreateRequest $createRequest #[MapRequestPayload()] BookingCreateRequest $createRequest
): JsonResponse { ): JsonResponse {
if (\is_null($createRequest)) { 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(); $booking = new Booking();
try { try {
if ($createRequest->arrivalDate >= $createRequest->departureDate) { 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(); $entityManager->beginTransaction();
$currentTime = new \DateTime(); $currentTime = new \DateTime("now");
// Create booking from request data. // Create booking from request data.
$booking->setBookingTime($currentTime); $booking->setBookingTime($currentTime);
@@ -124,7 +131,10 @@ class BookingController extends AbstractController
$entityManager->persist($booking); $entityManager->persist($booking);
if (\count($createRequest->rooms) == 0) { 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. // Create each room.
@@ -135,7 +145,10 @@ class BookingController extends AbstractController
//Get Room category. //Get Room category.
$roomCategory = $repoCat->find($roomItem->roomCategory); $roomCategory = $repoCat->find($roomItem->roomCategory);
if ($roomCategory == null) { 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. //Check room availability.
@@ -144,7 +157,10 @@ class BookingController extends AbstractController
$selectedGuestAmount = $roomItem->guestNumber; $selectedGuestAmount = $roomItem->guestNumber;
if ($amountAvailable == null || $selectedAmount > $amountAvailable) { 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); $room->setRoomCategory($roomCategory);
@@ -163,9 +179,9 @@ class BookingController extends AbstractController
} }
if (!$priceFound) { if (!$priceFound) {
throw new HttpException( throw new ApiErrorException(
500, new ApiErrorMessage(422, ApiErrorMessage::TYPE_LOGICAL_ERROR),
"No se puedo obtener el precio para la categoría: " . $roomCategory->getId() . " , guests: " . $selectedGuestAmount 'Price not found for category: ' . $roomCategory->getId() . ', guests: ' . $selectedGuestAmount,
); );
} }
+5 -1
View File
@@ -2,6 +2,8 @@
namespace App\Controller; namespace App\Controller;
use App\ApiErrorException;
use App\DTO\ApiErrorMessage;
use App\Entity\ConfigVariable; use App\Entity\ConfigVariable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -91,7 +93,9 @@ class ConfigVariableController extends AbstractController
public function getVariablesBySection(ConfigVariableRepository $repo, string $section): Response public function getVariablesBySection(ConfigVariableRepository $repo, string $section): Response
{ {
if(\is_null($section) || $section == '') { 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); $varList = $repo->findAllBySection($section);
+20 -5
View File
@@ -2,6 +2,8 @@
namespace App\Controller; namespace App\Controller;
use App\ApiErrorException;
use App\DTO\ApiErrorMessage;
use App\Repository\FareTableRepository; use App\Repository\FareTableRepository;
use App\Repository\RoomAvailabilityRepository; use App\Repository\RoomAvailabilityRepository;
use App\Repository\RoomCategoryRepository; use App\Repository\RoomCategoryRepository;
@@ -53,11 +55,16 @@ class SearchRoomsController extends AbstractController
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest #[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
): Response { ): Response {
if (\is_null($searchRequest)) { 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) { 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); $rs = $repoAvailability->getAvailabilityForPeriod($searchRequest->startDate, $searchRequest->endDate);
@@ -118,16 +125,24 @@ class SearchRoomsController extends AbstractController
#[MapRequestPayload()] SearchPricesForCategoryRequest $searchRequest #[MapRequestPayload()] SearchPricesForCategoryRequest $searchRequest
): Response { ): Response {
if (\is_null($searchRequest)) { 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); $roomCategory = $repoCat->find($searchRequest->roomCategory);
if ($roomCategory === null) { 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) { 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); $fareList = $priceService->getFinalPricesForSearch($roomCategory, $searchRequest->startDate, $searchRequest->endDate);