Skip to content

Commit

Permalink
Merge pull request #121 from leepeuker/add-movie-genre-filter
Browse files Browse the repository at this point in the history
Add movie genre filter
  • Loading branch information
leepeuker authored Nov 26, 2022
2 parents 3782f4f + 42da8da commit dd0ae42
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/Application/Movie/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -167,6 +172,7 @@ public function fetchUniqueMoviesPaginated(
string $sortOrder,
?Year $releaseYear,
?string $language,
?string $genre,
) : array {
return $this->historySelectService->fetchUniqueMoviesPaginated(
$userId,
Expand All @@ -176,7 +182,8 @@ public function fetchUniqueMoviesPaginated(
$sortBy,
$sortOrder,
$releaseYear,
$language
$language,
$genre,
);
}

Expand Down
18 changes: 17 additions & 1 deletion src/Application/Movie/History/Service/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion src/Application/Movie/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,22 @@ public function fetchTotalMinutesWatched(int $userId) : int
)->fetchFirstColumn()[0];
}

public function fetchUniqueMovieGenres(int $userId) : array
{
return $this->dbConnection->fetchFirstColumn(
<<<SQL
SELECT DISTINCT g.name
FROM movie_user_watch_dates mh
JOIN movie m on mh.movie_id = m.id
JOIN movie_genre mg on m.id = mg.movie_id
JOIN genre g on mg.genre_id = g.id
WHERE user_id = ?
ORDER BY g.name
SQL,
[$userId]
);
}

public function fetchUniqueMovieInHistoryCount(int $userId, ?string $searchTerm) : int
{
return $this->dbConnection->fetchFirstColumn(
Expand Down Expand Up @@ -480,7 +496,8 @@ public function fetchUniqueMoviesPaginated(
string $sortBy,
string $sortOrder,
?Year $releaseYear,
?string $language
?string $language,
?string $genre,
) : array {
$payload = [$userId, $userId, "%$searchTerm%"];

Expand All @@ -505,12 +522,19 @@ public function fetchUniqueMoviesPaginated(
$payload[] = $language;
}

if (empty($genre) === false) {
$whereQuery .= 'AND g.name = ? ';
$payload[] = $genre;
}

return $this->dbConnection->fetchAllAssociative(
<<<SQL
SELECT m.*, mur.rating as userRating
FROM movie m
JOIN movie_user_watch_dates mh on mh.movie_id = m.id and mh.user_id = ?
LEFT JOIN movie_user_rating mur ON mh.movie_id = mur.movie_id and mur.user_id = ?
JOIN movie_genre mg on m.id = mg.movie_id
JOIN genre g on mg.genre_id = g.id
$whereQuery
GROUP BY m.id, title, release_date, rating
ORDER BY $sortBySanitized $sortOrder, title asc
Expand Down
12 changes: 10 additions & 2 deletions src/HttpController/Dto/MoviesRequestDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ private function __construct(
private readonly string $sortBy,
private readonly string $sortOrder,
private readonly ?Year $releaseYear,
private readonly ?string $language
private readonly ?string $language,
private readonly ?string $genre,
) {
}

Expand All @@ -26,7 +27,8 @@ public static function createFromParameters(
string $sortBy,
string $sortOrder,
?Year $releaseYear,
?string $language
?string $language,
?string $genre,
) : self {
return new self(
$userId,
Expand All @@ -37,9 +39,15 @@ public static function createFromParameters(
$sortOrder,
$releaseYear,
$language,
$genre,
);
}

public function getGenre() : ?string
{
return $this->genre;
}

public function getLanguage() : ?string
{
return $this->language;
Expand Down
4 changes: 4 additions & 0 deletions src/HttpController/Mapper/MoviesRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class MoviesRequestMapper
{
private const DEFAULT_GENRE = null;

private const DEFAULT_RELEASE_YEAR = null;

private const DEFAULT_LIMIT = 24;
Expand All @@ -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,
Expand All @@ -44,6 +47,7 @@ public function mapRenderPageRequest(Request $request) : MoviesRequestDto
$sortOrder,
$releaseYear,
$language,
$genre,
);
}
}
5 changes: 4 additions & 1 deletion src/HttpController/MoviesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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),
]),
);
}
Expand Down
4 changes: 3 additions & 1 deletion templates/page/dashboard.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@
{% for mostWatchedGenre in mostWatchedGenres %}
{% if loop.index <= 12 %}
<div class="col" style="padding-bottom: 1rem;">
<div class="card h-100 position-relative">
<div class="card h-100 position-relative"
style="cursor: pointer"
onclick="window.location='/{{ routeUsername }}/movies?ge={{ mostWatchedGenre.name }}'">
<div class="card-header text-truncate" style="padding: 0.2rem;border-bottom:0">
<small class="text-muted" style="font-size: 0.9rem;font-weight: bold">{{ mostWatchedGenre.name }}</small>
</div>
Expand Down
9 changes: 9 additions & 0 deletions templates/page/movies.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
{% endfor %}
</select>
</div>
<div class="input-group mb-3">
<span class="input-group-text">Genre</span>
<select class="form-control" name="ge" id="genre">
<option value="" {{ genre is null ? 'selected' }}>All</option>
{% for uniqueGenre in uniqueGenres %}
<option value="{{ uniqueGenre }}" {{ uniqueGenre == genre ? 'selected' }}>{{ uniqueGenre }}</option>
{% endfor %}
</select>
</div>
<div class="input-group mb-3">
<span class="input-group-text">Language</span>
<select class="form-control" name="la" id="language">
Expand Down

0 comments on commit dd0ae42

Please sign in to comment.