CI / Docker Lint (push) Successful in 21s
- Added Symfony Validation Lib. - Updated API DTOs to include validation constraints.
138 lines
4.9 KiB
PHP
138 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\ApiErrorException;
|
|
use App\DTO\ApiErrorMessage;
|
|
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\Routing\Attribute\Route;
|
|
use OpenApi\Attributes as OA;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
|
|
|
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(
|
|
RoomAvailabilityRepository $repoAvailability,
|
|
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
|
|
): JsonResponse {
|
|
|
|
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
|
throw new ApiErrorException(
|
|
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
|
'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(
|
|
RoomAvailabilityRepository $repoAvailability,
|
|
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
|
|
): JsonResponse {
|
|
|
|
$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(
|
|
RoomCategoryRepository $repoCat,
|
|
PricesService $priceService,
|
|
#[MapRequestPayload()] SearchPricesForCategoryRequest $searchRequest
|
|
): JsonResponse {
|
|
|
|
$roomCategory = $repoCat->find($searchRequest->roomCategory);
|
|
if ($roomCategory === null) {
|
|
throw new ApiErrorException(
|
|
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
|
|
'Invalid RoomCategory: ' . $searchRequest->roomCategory,
|
|
);
|
|
}
|
|
|
|
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
|
throw new ApiErrorException(
|
|
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
|
'startDate cant be before endDate',
|
|
);
|
|
}
|
|
|
|
$fareList = $priceService->getFinalPricesForSearch($roomCategory, $searchRequest->startDate, $searchRequest->endDate);
|
|
$rs = [];
|
|
foreach ($fareList as $key => $value) {
|
|
$rs[] = ["guestNumber" => $key, "price" => $value];
|
|
}
|
|
|
|
return $this->json(["results" => $rs]);
|
|
}
|
|
}
|