<?php
namespace App\Controller;
use App\Entity\Data\ElementThData;
use App\Entity\Element;
use App\Repository\ElementRepository;
use App\Repository\OnPlaceRepository;
use App\Service\ResponseFactoryService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/api/v1/tourainehotels', name: 'api.tourainehotels')]
class ApiTouraineHotelsController extends AbstractController
{
public function __construct(protected int $httpCacheExpiration)
{
}
#[Route('/search/{details}',
name: 'search',
requirements: [
'details' => 'short-details|long-details'
])
]
public function search(string $details, ElementRepository $elementRepository,
ResponseFactoryService $responseFactoryService): JsonResponse
{
try {
$elements = $elementRepository->findBy(['groupmentTouraineHotels' => true]);
$list = [];
foreach ($elements as $element) {
$list[] = new ElementThData($element);
}
return $responseFactoryService->replyOk(['data' => $list], $details)
->setSharedMaxAge($this->httpCacheExpiration);;
} catch (\InvalidArgumentException $exception) {
return $responseFactoryService->replyError($exception->getMessage());
}
}
#[Route('/show/{reference}/{details}',
name: 'show',
requirements: [
'details' => 'short-details|long-details'
])
]
public function show(Element $element, string $details, ResponseFactoryService $responseFactoryService): JsonResponse
{
try {
$element = new ElementThData($element);
return $responseFactoryService->replyOk(['data' => $element], $details)
->setSharedMaxAge($this->httpCacheExpiration);;
} catch (\InvalidArgumentException $exception) {
return $responseFactoryService->replyError($exception->getMessage());
}
}
#[Route('/onplace/{reference}/{details}',
name: 'onplace',
requirements: [
'details' => 'onplace-details'
])
]
public function onplace(Element $element, string $details, OnPlaceRepository $onPlaceRepository, ResponseFactoryService $responseFactoryService): JsonResponse
{
try {
$onPlace = $onPlaceRepository->findBy(['parent' => $element->getThMember()->getId()]);
$data = [];
$data['status'] = 200;
$data['message'] = 'ok';
$data['data'] = $onPlace;
return new JsonResponse($data, 200);
} catch (\InvalidArgumentException $exception) {
return $responseFactoryService->replyError($exception->getMessage());
}
}
#[Route('/map/{details}',
name: 'map',
requirements: [
'details' => 'th-geojson'
])
]
public function map(string $details, ElementRepository $elementRepository,
ResponseFactoryService $responseFactoryService): JsonResponse
{
try {
$elements = $elementRepository->findBy(['groupmentTouraineHotels' => true]);
$list = [];
foreach ($elements as $element) {
$list[] = new ElementThData($element);
}
return $responseFactoryService->replyOk(['data' => $list], $details)
->setSharedMaxAge($this->httpCacheExpiration);
} catch (\InvalidArgumentException $exception) {
return $responseFactoryService->replyError($exception->getMessage());
}
}
#[Route('/export', name: 'export')]
public function export(ElementRepository $elementRepository, ResponseFactoryService $responseFactoryService): JsonResponse
{
try {
$elements = $elementRepository->findBy(['groupmentTouraineHotels' => true]);
$list = [];
foreach ($elements as $element) {
$list[] = new ElementThData($element);
}
return $responseFactoryService->replyOk(['data' => $list], 'export-details');
} catch (\InvalidArgumentException $exception) {
return $responseFactoryService->replyError($exception->getMessage());
}
}
}