feat: API endpoint to load travel data by code

This commit is contained in:
Björn Fromme
2025-01-14 12:41:40 +01:00
parent b196825ab3
commit c96b525aa4
6 changed files with 82 additions and 21 deletions
+9
View File
@@ -2,10 +2,19 @@
namespace App\BusProNet\Model; namespace App\BusProNet\Model;
use Symfony\Component\Serializer\Attribute\Groups;
class CrmSelection class CrmSelection
{ {
#[Groups(['api:single'])]
public ?int $id = null; public ?int $id = null;
#[Groups(['api:single'])]
public ?string $label = null; public ?string $label = null;
#[Groups(['api:single'])]
public bool $mutable = false; public bool $mutable = false;
#[Groups(['api:single'])]
public bool $selected = false; public bool $selected = false;
} }
@@ -2,6 +2,8 @@
namespace App\BusProNet\Model; namespace App\BusProNet\Model;
use Symfony\Component\Serializer\Attribute\Groups;
class CrmSelectionGroup class CrmSelectionGroup
{ {
/** /**
@@ -16,8 +18,13 @@ class CrmSelectionGroup
86 => 'Programm', 86 => 'Programm',
]; ];
#[Groups(['api:single'])]
public ?int $id = null; public ?int $id = null;
#[Groups(['api:single'])]
public ?string $label = null; public ?string $label = null;
#[Groups(['api:single'])]
public ?array $selections = null; public ?array $selections = null;
public function getMutableSelections(): array public function getMutableSelections(): array
@@ -27,6 +34,7 @@ class CrmSelectionGroup
}); });
} }
#[Groups(['api:single'])]
public function isVisible(): bool public function isVisible(): bool
{ {
return 0 < count($this->getMutableSelections()); return 0 < count($this->getMutableSelections());
+1 -1
View File
@@ -49,7 +49,7 @@ class Room
#[Groups(['api:list', 'api:single'])] #[Groups(['api:list', 'api:single'])]
public ?int $available = null; public ?int $available = null;
#[Groups(['api:list', 'api:single'])] #[Groups(['booking'])]
public ?string $board = null; public ?string $board = null;
#[Groups(['booking'])] #[Groups(['booking'])]
+13 -13
View File
@@ -31,6 +31,19 @@ class HotelXmlLoader extends AbstractXmlLoader
} }
} }
public function mapCodeToId(string $hotelCode, ?string $filename = 'hotel.xml'): ?int
{
$hotels = $this->loadAll($filename);
foreach ($hotels as $hotel) {
if ($hotelCode === $hotel->code) {
return $hotel->id;
}
}
return null;
}
public function loadById(int $id, ?string $filename = 'hotel.xml'): ?Hotel public function loadById(int $id, ?string $filename = 'hotel.xml'): ?Hotel
{ {
$hotels = $this->loadAll($filename); $hotels = $this->loadAll($filename);
@@ -38,19 +51,6 @@ class HotelXmlLoader extends AbstractXmlLoader
return $hotels[$id] ?? null; return $hotels[$id] ?? null;
} }
public function loadByCode(string $code, ?string $filename = 'hotel.xml'): ?Hotel
{
$hotels = $this->loadAll($filename);
foreach ($hotels as $hotel) {
if ($code === $hotel->code) {
return $hotel;
}
}
return null;
}
private function loadXml(?string $filename = 'hotel.xml'): \SimpleXMLElement private function loadXml(?string $filename = 'hotel.xml'): \SimpleXMLElement
{ {
$filepath = $this->xmlPath.'/'.$filename; $filepath = $this->xmlPath.'/'.$filename;
+18 -3
View File
@@ -25,7 +25,7 @@ class TravelXmlLoader extends AbstractXmlLoader
parent::__construct($cache, $xmlPath); parent::__construct($cache, $xmlPath);
} }
public function generateMapping(): array public function generateFilesMap(): array
{ {
return $this->cache->get('bpn_travels_mapping', function (ItemInterface $item) { return $this->cache->get('bpn_travels_mapping', function (ItemInterface $item) {
$item->expiresAfter(3600); $item->expiresAfter(3600);
@@ -46,6 +46,7 @@ class TravelXmlLoader extends AbstractXmlLoader
$mapping[$travelId] = [ $mapping[$travelId] = [
'id' => (int) $attributes['idbuspro'], 'id' => (int) $attributes['idbuspro'],
'code' => (string) $attributes['code'],
'label' => (string) $travelXml->text, 'label' => (string) $travelXml->text,
'dateFrom' => $this->stringToDate((string) $attributes['termin']), 'dateFrom' => $this->stringToDate((string) $attributes['termin']),
'dateTo' => $this->stringToDate((string) $attributes['bis']), 'dateTo' => $this->stringToDate((string) $attributes['bis']),
@@ -58,6 +59,7 @@ class TravelXmlLoader extends AbstractXmlLoader
$mapping[$travelId]['hotels'][$hotelId] = [ $mapping[$travelId]['hotels'][$hotelId] = [
'id' => $hotelId, 'id' => $hotelId,
'code' => (string) $hotelXml->attributes()['code'],
'label' => $hotels[$hotelId]->name, 'label' => $hotels[$hotelId]->name,
]; ];
} }
@@ -68,13 +70,26 @@ class TravelXmlLoader extends AbstractXmlLoader
}); });
} }
public function mapCodeToId(string $travelCode): ?int
{
$mapping = $this->generateFilesMap();
$travelCodes = array_column($mapping, 'code', 'id');
if (false === $travelId = array_search($travelCode, $travelCodes)) {
return null;
}
return $travelId;
}
public function loadById(int $travelId, ?int $hotelId = null, ?string $filename = null): ?Travel public function loadById(int $travelId, ?int $hotelId = null, ?string $filename = null): ?Travel
{ {
if (null !== $filename) { if (null !== $filename) {
return $this->loadXml($travelId, $hotelId, $filename); return $this->loadXml($travelId, $hotelId, $filename);
} }
$mapping = $this->generateMapping(); $mapping = $this->generateFilesMap();
if (false === isset($mapping[$travelId])) { if (false === isset($mapping[$travelId])) {
return null; return null;
@@ -107,7 +122,7 @@ class TravelXmlLoader extends AbstractXmlLoader
$travel = new Travel(); $travel = new Travel();
$travel->id = (int) $attributes['idbuspro']; $travel->id = (int) $attributes['idbuspro'];
$travel->label = (string) $xml->text; $travel->label = (string) $xml->text;
$travel->hotelId = $hotelId; $travel->hotelId = (int) $hotelXml->attributes()['idbuspro'];
$travel->dateFrom = $this->stringToDate((string) $attributes['termin']); $travel->dateFrom = $this->stringToDate((string) $attributes['termin']);
$travel->dateTo = $this->stringToDate((string) $attributes['bis']); $travel->dateTo = $this->stringToDate((string) $attributes['bis']);
$travel->code = (string) $attributes['code']; $travel->code = (string) $attributes['code'];
+33 -4
View File
@@ -2,6 +2,7 @@
namespace App\Controller\Api; namespace App\Controller\Api;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\HotelXmlLoader; use App\BusProNet\XmlLoader\HotelXmlLoader;
use App\BusProNet\XmlLoader\PickupXmlLoader; use App\BusProNet\XmlLoader\PickupXmlLoader;
use App\BusProNet\XmlLoader\TravelXmlLoader; use App\BusProNet\XmlLoader\TravelXmlLoader;
@@ -27,19 +28,47 @@ class TravelController extends AbstractController
#[Route(path: '/travels', name: 'api_travel_mapping')] #[Route(path: '/travels', name: 'api_travel_mapping')]
public function mapping(): JsonResponse public function mapping(): JsonResponse
{ {
$mapping = $this->travelXmlLoader->generateMapping(); $mapping = $this->travelXmlLoader->generateFilesMap();
return $this->json($mapping); return $this->json($mapping);
} }
#[Route( #[Route(
path: '/travels/{travelId}/{hotelId}', path: '/travels/{travelId}/{hotelId}',
name: 'api_travel_single', name: 'api_travel_single_id',
requirements: ['travelId' => '\d+', 'hotelId' => '\d+'] 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); $cacheKey = sprintf('bpn_travel_%d_%d', $travelId, $hotelId ?? 0);
try { try {
$travel = $this->cache->get($cacheKey, function (ItemInterface $item) use ($travelId, $hotelId) { $travel = $this->cache->get($cacheKey, function (ItemInterface $item) use ($travelId, $hotelId) {
$item->expiresAfter(60); $item->expiresAfter(60);
@@ -59,6 +88,6 @@ class TravelController extends AbstractController
$travel = null; $travel = null;
} }
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]); return $travel;
} }
} }