Skip to content

Commit

Permalink
Merge pull request #612 from leepeuker/add-location-search-filter
Browse files Browse the repository at this point in the history
Add optional location filter to movie search
  • Loading branch information
leepeuker authored Aug 22, 2024
2 parents 36e6443 + 15f7c99 commit e06e9a0
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ db_mysql_create_database:
make app_database_migrate

db_mysql_import:
docker cp storage/dump.sql movary_mysql_1:/tmp/dump.sql
docker compose exec mysql bash -c 'mysql -uroot -p${DATABASE_MYSQL_ROOT_PASSWORD} < /tmp/dump.sql'
docker cp storage/dump.sql movary-mysql-1:/tmp/dump.sql
docker compose exec mysql bash -c 'mysql -uroot -p${DATABASE_MYSQL_ROOT_PASSWORD} movary < /tmp/dump.sql'

db_mysql_export:
docker compose exec mysql bash -c 'mysqldump --databases --add-drop-database -uroot -p$(DATABASE_MYSQL_ROOT_PASSWORD) $(DATABASE_MYSQL_NAME) > /tmp/dump.sql'
docker cp movary_mysql_1:/tmp/dump.sql storage/dump.sql
docker cp movary-mysql-1:/tmp/dump.sql storage/dump.sql
chown $(USER_ID):$(USER_ID) storage/dump.sql

db_migration_create:
Expand Down
6 changes: 6 additions & 0 deletions public/js/component/modal-advanced-search-movie.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const searchPerPageSelect = document.getElementById('searchPerPageSelect');
const searchUserRatingSelect = document.getElementById('searchUserRatingSelect');
const searchMinUserRatingSelect = document.getElementById('searchMinUserRatingSelect');
const searchMaxUserRatingSelect = document.getElementById('searchMaxUserRatingSelect');
const searchLocationSelect = document.getElementById('searchLocationSelect');

