src/Controller/FavoritesController.php line 101

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\UserFavorites;
  5. use App\Service\api\JwpHttp;
  6. use App\Service\email\GMailApi;
  7. use App\Service\favorites\UserFavoritesService;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class FavoritesController extends AbstractController
  15. {
  16.     /**
  17.      * @Security("is_granted('ROLE_USER_ACTIF') or is_granted('ROLE_USER_INACTIF')")
  18.      * @Route("/favorites", name="favorites")
  19.      */
  20.     public function index(UserFavoritesService $userFavoritesServiceJwpHttp $jwpHttp): Response
  21.     {
  22.         /* @var $user User*/
  23.         $user $this->getUser();
  24.         $playlistFavorite $userFavoritesService->getPlaylistFavorites($user);
  25.         return $this->render('favorites/index.html.twig', [
  26.             'playlists' => $playlistFavorite,
  27.             'title_page'=> 'Favoris'
  28.         ]);
  29.     }
  30.     /**
  31.      * @Security("is_granted('ROLE_USER_ACTIF') or is_granted('ROLE_USER_INACTIF')")
  32.      * @Route("/favorite/remove-favorite", name="remove-favorite")
  33.      */
  34.     public function removeFavorite(Request $requestUserFavoritesService $userFavoritesService)
  35.     {
  36.         $favoriteId $request->get('id');
  37.         $userFavoritesService->removeById($favoriteId);
  38.         return $this->forward('App\Controller\FavoritesController::index',[]);
  39.     }
  40.     /**
  41.      * @Security("is_granted('ROLE_USER_ACTIF') or is_granted('ROLE_USER_INACTIF')")
  42.      * @Route("/api/toggle-favorite", name="toggle-favorite")
  43.      */
  44.     public function toggleFavorite(Request $requestUserFavoritesService $userFavoritesService): JsonResponse
  45.     {
  46.         /* @var $user User*/
  47.         $user $this->getUser();
  48.         $entityId $request->get('entityId');
  49.         $entity $request->get('entity');
  50.         $data $userFavoritesService->toggle($user,$entityId,$entity);
  51.         return new JsonResponse($data);
  52.     }
  53.     /**
  54.      * @Security("is_granted('ROLE_USER_ACTIF') or is_granted('ROLE_USER_INACTIF')")
  55.      * @Route("/api/add-favorite", name="add-favorite")
  56.      */
  57.     public function addFavorite(Request $requestUserFavoritesService $userFavoritesService): JsonResponse
  58.     {
  59.         /* @var $user User*/
  60.         $user $this->getUser();
  61.         $entityId $request->get('entityId');
  62.         $userFavoritesService->add($user,$entityId);
  63.         return new JsonResponse('ok');
  64.     }
  65.     /**
  66.      * @Security("is_granted('ROLE_USER_ACTIF') or is_granted('ROLE_USER_INACTIF')")
  67.      * @Route("/api/list-favorite", name="list-favorite")
  68.      */
  69.     public function listFavorite(UserFavoritesService $userFavoritesService): JsonResponse
  70.     {
  71.         $favorites =$userFavoritesService->findAll($this->getUser(),'playlist');
  72.         return new JsonResponse($favorites);
  73.     }
  74.     /**
  75.      * @Security("is_granted('ROLE_USER_ACTIF') or is_granted('ROLE_USER_INACTIF')")
  76.      * @Route("/api/favorite-exist", name="favorite-exist")
  77.      */
  78.     public function favoriteExist(Request $requestUserFavoritesService $userFavoritesService): JsonResponse
  79.     {
  80.         if( ! $request->query->has('entityId') ){
  81.             return new JsonResponse('ko');
  82.         }
  83.         $entityId $request->query->get('entityId');
  84.         $favoriteExist $userFavoritesService->exist($this->getUser(),$entityId,'playlist') ? true false;
  85.         return new JsonResponse(['favoriteExist'=>$favoriteExist]);
  86.     }
  87. }