forked from Somnia/SomniaRooms
API - Search Prices - Improvements
- Use config variables for API responses.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\ApiErrorException;
|
||||
use App\Consts\ConfigVariableConsts;
|
||||
use App\DTO\ApiErrorMessage;
|
||||
use App\Repository\RoomAvailabilityRepository;
|
||||
use App\Repository\RoomCategoryRepository;
|
||||
@@ -11,16 +12,28 @@ use App\Response\SearchAvailabilityResponse;
|
||||
use App\Request\SearchPricesForCategoryRequest;
|
||||
use App\Response\SearchPricesResponse;
|
||||
use App\Response\SearchPricesResponsePriceItem;
|
||||
use App\Service\ConfigVariableService;
|
||||
use App\Service\PricesService;
|
||||
use DateTimeInterface;
|
||||
use Nelmio\ApiDocBundle\Annotation\Model;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
|
||||
class SearchRoomsController extends AbstractController
|
||||
{
|
||||
private string $configMaxAllowedNightAmount;
|
||||
|
||||
public function __construct(
|
||||
private ConfigVariableService $configVarService,
|
||||
private LoggerInterface $logger
|
||||
) {
|
||||
$this->configMaxAllowedNightAmount = $configVarService->getVariableFromConst(ConfigVariableConsts::CONFIG_API_NAXNIGHTS)->getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get availability for all rooms on period.
|
||||
* For each room, the minimal availability will be returned.
|
||||
@@ -52,12 +65,11 @@ class SearchRoomsController extends AbstractController
|
||||
#[MapRequestPayload()] SearchPeriodRequest $searchRequest
|
||||
): JsonResponse {
|
||||
|
||||
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
||||
'startDate cant be before endDate',
|
||||
);
|
||||
$datesValid = $this->areRequestDatesValid($searchRequest->startDate, $searchRequest->endDate);
|
||||
if ($datesValid != null) {
|
||||
throw $datesValid;
|
||||
}
|
||||
|
||||
$rs = $repoAvailability->getAvailabilityForPeriod($searchRequest->startDate, $searchRequest->endDate);
|
||||
|
||||
return $this->json(["results" => $rs]);
|
||||
@@ -93,11 +105,10 @@ class SearchRoomsController extends AbstractController
|
||||
PricesService $priceService,
|
||||
#[MapRequestPayload()] SearchPeriodRequest $searchRequest
|
||||
): JsonResponse {
|
||||
if ($searchRequest->startDate >= $searchRequest->endDate) {
|
||||
throw new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
||||
'startDate cant be before endDate',
|
||||
);
|
||||
|
||||
$datesValid = $this->areRequestDatesValid($searchRequest->startDate, $searchRequest->endDate);
|
||||
if ($datesValid != null) {
|
||||
throw $datesValid;
|
||||
}
|
||||
|
||||
$rs = $priceService->getRoomSearch($searchRequest->startDate, $searchRequest->endDate);
|
||||
@@ -138,6 +149,11 @@ class SearchRoomsController extends AbstractController
|
||||
#[MapRequestPayload()] SearchPricesForCategoryRequest $searchRequest
|
||||
): JsonResponse {
|
||||
|
||||
$datesValid = $this->areRequestDatesValid($searchRequest->startDate, $searchRequest->endDate);
|
||||
if ($datesValid != null) {
|
||||
throw $datesValid;
|
||||
}
|
||||
|
||||
$roomCategory = $repoCat->find($searchRequest->roomCategory);
|
||||
if ($roomCategory === null) {
|
||||
throw new ApiErrorException(
|
||||
@@ -146,13 +162,6 @@ class SearchRoomsController extends AbstractController
|
||||
);
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -161,4 +170,25 @@ class SearchRoomsController extends AbstractController
|
||||
|
||||
return $this->json(["results" => $rs]);
|
||||
}
|
||||
|
||||
|
||||
private function areRequestDatesValid(DateTimeInterface $dateStart, DateTimeInterface $dateEnd): ApiErrorException|null
|
||||
{
|
||||
if ($dateStart >= $dateEnd) {
|
||||
return new ApiErrorException(
|
||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
||||
'startDate cant be before endDate',
|
||||
);
|
||||
}
|
||||
|
||||
$daysDiff = $dateStart->diff($dateEnd)->days;
|
||||
if ($daysDiff > $this->configMaxAllowedNightAmount) {
|
||||
return new ApiErrorException(
|
||||
new ApiErrorMessage(400, ApiErrorMessage::TYPE_API_LIMIT),
|
||||
'Query exceeded max allowed night amount',
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user