Browse Source

Ajout de l'évitement des doublons dans recherche imdb

Sangfroid 2 months ago
parent
commit
dbf7aad511

+ 12 - 2
src/Controller/VideothequeCrudController.php

@@ -64,9 +64,13 @@ class VideothequeCrudController extends AbstractController
 	}
 
     #[Route("/addtmdb", name: "videotheque_ajouter_tmdb")]
-    public function ajouterTmdb(Request $request, TmdbApiService $tmdbApiService): Response
+    public function ajouterTmdb(
+        Request $request,
+        TmdbApiService $tmdbApiService,
+        FilmRepository $filmRepository
+    ): Response
     {
-        $films = array();
+        $films = [];
         $nbFilms = 0;
         $nbPages = 0;
         
@@ -80,6 +84,12 @@ class VideothequeCrudController extends AbstractController
             $nbFilms = $tmdbApiService->countResults();
             $nbPages = $tmdbApiService->countPages();
             $films = $tmdbApiService->getFilms();
+            foreach($films as $key => $film) {
+                $filmDejaEnBase = $filmRepository->findFilmInImdb($film['data']);
+                foreach($filmDejaEnBase as $filmInBdd) {
+                    $films[$key]['filmBdd'][] = $filmInBdd;
+                }         
+            }
         }
         
         return $this->render('videotheque/add_tmdb.html.twig', [

+ 27 - 3
src/Repository/FilmRepository.php

@@ -39,7 +39,10 @@ class FilmRepository extends ServiceEntityRepository
             ->innerJoin('f.genres', 'g')
             ->addSelect('f');
 
-        $qb->andWhere($qb->expr()->in('g.name', $genreNames));
+        $qb
+            ->andWhere($qb->expr()->in('g.name', ':genrenames'))
+            ->setParameter('genrenames', $genreNames)
+        ;
 
         return $qb;
     }
@@ -58,8 +61,9 @@ class FilmRepository extends ServiceEntityRepository
         $qb
             ->leftJoin('f.realisateurs', 'rea')
             ->addSelect('f')
-            ->andWhere($qb->expr()->in('rea.nomComplet', $realisateursNames));
-
+            ->andWhere($qb->expr()->in('rea.nomComplet', ':realisateurs'))
+            ->setParameter('realisateurs', $realisateursNames)
+        ;
         return $qb;
     }
 
@@ -176,4 +180,24 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult()
         ;
     }
+
+    public function findFilmInImdb(Film $film): array
+    {
+        $reals = [];
+        foreach($film->getRealisateurs() as $real) {
+            $reals[] = $real->getNomComplet();
+        }
+        $qb = $this->createQueryBuilder('f');
+        $qb
+            ->andWhere('f.titre = :titre')
+            ->setParameter('titre', $film->getTitre())
+            ->innerJoin('f.realisateurs', 'rea')
+            ->andWhere($qb->expr()->in('rea.nomComplet', ':reals'))
+            ->setParameter('reals', $reals)
+        ;
+        return $qb
+            ->getQuery()
+            ->getResult()
+        ;
+    }
 }

+ 1 - 1
src/Service/TmdbApiService.php

@@ -90,7 +90,7 @@ class TmdbApiService
         foreach($entrees as $entree) {
             $film = $this->hydrateFilm($entree->id);
 
-            $this->films[$entree->id] = $film;
+            $this->films[$entree->id]['data'] = $film;
         }
     }
 

+ 19 - 7
templates/videotheque/add_tmdb.html.twig

@@ -53,14 +53,26 @@
                 <tr>
             {% endif %}
             {% for idtmdb, film in films %}
-                <tr class="{{ film.information ? '' : 'border-bottom' }}">
+                {% if film['filmBdd'] is defined %}
+                <tr>
+                    <td colspan="5">
+                        <div class="alert alert-warning mb-0" role="alert">
+                            Ce film est peut-être déjà en base : 
+                            {% for filmBdd in film['filmBdd'] %}
+                                &nbsp;<a href="{{ path('videotheque_voirfilm', {'id': filmBdd.id} ) }}">Aller sur la fiche</a>
+                            {% endfor %}
+                        </div>
+                    </td>
+                </tr>
+                {% endif %}
+                <tr class="{{ film['data'].information ? '' : 'border-bottom' }}">
                     <td><a type="button" class="btn btn-primary" href="{{ path('videotheque_ajouter', {'idtmdb': idtmdb }) }}">Créér</a></td>
-                    <td>{{ film.titre }}</td>
-                    <td>{% if film.dateSortie %}{{ film.dateSortie | date ('d/m/Y')}}{% endif %}</td>
-                    <td>{% for genre in film.genres %}<span class="badge bg-info me-1">{{ genre.name }}</span>{% endfor %}</td>
-                    <td>{% for realisateur in film.realisateurs %}<span class="badge bg-info me-1">{{ realisateur.nomComplet }}</span>{% endfor %}</td>
+                    <td>{{ film['data'].titre }}</td>
+                    <td>{% if film['data'].dateSortie %}{{ film['data'].dateSortie | date ('d/m/Y')}}{% endif %}</td>
+                    <td>{% for genre in film['data'].genres %}<span class="badge bg-info me-1">{{ genre.name }}</span>{% endfor %}</td>
+                    <td>{% for realisateur in film['data'].realisateurs %}<span class="badge bg-info me-1">{{ realisateur.nomComplet }}</span>{% endfor %}</td>
                 </tr>
-                {% if film.information %}
+                {% if film['data'].information %}
                     <tr data-controller="collapser" class="border-bottom">
                         <td colspan="5">
                             <div class="d-flex">
@@ -70,7 +82,7 @@
                                 </a>
                             </div>
                             <div id="collapseResume-{{ idtmdb }}" class="collapse" data-collapser-target="collapse">
-                                {{ film.information }}
+                                {{ film['data'].information }}
                             </div>
                         </td>
                     </tr>