This commit is contained in:
@@ -20,13 +20,14 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
|
||||
class BookingController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Get booking details
|
||||
*/
|
||||
#[Route('/api/booking/details', name: 'app_booking_details', methods: ["POST"])]
|
||||
#[Route('/api/booking/details', name: 'app_booking_details', methods: ["POST"], format: "json")]
|
||||
#[OA\Tag(name: "Booking")]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
@@ -42,23 +43,22 @@ class BookingController extends AbstractController
|
||||
ref: new Model(type: BookingDetailsRequest::class),
|
||||
)
|
||||
)]
|
||||
public function getBookingDetails(Request $request, BookingRepository $repoBooking): JsonResponse
|
||||
{
|
||||
$payload = $request->toArray();
|
||||
if (\is_null($payload)) {
|
||||
public function getBookingDetails(
|
||||
BookingRepository $repoBooking,
|
||||
#[MapRequestPayload()] BookingDetailsRequest $detailsRequest
|
||||
): JsonResponse {
|
||||
|
||||
if (\is_null($detailsRequest)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
}
|
||||
|
||||
$refNumber = $payload["refNumber"];
|
||||
$customerEmail = $payload["email"];
|
||||
|
||||
$bookingList = $repoBooking->findBy(["refNumber" => $refNumber]);
|
||||
$bookingList = $repoBooking->findBy(["refNumber" => $detailsRequest->refNumber]);
|
||||
if (\is_null($bookingList) || \count($bookingList) == 0) {
|
||||
throw new HttpException(404, 'Invalid Booking');
|
||||
}
|
||||
|
||||
$customerBooking = $bookingList[0];
|
||||
if ($customerBooking->getBookingCustomer()->getEmail() != $customerEmail) {
|
||||
if ($customerBooking->getBookingCustomer()->getEmail() != $detailsRequest->email) {
|
||||
throw new HttpException(404, 'Invalid Booking');
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ class BookingController extends AbstractController
|
||||
/**
|
||||
* Create a new Booking.
|
||||
*/
|
||||
#[Route('/api/booking/create', name: 'app_booking_create', methods: ["POST"])]
|
||||
#[Route('/api/booking/create', name: 'app_booking_create', methods: ["POST"], format: "json")]
|
||||
#[OA\Tag(name: "Booking")]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
@@ -84,25 +84,21 @@ class BookingController extends AbstractController
|
||||
ref: new Model(type: BookingCreateRequest::class),
|
||||
)
|
||||
)]
|
||||
public function newBooking(Request $request, BookingRepository $repoBooking, EntityManagerInterface $entityManager,
|
||||
RoomCategoryRepository $repoCat, RoomAvailabilityRepository $repoAvailability,
|
||||
PricesService $priceSrv): JsonResponse
|
||||
{
|
||||
$payload = $request->toArray();
|
||||
if (\is_null($payload)) {
|
||||
public function newBooking(
|
||||
Request $request,
|
||||
EntityManagerInterface $entityManager,
|
||||
RoomCategoryRepository $repoCat,
|
||||
RoomAvailabilityRepository $repoAvailability,
|
||||
PricesService $priceSrv,
|
||||
#[MapRequestPayload()] BookingCreateRequest $createRequest
|
||||
): JsonResponse {
|
||||
if (\is_null($createRequest)) {
|
||||
throw new HttpException('Invalid Payload', 400);
|
||||
}
|
||||
|
||||
|
||||
$booking = new Booking();
|
||||
try {
|
||||
$reqArrivalDate = $payload["arrivalDate"];
|
||||
$reqDepartureDate = $payload["departureDate"];
|
||||
|
||||
$arrivalDate = new \DateTime($reqArrivalDate);
|
||||
$departureDate = new \DateTime($reqDepartureDate);
|
||||
|
||||
if ($arrivalDate >= $departureDate) {
|
||||
if ($createRequest->arrivalDate >= $createRequest->departureDate) {
|
||||
throw new HttpException(400, "Arrival cant be before departure");
|
||||
}
|
||||
|
||||
@@ -112,9 +108,11 @@ class BookingController extends AbstractController
|
||||
|
||||
// Create booking from request data.
|
||||
$booking->setBookingTime($currentTime);
|
||||
$booking->setArrivalDate($arrivalDate);
|
||||
$booking->setDepartureDate($departureDate);
|
||||
$booking->setArrivalDate($createRequest->arrivalDate);
|
||||
$booking->setDepartureDate($createRequest->departureDate);
|
||||
$booking->setBookingCustomer($createRequest->customerDetails);
|
||||
|
||||
//TODOME: Improve refNumber generation.
|
||||
$highest_id = $entityManager->createQueryBuilder()
|
||||
->select('MAX(e.id)')
|
||||
->from('App\Entity\Booking', 'e')
|
||||
@@ -125,39 +123,27 @@ class BookingController extends AbstractController
|
||||
|
||||
$entityManager->persist($booking);
|
||||
|
||||
// Create booking customer.
|
||||
$reqCustomer = $payload["customerDetails"];
|
||||
$bookingCustomer = new BookingCustomer();
|
||||
$bookingCustomer->setName($reqCustomer["name"]);
|
||||
$bookingCustomer->setSurname($reqCustomer["surname"]);
|
||||
$bookingCustomer->setEmail($reqCustomer["email"]);
|
||||
$bookingCustomer->setPhoneNumber($reqCustomer["phoneNumber"]);
|
||||
$bookingCustomer->setBooking($booking);
|
||||
|
||||
$entityManager->persist($bookingCustomer);
|
||||
|
||||
$reqRoomList = $payload["rooms"];
|
||||
if (\count($reqRoomList) == 0) {
|
||||
if (\count($createRequest->rooms) == 0) {
|
||||
throw new HttpException(400, "Se debe indicar al menos una habitación");
|
||||
}
|
||||
|
||||
// Create each room.
|
||||
foreach ($reqRoomList as $roomItem) {
|
||||
foreach ($createRequest->rooms as $roomItem) {
|
||||
$room = new BookingRoom();
|
||||
$room->setBooking($booking);
|
||||
|
||||
//Get Room category.
|
||||
$roomCategory = $repoCat->find($roomItem["roomCategory"]);
|
||||
$roomCategory = $repoCat->find($roomItem->roomCategory);
|
||||
if ($roomCategory == null) {
|
||||
throw new HttpException(400, "La RoomCategory no es válida.");
|
||||
}
|
||||
|
||||
//Check room availability.
|
||||
$amountAvailable = $repoAvailability->getAvailabilityForCategoryInPeriod($roomCategory, $arrivalDate, $departureDate);
|
||||
$selectedAmount = $roomItem['amount'];
|
||||
$selectedGuestAmount = $roomItem["guestNumber"];
|
||||
$amountAvailable = $repoAvailability->getAvailabilityForCategoryInPeriod($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate);
|
||||
$selectedAmount = $roomItem->amount;
|
||||
$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.");
|
||||
}
|
||||
|
||||
@@ -166,17 +152,17 @@ class BookingController extends AbstractController
|
||||
$room->setAmount($selectedAmount);
|
||||
|
||||
//Get Price for period.
|
||||
$priceList = $priceSrv->getFinalPricesForSearch($roomCategory, $arrivalDate, $departureDate);
|
||||
$priceList = $priceSrv->getFinalPricesForSearch($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate);
|
||||
$priceFound = false;
|
||||
foreach ($priceList as $key => $value) {
|
||||
if($key == $selectedGuestAmount) {
|
||||
if ($key == $selectedGuestAmount) {
|
||||
$room->setRoomPrice($value);
|
||||
$priceFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!$priceFound) {
|
||||
if (!$priceFound) {
|
||||
throw new HttpException(
|
||||
500,
|
||||
"No se puedo obtener el precio para la categoría: " . $roomCategory->getId() . " , guests: " . $selectedGuestAmount
|
||||
@@ -186,12 +172,11 @@ class BookingController extends AbstractController
|
||||
$entityManager->persist($room);
|
||||
|
||||
//If booking is nice, let's update current availability.
|
||||
$repoAvailability->updateAvailabilityForRoomCategoryOnPeriod($roomCategory, $arrivalDate, $departureDate, $selectedAmount, true); //Update using "diff" mode.
|
||||
$repoAvailability->updateAvailabilityForRoomCategoryOnPeriod($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate, $selectedAmount, true); //Update using "diff" mode.
|
||||
}
|
||||
|
||||
$entityManager->commit();
|
||||
$entityManager->flush();
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
$entityManager->rollback();
|
||||
throw $th;
|
||||
|
||||
@@ -16,6 +16,8 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class SearchRoomsController extends AbstractController
|
||||
{
|
||||
@@ -23,7 +25,7 @@ class SearchRoomsController extends AbstractController
|
||||
* Get availability for all rooms on period.
|
||||
* For each room, the minimal availability will be returned.
|
||||
*/
|
||||
#[Route('/api/search/availability', name: 'app_search_availability', methods: ["POST"])]
|
||||
#[Route('/api/search/availability', name: 'app_search_availability', methods: ["POST"], format: "json")]
|
||||
#[OA\Tag(name: "Searchs")]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
@@ -42,26 +44,22 @@ class SearchRoomsController extends AbstractController
|
||||
#[OA\RequestBody(
|
||||
content: new OA\JsonContent(
|
||||
type: 'object',
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: "filter",
|
||||
ref: new Model(type: SearchAvailabilityPeriodRequest::class),
|
||||
),
|
||||
],
|
||||
)
|
||||
)]
|
||||
public function getAvailabilityForPeriod(Request $request, RoomAvailabilityRepository $repoAvailability): Response
|
||||
{
|
||||
$payload = $request->toArray();
|
||||
if (\is_null($payload)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
public function getAvailabilityForPeriod(
|
||||
Request $request,
|
||||
RoomAvailabilityRepository $repoAvailability,
|
||||
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
|
||||
): Response {
|
||||
if (\is_null($searchRequest)) {
|
||||
throw new HttpException('Invalid Payload', 400);
|
||||
}
|
||||
$filter = $payload["filter"];
|
||||
|
||||
$startDate = new \DateTime($filter["startDate"]);
|
||||
$endDate = new \DateTime($filter["endDate"]);
|
||||
|
||||
$rs = $repoAvailability->getAvailabilityForPeriod($startDate, $endDate);
|
||||
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
||||
throw new HttpException(400, "startDate cant be before endDate");
|
||||
}
|
||||
$rs = $repoAvailability->getAvailabilityForPeriod($searchRequest->startDate, $searchRequest->endDate);
|
||||
|
||||
return $this->json(["results" => $rs]);
|
||||
}
|
||||
@@ -70,20 +68,18 @@ class SearchRoomsController extends AbstractController
|
||||
* Get Availability for all rooms on each day of a period.
|
||||
* (Not used on Front-end APP)
|
||||
*/
|
||||
#[Route('/api/search/availabilityDetails', name: 'app_search_availabilityDetails', methods: ["POST"])]
|
||||
#[Route('/api/search/availabilityDetails', name: 'app_search_availabilityDetails', methods: ["POST"], format: "json")]
|
||||
#[OA\Tag(name: "Searchs")]
|
||||
public function getAvailabilityForPeriodDetails(Request $request, RoomAvailabilityRepository $repoAvailability): Response
|
||||
{
|
||||
$payload = $request->toArray();
|
||||
if (\is_null($payload)) {
|
||||
public function getAvailabilityForPeriodDetails(
|
||||
Request $request,
|
||||
RoomAvailabilityRepository $repoAvailability,
|
||||
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
|
||||
): Response {
|
||||
if (\is_null($searchRequest)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
}
|
||||
$filter = $payload["filter"];
|
||||
|
||||
$startDate = new \DateTime($filter["startDate"]);
|
||||
$endDate = new \DateTime($filter["endDate"]);
|
||||
|
||||
$rs = $repoAvailability->getAvailabilityForPeriodDetails($startDate, $endDate);
|
||||
$rs = $repoAvailability->getAvailabilityForPeriodDetails($searchRequest->startDate, $searchRequest->endDate);
|
||||
|
||||
return $this->json(["results" => $rs]);
|
||||
}
|
||||
@@ -93,7 +89,7 @@ class SearchRoomsController extends AbstractController
|
||||
* Returns price for a room category.
|
||||
* This price is the total for all the selected days by guest number.
|
||||
*/
|
||||
#[Route('/api/search/fares', name: 'app_search_faresForCategory', methods: ["POST"])]
|
||||
#[Route('/api/search/fares', name: 'app_search_faresForCategory', methods: ["POST"], format: "json")]
|
||||
#[OA\Tag(name: "Searchs")]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
@@ -112,37 +108,29 @@ class SearchRoomsController extends AbstractController
|
||||
#[OA\RequestBody(
|
||||
content: new OA\JsonContent(
|
||||
type: 'object',
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: "filter",
|
||||
ref: new Model(type: SearchFareCategoryRequest::class),
|
||||
),
|
||||
],
|
||||
)
|
||||
)]
|
||||
public function getFaresForCategoryOnPeriod(Request $request, RoomCategoryRepository $repoCat, PricesService $priceService): Response
|
||||
{
|
||||
$payload = $request->toArray();
|
||||
if (\is_null($payload)) {
|
||||
public function getFaresForCategoryOnPeriod(
|
||||
Request $request,
|
||||
RoomCategoryRepository $repoCat,
|
||||
PricesService $priceService,
|
||||
#[MapRequestPayload()] SearchFareCategoryRequest $searchRequest
|
||||
): Response {
|
||||
if (\is_null($searchRequest)) {
|
||||
throw new \Exception('Invalid Payload', 400);
|
||||
}
|
||||
$filter = $payload["filter"];
|
||||
|
||||
$catId = $filter["roomCategory"];
|
||||
$roomCategory = $repoCat->find($catId);
|
||||
$roomCategory = $repoCat->find($searchRequest->roomCategory);
|
||||
if ($roomCategory === null) {
|
||||
throw new \Exception('Invalid Category', 400);
|
||||
}
|
||||
|
||||
$startDate = new \DateTime($filter["startDate"]);
|
||||
$endDate = new \DateTime($filter["endDate"]);
|
||||
|
||||
$daysDiff = $startDate->diff($endDate)->days;
|
||||
if (!$daysDiff || $startDate > $endDate) {
|
||||
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
||||
throw new \Exception('Invalid time interval', 400);
|
||||
}
|
||||
|
||||
$fareList = $priceService->getFinalPricesForSearch($roomCategory, $startDate, $endDate);
|
||||
$fareList = $priceService->getFinalPricesForSearch($roomCategory, $searchRequest->startDate, $searchRequest->endDate);
|
||||
$rs = [];
|
||||
foreach ($fareList as $key => $value) {
|
||||
$rs[] = ["guestNumber" => $key, "price" => $value];
|
||||
|
||||
@@ -19,7 +19,7 @@ class BookingCreateRequest
|
||||
public BookingCustomer $customerDetails;
|
||||
|
||||
/**
|
||||
* @var array<BookingCreateRoomItem>
|
||||
* @var BookingCreateRoomItem[]
|
||||
*/
|
||||
#[OA\Property(
|
||||
description: 'Rooms to book',
|
||||
|
||||
@@ -7,7 +7,7 @@ use OpenApi\Attributes as OA;
|
||||
class BookingCreateRoomItem
|
||||
{
|
||||
#[OA\Property(description: 'Room category ID')]
|
||||
public string $roomCategory;
|
||||
public int $roomCategory;
|
||||
|
||||
#[OA\Property(description: 'Guest number for the room')]
|
||||
public int $guestNumber;
|
||||
|
||||
Reference in New Issue
Block a user