Эх сурвалжийг харах

Merge branch 'develop' of https://gogs.fdlibre.eu/sangfroid/films-symfony-4-2 into develop

François Drouhard 2 жил өмнө
parent
commit
3ceec634b1

+ 5 - 4
src/Controller/VideothequeController.php

@@ -21,6 +21,7 @@ use App\Service\CommentaireManager;
 use App\Service\FilmManager;
 use App\Service\OptionsManager;
 use Symfony\Component\Form\FormFactoryInterface;
+use Symfony\Component\Serializer\SerializerInterface;
 
 class VideothequeController extends AbstractController
 {
@@ -170,13 +171,13 @@ class VideothequeController extends AbstractController
     /**
      * @Route("/ajax_req_realisateurs", name="videotheque_ajax_realisateurs")
      */
-    public function ajaxRealisateurs(Request $request, RealisateurRepository $repo)
+    public function ajaxRealisateurs(RealisateurRepository $repo, SerializerInterface $serializer)
     {
         $realisateurs = $repo->findNomsComplets();
         $liste = array();
         foreach ($realisateurs as $key=>$nom)
         {
-            $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
+            $liste[$key] = $serializer->serialize($nom, 'json');
         }
 
         return new JsonResponse($liste);
@@ -185,13 +186,13 @@ class VideothequeController extends AbstractController
     /**
      * @Route("/ajax_req_genres", name="videotheque_ajax_genres")
      */
-    public function ajaxGenres(Request $request, GenreRepository $repo)
+    public function ajaxGenres(GenreRepository $repo, SerializerInterface $serializer)
     {
         $genres = $repo->findGenres();
         $liste = array();
         foreach ($genres as $key=>$nom)
         {
-            $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
+            $liste[$key] = $serializer->serialize($nom, 'json');
         }
 
         return new JsonResponse($liste);

+ 11 - 7
src/Controller/VideothequePersonnelleController.php

@@ -3,12 +3,14 @@
 namespace App\Controller;
 
 use App\Repository\FilmRepository;
+use App\Service\FilmManager;
 use App\Service\OptionsManager;
 use Doctrine\ORM\EntityManagerInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Serializer\SerializerInterface;
 
 class VideothequePersonnelleController extends AbstractController
 {
@@ -41,16 +43,17 @@ class VideothequePersonnelleController extends AbstractController
      * @param Request $request
      * @return JsonResponse
      */
-    public function modifierFilmDansListeAction(Request $request, EntityManagerInterface $em, FilmRepository $repo)
+    public function modifierFilmDansListeAction(Request $request, EntityManagerInterface $em, FilmRepository $repo, FilmManager $filmManager)
     {
+        $result = null;
         $film = $repo->find($request->request->get('id_film'));
         if ($request->isXmlHttpRequest())
         {
-            $film->inverseUserWantToView($this->getUser());
+            $result = $filmManager->inverseUserWantToView($film);
             $em->flush();
         }
         /*$resultat = $this->get('serializer')->serialize($film, 'json');*/
-        return new JsonResponse('OK');
+        return new JsonResponse((object)['newState' => $result]);
     }
 
     /**
@@ -58,16 +61,17 @@ class VideothequePersonnelleController extends AbstractController
      * @param Request $request
      * @return JsonResponse
      */
-    public function modifierFilmVusAction(Request $request, FilmRepository $repo, EntityManagerInterface $em)
+    public function modifierFilmVusAction(Request $request, FilmRepository $repo, EntityManagerInterface $em, FilmManager $filmManager)
     {
+        $result = null;
+
         $film = $repo->find($request->request->get('id_film'));
         if ($request->isXmlHttpRequest())
         {
-            $film->inverseUserWhoSeen($this->getUser());
+            $result = $filmManager->inverseUserWhoSeen($film);
             $em->flush();
         }
-        /*$resultat = $this->get('serializer')->serialize($film, 'json');*/
-        return new JsonResponse('OK');
+        return new JsonResponse((object)['newState' => $result]);
     }
 
 }

+ 0 - 27
src/Entity/Film.php

@@ -374,20 +374,6 @@ class Film
         return $this->usersWantToView;
     }
 
-    /**
-     * Inverse ToSee
-     * @param \App\Entity\User $user
-     */
-    public function inverseUserWantToView(User $user)
-    {
-        if ($this->usersWantToView->contains($user)) {
-            $this->removeUserWantToView($user);
-        } else {
-            $this->addUserWantToView($user);
-        }
-
-    }
-
     /////////////////////////////////////////////////////////////////////
     /**
      * Add user
@@ -425,19 +411,6 @@ class Film
         return $this->usersWhoSeen;
     }
 
-    /**
-     * Inverse ToSee
-     * @param \App\Entity\User $user
-     */
-    public function inverseUserWhoSeen(User $user)
-    {
-        if ($this->usersWhoSeen->contains($user)) {
-            $this->removeUserWhoSeen($user);
-        } else {
-            $this->addUserWhoSeen($user);
-        }
-    }
-
     /**
      * Get genre
      *

+ 38 - 11
src/Service/FilmManager.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 use App\Entity\Film;
+use App\Entity\User;
 use Doctrine\ORM\EntityManagerInterface;
 use Symfony\Component\Security\Core\Security;
 
@@ -10,20 +11,14 @@ use Symfony\Component\Security\Core\Security;
  * Class FilmManager
  */
 class FilmManager {
-    private $em;
-    private $uc;
-    private $user;
-    private $cm;
+    protected $user;
 
-    public function __construct(EntityManagerInterface $em, UniciteCollections $uc, CommentaireManager $cm, Security $security)
+    public function __construct(protected EntityManagerInterface $em, protected UniciteCollections $uc, protected CommentaireManager $cm, Security $security)
     {
-        $this->em = $em;
-        $this->uc = $uc;
-        $this->cm = $cm;
         $this->user = $security->getUser();
     }
 
-    public function addFilm (Film $film)
+    public function addFilm (Film $film): void
     {
         $film->setAuthered($this->user);
         $film = $this->uc->assureUniciteCollections($film);
@@ -31,13 +26,13 @@ class FilmManager {
         $this->em->flush();
     }
 
-    public function editFilm(Film $film)
+    public function editFilm(Film $film): void
     {
         $film = $this->uc->assureUniciteCollections($film);
         $this->em->flush();
     }
 
-    public function delFilm(Film $film)
+    public function delFilm(Film $film): void
     {
         $commentaires = $film->getCommentaires();
         foreach($commentaires as $commentaire)
@@ -48,4 +43,36 @@ class FilmManager {
 
         $this->em->flush();
     }
+
+    public function inverseUserWhoSeen (Film $film): bool
+    {
+        if ($film->getUsersWhoSeen()->contains($this->user)) {
+            $film->removeUserWhoSeen($this->user);
+        } else {
+            $film->addUserWhoSeen($this->user);
+        }
+        return $this->isSeen($film);
+    }
+
+    public function inverseUserWantToView (Film $film): bool
+    {
+        if ($film->getUsersWantToView()->contains($this->user)) {
+            $film->removeUserWantToView($this->user);
+        } else {
+            $film->addUserWantToView($this->user);
+        }
+        return $this->isWantedToBeSeen($film);
+    }
+
+    public function isSeen(Film $film, User $user = null): bool
+    {
+        $user = $user ?: $this->user;
+        return $film->getUsersWhoSeen()->contains($user);
+    }
+
+    public function isWantedToBeSeen(Film $film, User $user = null): bool
+    {
+        $user = $user ?: $this->user;
+        return $film->getUsersWantToView()->contains($user);
+    }
 }