157 lines
5.5 KiB
PHP
157 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Repository\FareTableRepository;
|
|
use App\Repository\RoomAvailabilityRepository;
|
|
use App\Repository\RoomCategoryRepository;
|
|
use App\Request\SearchAvailabilityPeriodRequest;
|
|
use App\Response\SearchAvailabilityResponse;
|
|
use App\Request\SearchPricesForCategoryRequest;
|
|
use App\Response\SearchPricesForCategoryResponse;
|
|
use App\Service\PricesService;
|
|
use Nelmio\ApiDocBundle\Annotation\Model;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
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
|
|
{
|
|
/**
|
|
* 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"], format: "json")]
|
|
#[OA\Tag(name: "Searchs")]
|
|
#[OA\Response(
|
|
response: 200,
|
|
description: 'Each room available',
|
|
content: new OA\JsonContent(
|
|
type: 'object',
|
|
properties: [
|
|
new OA\Property(
|
|
property: "results",
|
|
type: "array",
|
|
items: new OA\Items(ref: new Model(type: SearchAvailabilityResponse::class))
|
|
)
|
|
]
|
|
)
|
|
)]
|
|
#[OA\RequestBody(
|
|
content: new OA\JsonContent(
|
|
type: 'object',
|
|
ref: new Model(type: SearchAvailabilityPeriodRequest::class),
|
|
)
|
|
)]
|
|
public function getAvailabilityForPeriod(
|
|
Request $request,
|
|
RoomAvailabilityRepository $repoAvailability,
|
|
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
|
|
): Response {
|
|
if (\is_null($searchRequest)) {
|
|
throw new HttpException('Invalid Payload', 400);
|
|
}
|
|
|
|
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]);
|
|
}
|
|
|
|
/**
|
|
* 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"], format: "json")]
|
|
#[OA\Tag(name: "Searchs")]
|
|
public function getAvailabilityForPeriodDetails(
|
|
Request $request,
|
|
RoomAvailabilityRepository $repoAvailability,
|
|
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
|
|
): Response {
|
|
if (\is_null($searchRequest)) {
|
|
throw new \Exception('Invalid Payload', 400);
|
|
}
|
|
|
|
$rs = $repoAvailability->getAvailabilityForPeriodDetails($searchRequest->startDate, $searchRequest->endDate);
|
|
|
|
return $this->json(["results" => $rs]);
|
|
}
|
|
|
|
/**
|
|
* Get room category price for given period.
|
|
* Returns price for a room category.
|
|
* This price is the total for all the selected days by guest number.
|
|
*/
|
|
#[Route('/api/search/prices', name: 'app_search_pricesForCategory', methods: ["POST"], format: "json")]
|
|
#[OA\Tag(name: "Searchs")]
|
|
#[OA\Response(
|
|
response: 200,
|
|
description: 'Room price by guest number',
|
|
content: new OA\JsonContent(
|
|
type: 'object',
|
|
properties: [
|
|
new OA\Property(
|
|
property: "results",
|
|
type: "array",
|
|
items: new OA\Items(ref: new Model(type: SearchPricesForCategoryResponse::class))
|
|
)
|
|
]
|
|
)
|
|
)]
|
|
#[OA\RequestBody(
|
|
content: new OA\JsonContent(
|
|
type: 'object',
|
|
ref: new Model(type: SearchPricesForCategoryRequest::class),
|
|
)
|
|
)]
|
|
public function getPricesForCategoryOnPeriod(
|
|
Request $request,
|
|
RoomCategoryRepository $repoCat,
|
|
PricesService $priceService,
|
|
#[MapRequestPayload()] SearchPricesForCategoryRequest $searchRequest
|
|
): Response {
|
|
if (\is_null($searchRequest)) {
|
|
throw new \Exception('Invalid Payload', 400);
|
|
}
|
|
|
|
$roomCategory = $repoCat->find($searchRequest->roomCategory);
|
|
if ($roomCategory === null) {
|
|
throw new \Exception('Invalid Category', 400);
|
|
}
|
|
|
|
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
|
throw new \Exception('Invalid time interval', 400);
|
|
}
|
|
|
|
$fareList = $priceService->getFinalPricesForSearch($roomCategory, $searchRequest->startDate, $searchRequest->endDate);
|
|
$rs = [];
|
|
foreach ($fareList as $key => $value) {
|
|
$rs[] = ["guestNumber" => $key, "price" => $value];
|
|
}
|
|
|
|
return $this->json(["results" => $rs]);
|
|
}
|
|
|
|
#[Route('/debug/search', name: 'app_search_faresDebug')]
|
|
public function faresDebug(Request $request, PricesService $priceService, FareTableRepository $repoFareTable, RoomCategoryRepository $repoCat): Response
|
|
{
|
|
$fareTable[] = $repoFareTable->find(1);
|
|
$fareTable[] = $repoFareTable->find(2);
|
|
$cat = $repoCat->find(4);
|
|
|
|
$startDate = new \DateTime("2024-04-27");
|
|
$endDate = new \DateTime("2024-05-02");
|
|
|
|
$rs = $priceService->getFinalPricesForSearch($cat, $startDate, $endDate);
|
|
|
|
return $this->json($rs);
|
|
}
|
|
}
|