feat: re-implement contact form submissions api endpoint for website
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\ContactFormSubmission;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Model\PersonalData;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[Route('/api')]
|
||||
#[IsGranted('ROLE_OAUTH2_API')]
|
||||
class ContactFormController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly ApiClient $apiClient)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/contactform', name: 'api_contacform', methods: ['POST'])]
|
||||
public function index(#[MapRequestPayload] ContactFormSubmission $dto): JsonResponse
|
||||
{
|
||||
$personalData = PersonalData::fromContactFormSubmission($dto);
|
||||
|
||||
try {
|
||||
$result = $this->apiClient->createAddress($personalData);
|
||||
|
||||
if ($result instanceof Notification) {
|
||||
return new JsonResponse([
|
||||
'success' => false,
|
||||
'message' => $result->message,
|
||||
], Response::HTTP_CONFLICT);
|
||||
}
|
||||
|
||||
return $this->json([
|
||||
'success' => true,
|
||||
'addressId' => $result->addressId,
|
||||
'personId' => $result->personId,
|
||||
'isNewRecord' => $result->isNewRecord,
|
||||
], Response::HTTP_CREATED);
|
||||
} catch (ApiClientException $e) {
|
||||
return new JsonResponse([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage(),
|
||||
], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\BusProNet\XmlLoader\HotelLoader;
|
||||
use App\Exception\HotelNotFoundException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -28,12 +29,12 @@ class HotelController extends AbstractController
|
||||
#[Route('/hotels/{id}', name: 'api_hotels_single')]
|
||||
public function single(int $id): JsonResponse
|
||||
{
|
||||
$hotel = $this->xmlLoader->loadById($id);
|
||||
try {
|
||||
$hotel = $this->xmlLoader->loadById($id);
|
||||
|
||||
if (null === $hotel) {
|
||||
return $this->json($hotel, Response::HTTP_OK, [], ['groups' => 'api:single']);
|
||||
} catch (HotelNotFoundException $e) {
|
||||
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return $this->json($hotel, Response::HTTP_OK, [], ['groups' => 'api:single']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user