<?php
namespace App\Controller;
use App\Services\Api\Auth\AuthService;
use App\Services\Api\Favorites\FavoriteService;
use App\Services\TokenService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class FavouriteController extends AbstractController
{
private FavoriteService $favoriteService;
private TokenService $tokenVerifier;
private AuthService $authService;
public function __construct(FavoriteService $favoriteService, TokenService $tokenVerifier, AuthService $authService)
{
$this->favoriteService = $favoriteService;
$this->tokenVerifier = $tokenVerifier;
$this->authService = $authService;
}
/**
* @throws \Exception
* @throws TransportExceptionInterface
*/
#[Route('/add/favorites/attraction/{id}', name: 'app_add_favorite')]
public function index($id): Response
{
// Vérifie si le token est valide
if (!$this->tokenVerifier->isTokenValid()) {
return $this->redirectToRoute('app_login');
}
$body = [
"attraction_id" => (int)$id
];
$response = $this->favoriteService->addFavorite($body);
if ($response->getStatusCode() == 200 || $response->getStatusCode() == 201 || $response->getStatusCode() == 202){
$this->addFlash('success', "L'attraction a été ajoutée avec succès aux favoris");
}else {
$this->addFlash('danger', json_decode($response->getContent(false), true)['message']);
}
return $this->redirectToRoute("services_app_favory");
}
/**
* @throws TransportExceptionInterface
* @throws DecodingExceptionInterface
* @throws \Exception
* @throws DecodingExceptionInterface
*/
#[Route('/favory', name: 'services_app_favory')]
public function favory(Request $request): Response
{
if (!$this->tokenVerifier->isTokenValid()) {
$this->addFlash('danger', "Vous devez être connecté");
return $this->redirectToRoute('app_login');
}
$page = $request->query->getInt('page', 1);
$userId = $this->authService->getAllAboutMe()['id'];
$favories = $this->favoriteService->getFavorites($userId, $page)->toArray();
$pagination = [];
if (isset($favories['hydra:view'])){
$paginationFavories = $favories['hydra:view'];
if (isset($paginationFavories['hydra:first'])){
$pagination['first'] = explode('page=',$paginationFavories['hydra:first'])[1];
$pagination['last'] = explode('page=',$paginationFavories['hydra:last'])[1];
}
}
return $this->render('frontend/services/favourite.html.twig', [
'favories' => $favories,
'page' => $page,
'pagination' => $pagination
]);
}
/**
* @throws TransportExceptionInterface
* @throws \Exception
*/
#[Route('/delete/favory/{id}', name: 'services_delete_favory')]
public function deleteFavory($id): Response
{
$response = $this->favoriteService->deleteFavorite((int) $id);
if ($response->getStatusCode() == 200 || $response->getStatusCode() == 201 || $response->getStatusCode() == 204) {
$this->addFlash('success', "L'attraction a été retirée de vos favoris.");
} else {
$this->addFlash('danger', json_decode($response->getContent(false), true)['message']);
}
return $this->redirectToRoute("services_app_favory");
}
}