src/Controller/FavouriteController.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Services\Api\Auth\AuthService;
  4. use App\Services\Api\Favorites\FavoriteService;
  5. use App\Services\TokenService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  11. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  12. class FavouriteController extends AbstractController
  13. {
  14.     private FavoriteService $favoriteService;
  15.     private TokenService $tokenVerifier;
  16.     private AuthService $authService;
  17.     public function __construct(FavoriteService $favoriteServiceTokenService $tokenVerifierAuthService $authService)
  18.     {
  19.         $this->favoriteService $favoriteService;
  20.         $this->tokenVerifier $tokenVerifier;
  21.         $this->authService $authService;
  22.     }
  23.     /**
  24.      * @throws \Exception
  25.      * @throws TransportExceptionInterface
  26.      */
  27.     #[Route('/add/favorites/attraction/{id}'name'app_add_favorite')]
  28.     public function index($id): Response
  29.     {
  30.         // Vérifie si le token est valide
  31.         if (!$this->tokenVerifier->isTokenValid()) {
  32.             return $this->redirectToRoute('app_login');
  33.         }
  34.         $body = [
  35.             "attraction_id" => (int)$id
  36.         ];
  37.         $response $this->favoriteService->addFavorite($body);
  38.         if ($response->getStatusCode() == 200 || $response->getStatusCode() == 201 || $response->getStatusCode() == 202){
  39.             $this->addFlash('success'"L'attraction a été ajoutée avec succès aux favoris");
  40.         }else {
  41.             $this->addFlash('danger'json_decode($response->getContent(false), true)['message']);
  42.         }
  43.         return $this->redirectToRoute("services_app_favory");
  44.     }
  45.     /**
  46.      * @throws TransportExceptionInterface
  47.      * @throws DecodingExceptionInterface
  48.      * @throws \Exception
  49.      * @throws DecodingExceptionInterface
  50.      */
  51.     #[Route('/favory'name'services_app_favory')]
  52.     public function favory(Request $request): Response
  53.     {
  54.         if (!$this->tokenVerifier->isTokenValid()) {
  55.             $this->addFlash('danger'"Vous devez être connecté");
  56.             return $this->redirectToRoute('app_login');
  57.         }
  58.         $page $request->query->getInt('page'1);
  59.         $userId $this->authService->getAllAboutMe()['id'];
  60.         $favories $this->favoriteService->getFavorites($userId$page)->toArray();
  61.         $pagination = [];
  62.         if (isset($favories['hydra:view'])){
  63.             $paginationFavories $favories['hydra:view'];
  64.             if (isset($paginationFavories['hydra:first'])){
  65.                 $pagination['first'] = explode('page=',$paginationFavories['hydra:first'])[1];
  66.                 $pagination['last'] = explode('page=',$paginationFavories['hydra:last'])[1];
  67.             }
  68.         }
  69.         return $this->render('frontend/services/favourite.html.twig', [
  70.             'favories' => $favories,
  71.             'page' => $page,
  72.             'pagination' => $pagination
  73.         ]);
  74.     }
  75.     /**
  76.      * @throws TransportExceptionInterface
  77.      * @throws \Exception
  78.      */
  79.     #[Route('/delete/favory/{id}'name'services_delete_favory')]
  80.     public function deleteFavory($id): Response
  81.     {
  82.         $response =  $this->favoriteService->deleteFavorite((int) $id);
  83.         if ($response->getStatusCode() == 200 || $response->getStatusCode() == 201 || $response->getStatusCode() == 204) {
  84.             $this->addFlash('success'"L'attraction a été retirée de vos favoris.");
  85.         } else {
  86.             $this->addFlash('danger'json_decode($response->getContent(false), true)['message']);
  87.         }
  88.         return $this->redirectToRoute("services_app_favory");
  89.     }
  90. }