feat: API endpoint to load travel data by code
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\BusProNet\XmlLoader\HotelXmlLoader;
|
||||
use App\BusProNet\XmlLoader\PickupXmlLoader;
|
||||
use App\BusProNet\XmlLoader\TravelXmlLoader;
|
||||
@@ -27,19 +28,47 @@ class TravelController extends AbstractController
|
||||
#[Route(path: '/travels', name: 'api_travel_mapping')]
|
||||
public function mapping(): JsonResponse
|
||||
{
|
||||
$mapping = $this->travelXmlLoader->generateMapping();
|
||||
$mapping = $this->travelXmlLoader->generateFilesMap();
|
||||
|
||||
return $this->json($mapping);
|
||||
}
|
||||
|
||||
#[Route(
|
||||
path: '/travels/{travelId}/{hotelId}',
|
||||
name: 'api_travel_single',
|
||||
name: 'api_travel_single_id',
|
||||
requirements: ['travelId' => '\d+', 'hotelId' => '\d+']
|
||||
)]
|
||||
public function single(int $travelId, ?int $hotelId = null): JsonResponse
|
||||
public function singleById(int $travelId, ?int $hotelId = null): JsonResponse
|
||||
{
|
||||
$travel = $this->loadCached($travelId, $hotelId);
|
||||
|
||||
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
|
||||
}
|
||||
|
||||
#[Route(
|
||||
path: '/travels/{travelCode}/{hotelCode}',
|
||||
name: 'api_travel_single_code',
|
||||
)]
|
||||
public function singleByCode(string $travelCode, ?string $hotelCode = null): JsonResponse
|
||||
{
|
||||
$travelCode = str_replace('-', '/', $travelCode);
|
||||
|
||||
$travelId = $this->travelXmlLoader->mapCodeToId($travelCode);
|
||||
$hotelId = $hotelCode ? $this->hotelXmlLoader->mapCodeToId($travelCode) : null;
|
||||
|
||||
if (null === $travelId) {
|
||||
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$travel = $this->loadCached($travelId, $hotelId);
|
||||
|
||||
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
|
||||
}
|
||||
|
||||
private function loadCached(int $travelId, ?int $hotelId = null): ?Travel
|
||||
{
|
||||
$cacheKey = sprintf('bpn_travel_%d_%d', $travelId, $hotelId ?? 0);
|
||||
|
||||
try {
|
||||
$travel = $this->cache->get($cacheKey, function (ItemInterface $item) use ($travelId, $hotelId) {
|
||||
$item->expiresAfter(60);
|
||||
@@ -59,6 +88,6 @@ class TravelController extends AbstractController
|
||||
$travel = null;
|
||||
}
|
||||
|
||||
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
|
||||
return $travel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user