function search() {
let sortBy = searchSortBySelect.value
Expand All @@ -26,6 +27,7 @@ function search() {
let searchMinUserRating = searchMinUserRatingSelect.value
let searchMaxUserRating = searchMaxUserRatingSelect.value
let searchTerm = document.getElementById('searchTermInput').value
let searchLocation = searchLocationSelect.value

let getParameters = '?'

Expand All @@ -45,6 +47,9 @@ function search() {
if (searchTerm != '') {
getParameters += '&s=' + searchTerm
}
if (searchLocation != '') {
getParameters += '&loc=' + searchLocation
}
if (searchUserRating != '') {
getParameters += '&ur=' + searchUserRating
if (searchUserRating === '1') {
Expand All @@ -68,6 +73,7 @@ function resetSearchOptions() {
searchMinUserRatingSelect.value = ''
searchMaxUserRatingSelect.value = ''
searchPerPageSelect.value = '24'
searchLocationSelect.value = ''
}


Expand Down
11 changes: 11 additions & 0 deletions src/Domain/Movie/History/MovieHistoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function fetchPlayedMoviesPaginated(
?bool $hasUserRating = null,
?int $userRatingMin = null,
?int $userRatingMax = null,
?int $locationId = null,
) : array {
if ($sortOrder === null) {
$sortOrder = SortOrder::createAsc();
Expand All @@ -283,6 +284,7 @@ public function fetchPlayedMoviesPaginated(
$hasUserRating,
$userRatingMin,
$userRatingMax,
$locationId,
);

return $this->urlGenerator->replacePosterPathWithImageSrcUrl($movies);
Expand Down Expand Up @@ -369,6 +371,11 @@ public function fetchUniqueDirectorsGenders(int $userId) : array
return $uniqueDirectorsGendersEnriched;
}

public function fetchUniqueLocations(int $userId) : array
{
return $this->movieRepository->fetchUniqueLocations($userId);
}

public function fetchUniqueMovieGenres(int $userId) : array
{
return $this->movieRepository->fetchUniqueMovieGenres($userId);
Expand Down Expand Up @@ -407,6 +414,7 @@ public function fetchUniqueWatchedMoviesCount(
?bool $hasUserRating = null,
?int $userRatingMin = null,
?int $userRatingMax = null,
?int $locationId = null,
) : int {
return $this->movieRepository->fetchUniqueWatchedMoviesCount(
$userId,
Expand All @@ -417,6 +425,7 @@ public function fetchUniqueWatchedMoviesCount(
$hasUserRating,
$userRatingMin,
$userRatingMax,
$locationId,
);
}

Expand All @@ -433,6 +442,7 @@ public function fetchUniqueWatchedMoviesPaginated(
?bool $hasUserRating = null,
?int $userRatingMin = null,
?int $userRatingMax = null,
?int $locationId = null,
) : array {
if ($sortOrder === null) {
$sortOrder = SortOrder::createAsc();
Expand All @@ -451,6 +461,7 @@ public function fetchUniqueWatchedMoviesPaginated(
$hasUserRating,
$userRatingMin,
$userRatingMax,
$locationId,
);

return $this->urlGenerator->replacePosterPathWithImageSrcUrl($movies);
Expand Down
9 changes: 9 additions & 0 deletions src/Domain/Movie/MovieApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ public function fetchTotalPlayCountUnique(int $userId) : int
return $this->historyApi->fetchTotalPlayCountUnique($userId);
}

public function fetchUniqueLocations(int $userId) : array
{
return $this->historyApi->fetchUniqueLocations($userId);
}

public function fetchUniqueMovieGenres(int $userId) : array
{
return $this->historyApi->fetchUniqueMovieGenres($userId);
Expand All @@ -301,6 +306,7 @@ public function fetchUniqueWatchedMoviesCount(
?bool $hasUserRating,
?int $userRatingMin,
?int $userRatingMax,
?int $locationId,
) : int {
return $this->historyApi->fetchUniqueWatchedMoviesCount(
$userId,
Expand All @@ -311,6 +317,7 @@ public function fetchUniqueWatchedMoviesCount(
$hasUserRating,
$userRatingMin,
$userRatingMax,
$locationId,
);
}

Expand All @@ -327,6 +334,7 @@ public function fetchUniqueWatchedMoviesPaginated(
?bool $hasUserRating,
?int $userRatingMin,
?int $userRatingMax,
?int $locationId,
) : array {
return $this->historyApi->fetchUniqueWatchedMoviesPaginated(
$userId,
Expand All @@ -341,6 +349,7 @@ public function fetchUniqueWatchedMoviesPaginated(
$hasUserRating,
$userRatingMin,
$userRatingMax,
$locationId,
);
}

Expand Down
27 changes: 27 additions & 0 deletions src/Domain/Movie/MovieRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,21 @@ public function fetchUniqueDirectorsGenders(int $userId) : array
);
}

public function fetchUniqueLocations(int $userId) : array
{
return $this->dbConnection->fetchAllAssociative(
<<<SQL
SELECT DISTINCT l.name, l.id
FROM movie_user_watch_dates mh
JOIN movie m on mh.movie_id = m.id
JOIN location l on l.id = mh.location_id
WHERE mh.user_id = ?
ORDER BY l.name
SQL,
[$userId],
);
}

public function fetchUniqueMovieGenres(int $userId) : array
{
return $this->dbConnection->fetchFirstColumn(
Expand Down Expand Up @@ -776,6 +791,7 @@ public function fetchUniqueWatchedMoviesCount(
?bool $hasUserRating,
?int $userRatingMin,
?int $userRatingMax,
?int $locationId,
) : int {
$payload = [$userId, $userId, "%$searchTerm%"];

Expand Down Expand Up @@ -809,6 +825,11 @@ public function fetchUniqueWatchedMoviesCount(
$payload[] = $userRatingMax;
}

if ($locationId !== null) {
$whereQuery .= 'AND mh.location_id = ? ';
$payload[] = $locationId;
}

return $this->dbConnection->fetchFirstColumn(
<<<SQL
SELECT COUNT(DISTINCT m.id)
Expand Down Expand Up @@ -837,6 +858,7 @@ public function fetchUniqueWatchedMoviesPaginated(
?bool $hasUserRating,
?int $userRatingMin,
?int $userRatingMax,
?int $locationId,
) : array {
$payload = [$userId, $userId, "%$searchTerm%"];

Expand Down Expand Up @@ -886,6 +908,11 @@ public function fetchUniqueWatchedMoviesPaginated(
$payload[] = $userRatingMax;
}

if ($locationId !== null) {
$whereQuery .= 'AND mh.location_id = ? ';
$payload[] = $locationId;
}

return $this->dbConnection->fetchAllAssociative(
<<<SQL
SELECT * FROM (
Expand Down
8 changes: 8 additions & 0 deletions src/HttpController/Web/Dto/MoviesRequestDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private function __construct(
private readonly ?bool $hasUserRating,
private readonly ?int $userRatingMin,
private readonly ?int $userRatingMax,
private readonly ?int $locationId,
) {
}

Expand All @@ -36,6 +37,7 @@ public static function createFromParameters(
?bool $hasUserRating = null,
?int $userRatingMin = null,
?int $userRatingMax = null,
?int $locationId = null,
) : self {
return new self(
$userId,
Expand All @@ -50,6 +52,7 @@ public static function createFromParameters(
$hasUserRating,
$userRatingMin,
$userRatingMax,
$locationId,
);
}

Expand All @@ -68,6 +71,11 @@ public function getLimit() : int
return $this->limit;
}

public function getLocationId() : ?int
{
return $this->locationId;
}

public function getPage() : int
{
return $this->page;
Expand Down
2 changes: 2 additions & 0 deletions src/HttpController/Web/Mapper/MoviesRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function mapRenderPageRequest(Request $request) : MoviesRequestDto
$userRating = isset($getParameters['ur']) === true ? (bool)$getParameters['ur'] : self::DEFAULT_HAS_USER_RATING;
$userRatingMin = isset($getParameters['minur']) === true ? (int)$getParameters['minur'] : self::DEFAULT_USER_RATING_MIN;
$userRatingMax = isset($getParameters['maxur']) === true ? (int)$getParameters['maxur'] : self::DEFAULT_USER_RATING_MAX;
$locationId = isset($getParameters['loc']) === true ? (int)$getParameters['loc'] : null;

return MoviesRequestDto::createFromParameters(
$userId,
Expand All @@ -63,6 +64,7 @@ public function mapRenderPageRequest(Request $request) : MoviesRequestDto
$userRating,
$userRatingMin,
$userRatingMax,
$locationId,
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/HttpController/Web/MoviesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function renderPage(Request $request) : Response
$requestData->hasUserRating(),
$requestData->getUserRatingMin(),
$requestData->getUserRatingMax(),
$requestData->getLocationId(),
);

$watchedMoviesCount = $this->movieApi->fetchUniqueWatchedMoviesCount(
Expand All @@ -50,6 +51,7 @@ public function renderPage(Request $request) : Response
$requestData->hasUserRating(),
$requestData->getUserRatingMin(),
$requestData->getUserRatingMax(),
$requestData->getLocationId(),
);
$paginationElements = $this->paginationElementsCalculator->createPaginationElements($watchedMoviesCount, $requestData->getLimit(), $requestData->getPage());

Expand All @@ -66,9 +68,11 @@ public function renderPage(Request $request) : Response
'releaseYear' => (string)$requestData->getReleaseYear(),
'language' => (string)$requestData->getLanguage(),
'genre' => (string)$requestData->getGenre(),
'locationId' => $requestData->getLocationId(),
'uniqueReleaseYears' => $this->movieApi->fetchUniqueMovieReleaseYears($requestData->getUserId()),
'uniqueLanguages' => $this->movieApi->fetchUniqueMovieLanguages($requestData->getUserId()),
'uniqueGenres' => $this->movieApi->fetchUniqueMovieGenres($requestData->getUserId()),
'uniqueLocations' => $this->movieApi->fetchUniqueLocations($requestData->getUserId()),
'hasUserRating' => $requestData->hasUserRating(),
'minUserRating' => $requestData->getUserRatingMin(),
'maxUserRating' => $requestData->getUserRatingMax(),
Expand Down
9 changes: 9 additions & 0 deletions templates/component/modal-advanced-search-movie.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
<option value="10" {{ maxUserRating == '10' or maxUserRating is null ? 'selected' }}>10</option>
</select>
</div>
<div class="input-group mb-3">
<span class="input-group-text">Location</span>
<select class="form-control" name="loc" id="searchLocationSelect">
<option value="" {{ locationId is null ? 'selected' }}></option>
{% for uniqueLocation in uniqueLocations %}
<option value="{{ uniqueLocation.id }}" {{ uniqueLocation.id == locationId ? 'selected' }}>{{ uniqueLocation.name }}</option>
{% endfor %}
</select>
</div>

<h6 class="text-center">Pagination</h6>

Expand Down

0 comments on commit e06e9a0

Please sign in to comment.