feat: api endpoints

This commit is contained in:
Björn Fromme
2024-12-04 14:18:47 +01:00
parent 7379f5ac93
commit 231aaaac9b
26 changed files with 606 additions and 43 deletions
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Controller\Api;
use App\BusProNet\DataProvider\CountryDataProvider;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api')]
class CountryController extends AbstractController
{
public function __construct(private readonly CountryDataProvider $dataProvider)
{
}
#[Route(path: '/countries', name: 'api_country_all')]
public function index(): JsonResponse
{
$countries = $this->dataProvider->getAll();
return $this->json($countries);
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\Controller\Api;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\Notification;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api')]
class CrmAttributesController extends AbstractController
{
public function __construct(private readonly APiClient $apiClient)
{
}
#[Route('/crm-attributes', name: 'api_crm_attributes', methods: ['GET'])]
public function index(Request $request): JsonResponse
{
$email = $request->headers->get('x-bpn-email');
$password = $request->headers->get('x-bpn-password');
if (null === $email || null === $password) {
return new JsonResponse(['message' => 'Invalid credentials'], Response::HTTP_BAD_REQUEST);
}
try {
$data = $this->apiClient->getCrmAttributes($email, $password);
if ($data instanceof Notification) {
return new JsonResponse(['message' => $data->message, 'code' => $data->code], Response::HTTP_BAD_REQUEST);
}
return $this->json($data);
} catch (ApiClientException $e) {
return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
}
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace App\Controller\Api;
use App\BusProNet\XmlLoader\HotelXmlLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api')]
class HotelController extends AbstractController
{
public function __construct(private readonly HotelXmlLoader $xmlLoader)
{
}
#[Route('/hotels', name: 'api_hotel_all')]
public function index(): JsonResponse
{
$hotels = $this->xmlLoader->loadAll();
return $this->json($hotels, Response::HTTP_OK, [], ['groups' => 'api:list']);
}
#[Route('/hotels/{id}', name: 'api_hotels_single')]
public function single(int $id): JsonResponse
{
$hotel = $this->xmlLoader->loadById($id);
if (null === $hotel) {
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
}
return $this->json($hotel, Response::HTTP_OK, [], ['groups' => 'api:single']);
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\Controller\Api;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\Notification;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api')]
class PersonalDataController extends AbstractController
{
public function __construct(private readonly APiClient $apiClient)
{
}
#[Route('/personal-data', name: 'api_personal_data', methods: ['GET'])]
public function index(Request $request): JsonResponse
{
$email = $request->headers->get('x-bpn-email');
$password = $request->headers->get('x-bpn-password');
if (null === $email || null === $password) {
return new JsonResponse(['message' => 'Invalid credentials'], Response::HTTP_BAD_REQUEST);
}
try {
$data = $this->apiClient->getPersonalData($email, $password);
if ($data instanceof Notification) {
return new JsonResponse(['message' => $data->message, 'code' => $data->code], Response::HTTP_BAD_REQUEST);
}
return $this->json($data);
} catch (ApiClientException $e) {
return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
}
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Controller\Api;
use App\BusProNet\XmlLoader\PickupXmlLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api')]
class PickupController extends AbstractController
{
public function __construct(private readonly PickupXmlLoader $xmlLoader)
{
}
#[Route('/pickups', name: 'api_pickup_all')]
public function index(): JsonResponse
{
$pickups = $this->xmlLoader->loadAll();
return $this->json($pickups, Response::HTTP_OK, [], ['groups' => 'api:list']);
}
#[Route('/pickups/{id}', name: 'api_pickup_single')]
public function single(int $id): JsonResponse
{
$pickup = $this->xmlLoader->loadById($id);
return $this->json($pickup, Response::HTTP_OK, [], ['groups' => 'api:list']);
}
}
@@ -0,0 +1,47 @@
<?php
namespace App\Controller\Api;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Validator\ValidatorInterface;
#[Route('/api')]
class ResetPasswordController extends AbstractController
{
public function __construct(private readonly ApiClient $apiClient, private readonly ValidatorInterface $validator)
{
}
#[Route('/reset-password', name: 'api_reset_password', methods: ['POST'])]
public function index(Request $request): Response
{
$payload = json_decode($request->getContent(), true);
$email = $payload['email'] ?? null;
if (null === $email) {
return new JsonResponse(['message' => 'Invalid email'], Response::HTTP_BAD_REQUEST);
}
$violationList = $this->validator->validate($email, [new Email([
'mode' => 'strict',
])]);
if (0 < count($violationList)) {
return new JsonResponse(['message' => 'Invalid email'], Response::HTTP_BAD_REQUEST);
}
try {
$this->apiClient->resetPassword($email);
} catch (ApiClientException $e) {
}
return $this->json(['message' => 'Password reset invoked']);
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace App\Controller\Api;
use App\BusProNet\XmlLoader\HotelXmlLoader;
use App\BusProNet\XmlLoader\PickupXmlLoader;
use App\BusProNet\XmlLoader\TravelXmlLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
#[Route('/api')]
class TravelController extends AbstractController
{
public function __construct(
private readonly TravelXmlLoader $travelXmlLoader,
private readonly HotelXmlLoader $hotelXmlLoader,
private readonly PickupXmlLoader $pickupXmlLoader,
private readonly CacheInterface $cache,
) {
}
#[Route(path: '/travels', name: 'api_travel_mapping')]
public function mapping(): JsonResponse
{
$mapping = $this->travelXmlLoader->generateMapping();
return $this->json($mapping);
}
#[Route(
path: '/travels/{travelId}/{hotelId}',
name: 'api_travel_single',
requirements: ['travelId' => '\d+', 'hotelId' => '\d+']
)]
public function single(int $travelId, ?int $hotelId = null): JsonResponse
{
$cacheKey = sprintf('bpn_travel_%d_%d', $travelId, $hotelId ?? 0);
$travel = $this->cache->get($cacheKey, function (ItemInterface $item) use ($travelId, $hotelId) {
$item->expiresAfter(60);
$travel = $this->travelXmlLoader->loadById($travelId, $hotelId);
if (null === $travel) {
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
}
$this->pickupXmlLoader->patchPickupsDetails($travel);
$this->hotelXmlLoader->patchHotelDetails($travel);
return $travel;
});
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
}
}
+7 -7
View File
@@ -3,8 +3,8 @@
namespace App\Controller\Booking;
use App\BusProNet\ApiClient;
use App\BusProNet\DataLoader\PickupDataLoader;
use App\BusProNet\DataLoader\TravelDataLoader;
use App\BusProNet\XmlLoader\PickupXmlLoader;
use App\BusProNet\XmlLoader\TravelXmlLoader;
use App\Form\BookingType;
use App\Form\Model\BookingData;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -19,11 +19,11 @@ use Symfony\Contracts\Cache\ItemInterface;
class EditController extends AbstractController
{
public function __construct(
private readonly ApiClient $apiClient,
private readonly TravelDataLoader $travelDataLoader,
private readonly PickupDataLoader $pickupDataLoader,
private readonly CacheInterface $cache,
private readonly Security $security,
private readonly ApiClient $apiClient,
private readonly TravelXmlLoader $travelDataLoader,
private readonly PickupXmlLoader $pickupDataLoader,
private readonly CacheInterface $cache,
private readonly Security $security,
) {
}
+5 -5
View File
@@ -3,7 +3,7 @@
namespace App\Controller\Booking;
use App\BusProNet\ApiClient;
use App\BusProNet\DataLoader\TravelDataLoader;
use App\BusProNet\XmlLoader\TravelXmlLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
@@ -16,10 +16,10 @@ use Symfony\Contracts\Cache\ItemInterface;
class IndexController extends AbstractController
{
public function __construct(
private readonly ApiClient $apiClient,
private readonly TravelDataLoader $travelDataLoader,
private readonly CacheInterface $cache,
private readonly Security $security,
private readonly ApiClient $apiClient,
private readonly TravelXmlLoader $travelDataLoader,
private readonly CacheInterface $cache,
private readonly Security $security,
) {
}