From 42da8da43f881b6d0eaa4ce5acffe64f0e8b19fc Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Sat, 26 Nov 2022 22:42:44 +0100 Subject: [PATCH] Add movie genre filter --- src/Application/Movie/Api.php | 9 ++++++- .../Movie/History/Service/Select.php | 18 ++++++++++++- src/Application/Movie/Repository.php | 26 ++++++++++++++++++- src/HttpController/Dto/MoviesRequestDto.php | 12 +++++++-- .../Mapper/MoviesRequestMapper.php | 4 +++ src/HttpController/MoviesController.php | 5 +++- templates/page/dashboard.html.twig | 4 ++- templates/page/movies.html.twig | 9 +++++++ 8 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/Application/Movie/Api.php b/src/Application/Movie/Api.php index a4651640..1724b6f7 100644 --- a/src/Application/Movie/Api.php +++ b/src/Application/Movie/Api.php @@ -148,6 +148,11 @@ public function fetchHistoryOrderedByWatchedAtDesc(int $userId) : array return $this->historySelectService->fetchHistoryOrderedByWatchedAtDesc($userId); } + public function fetchUniqueMovieGenres(int $userId) : array + { + return $this->historySelectService->fetchUniqueMovieGenres($userId); + } + public function fetchUniqueMovieLanguages(int $userId) : array { return $this->historySelectService->fetchUniqueMovieLanguages($userId); @@ -167,6 +172,7 @@ public function fetchUniqueMoviesPaginated( string $sortOrder, ?Year $releaseYear, ?string $language, + ?string $genre, ) : array { return $this->historySelectService->fetchUniqueMoviesPaginated( $userId, @@ -176,7 +182,8 @@ public function fetchUniqueMoviesPaginated( $sortBy, $sortOrder, $releaseYear, - $language + $language, + $genre, ); } diff --git a/src/Application/Movie/History/Service/Select.php b/src/Application/Movie/History/Service/Select.php index 7dba3961..48198ed2 100644 --- a/src/Application/Movie/History/Service/Select.php +++ b/src/Application/Movie/History/Service/Select.php @@ -158,6 +158,11 @@ public function fetchTotalHoursWatched(int $userId) : int return (int)round($minutes / 60); } + public function fetchUniqueMovieGenres(int $userId) : array + { + return $this->movieRepository->fetchUniqueMovieGenres($userId); + } + public function fetchUniqueMovieInHistoryCount(int $userId, ?string $searchTerm = null) : int { return $this->movieRepository->fetchUniqueMovieInHistoryCount($userId, $searchTerm); @@ -189,8 +194,19 @@ public function fetchUniqueMoviesPaginated( string $sortOrder = 'ASC', ?Year $releaseYear = null, ?string $language = null, + ?string $genre = null, ) : array { - return $this->movieRepository->fetchUniqueMoviesPaginated($userId, $limit, $page, $searchTerm, $sortBy, $sortOrder, $releaseYear, $language); + return $this->movieRepository->fetchUniqueMoviesPaginated( + $userId, + $limit, + $page, + $searchTerm, + $sortBy, + $sortOrder, + $releaseYear, + $language, + $genre, + ); } public function findByTraktId(TraktId $traktId) : ?Entity diff --git a/src/Application/Movie/Repository.php b/src/Application/Movie/Repository.php index 26eab2fd..f5107eba 100644 --- a/src/Application/Movie/Repository.php +++ b/src/Application/Movie/Repository.php @@ -431,6 +431,22 @@ public function fetchTotalMinutesWatched(int $userId) : int )->fetchFirstColumn()[0]; } + public function fetchUniqueMovieGenres(int $userId) : array + { + return $this->dbConnection->fetchFirstColumn( + <<dbConnection->fetchFirstColumn( @@ -480,7 +496,8 @@ public function fetchUniqueMoviesPaginated( string $sortBy, string $sortOrder, ?Year $releaseYear, - ?string $language + ?string $language, + ?string $genre, ) : array { $payload = [$userId, $userId, "%$searchTerm%"]; @@ -505,12 +522,19 @@ public function fetchUniqueMoviesPaginated( $payload[] = $language; } + if (empty($genre) === false) { + $whereQuery .= 'AND g.name = ? '; + $payload[] = $genre; + } + return $this->dbConnection->fetchAllAssociative( <<genre; + } + public function getLanguage() : ?string { return $this->language; diff --git a/src/HttpController/Mapper/MoviesRequestMapper.php b/src/HttpController/Mapper/MoviesRequestMapper.php index 697dc7d0..d05e1d01 100644 --- a/src/HttpController/Mapper/MoviesRequestMapper.php +++ b/src/HttpController/Mapper/MoviesRequestMapper.php @@ -9,6 +9,8 @@ class MoviesRequestMapper { + private const DEFAULT_GENRE = null; + private const DEFAULT_RELEASE_YEAR = null; private const DEFAULT_LIMIT = 24; @@ -34,6 +36,7 @@ public function mapRenderPageRequest(Request $request) : MoviesRequestDto $releaseYear = $request->getGetParameters()['ry'] ?? self::DEFAULT_RELEASE_YEAR; $releaseYear = empty($releaseYear) === false ? Year::createFromString($releaseYear) : null; $language = $request->getGetParameters()['la'] ?? null; + $genre = $request->getGetParameters()['ge'] ?? self::DEFAULT_GENRE; return MoviesRequestDto::createFromParameters( $userId, @@ -44,6 +47,7 @@ public function mapRenderPageRequest(Request $request) : MoviesRequestDto $sortOrder, $releaseYear, $language, + $genre, ); } } diff --git a/src/HttpController/MoviesController.php b/src/HttpController/MoviesController.php index 0e79cb7d..a3013099 100644 --- a/src/HttpController/MoviesController.php +++ b/src/HttpController/MoviesController.php @@ -37,7 +37,8 @@ public function renderPage(Request $request) : Response $requestData->getSortBy(), $requestData->getSortOrder(), $requestData->getReleaseYear(), - $requestData->getLanguage() + $requestData->getLanguage(), + $requestData->getGenre(), ); $historyCount = $this->movieApi->fetchUniqueMoviesCount($userId, $requestData->getSearchTerm()); @@ -62,8 +63,10 @@ public function renderPage(Request $request) : Response 'sortOrder' => $requestData->getSortOrder(), 'releaseYear' => (string)$requestData->getReleaseYear(), 'language' => (string)$requestData->getLanguage(), + 'genre' => (string)$requestData->getGenre(), 'uniqueReleaseYears' => $this->movieApi->fetchUniqueMovieReleaseYears($userId), 'uniqueLanguages' => $this->movieApi->fetchUniqueMovieLanguages($userId), + 'uniqueGenres' => $this->movieApi->fetchUniqueMovieGenres($userId), ]), ); } diff --git a/templates/page/dashboard.html.twig b/templates/page/dashboard.html.twig index 26e0385d..6a8a65ed 100644 --- a/templates/page/dashboard.html.twig +++ b/templates/page/dashboard.html.twig @@ -170,7 +170,9 @@ {% for mostWatchedGenre in mostWatchedGenres %} {% if loop.index <= 12 %}
-
+
{{ mostWatchedGenre.name }}
diff --git a/templates/page/movies.html.twig b/templates/page/movies.html.twig index b871308c..05c38fe6 100644 --- a/templates/page/movies.html.twig +++ b/templates/page/movies.html.twig @@ -64,6 +64,15 @@ {% endfor %}
+
+ Genre + +
Language