41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
|
|
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;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
#[Route('/api')]
|
|
#[IsGranted('ROLE_OAUTH2_API')]
|
|
class HotelController extends AbstractController
|
|
{
|
|
public function __construct(private readonly HotelLoader $xmlLoader)
|
|
{
|
|
}
|
|
|
|
#[Route('/hotels', name: 'api_hotels_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
|
|
{
|
|
try {
|
|
$hotel = $this->xmlLoader->loadById($id);
|
|
|
|
return $this->json($hotel, Response::HTTP_OK, [], ['groups' => 'api:single']);
|
|
} catch (HotelNotFoundException $e) {
|
|
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
}
|
|
}
|