Files
myep/src/Controller/Api/AccommodationBookingController.php
T

63 lines
2.3 KiB
PHP

<?php
namespace App\Controller\Api;
use App\Entity\Groups\AccommodationBooking;
use App\Model\AccommodationBookingApiResponse;
use App\Repository\Groups\AccommodationBookingRepository;
use App\Service\AccommodationBookingBreakdownCalculator;
use App\Service\AccommodationBookingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;
#[Route('/api')]
#[IsGranted('ROLE_OAUTH2_API')]
class AccommodationBookingController extends AbstractController
{
public function __construct(
private readonly AccommodationBookingRepository $bookingRepository,
private readonly AccommodationBookingBreakdownCalculator $breakdownCalculator,
private readonly SerializerInterface $serializer,
private readonly AccommodationBookingService $bookingService,
) {
}
#[Route(path: '/accommodation-bookings/{uuid}', name: 'api_accommodation_bookings_single', methods: ['GET'])]
public function single(string $uuid): JsonResponse
{
$booking = $this->bookingRepository->findOneBy(['uuid' => $uuid]);
if (null === $booking) {
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
}
return $this->bookingResponse($booking);
}
#[Route(path: '/accommodation-bookings/{uuid}/accept', name: 'api_accommodation_bookings_accept', methods: ['POST'])]
public function accept(string $uuid): JsonResponse
{
$booking = $this->bookingRepository->findOneBy(['uuid' => $uuid]);
if (null === $booking) {
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
}
$this->bookingService->acceptBooking($booking);
return $this->bookingResponse($booking);
}
private function bookingResponse(AccommodationBooking $booking): JsonResponse
{
$response = new AccommodationBookingApiResponse($booking, $this->breakdownCalculator->compute($booking));
$json = $this->serializer->serialize($response, 'json', ['groups' => ['api:single']]);
return new JsonResponse($json, Response::HTTP_OK, [], true);
}
}