Skip to content

Commit

Permalink
Merge pull request #613 from leepeuker/add-top-lcations-dashboard-row
Browse files Browse the repository at this point in the history
Add a dashboard row for top locations
  • Loading branch information
leepeuker authored Aug 25, 2024
2 parents e06e9a0 + 4605b3e commit 008b531
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Domain/Movie/History/MovieHistoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ public function fetchTmdbIdsWithoutWatchDateByUserId(int $userId, array $movieId
return $this->movieRepository->fetchTmdbIdsWithoutWatchDateByUserId($userId, $movieIds);
}

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

public function fetchTotalHoursWatched(int $userId) : int
{
$minutes = $this->movieRepository->fetchTotalMinutesWatched($userId);
Expand Down
12 changes: 12 additions & 0 deletions src/Domain/Movie/MovieRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,18 @@ public function fetchTmdbIdsWithoutWatchDateByUserId(int $userId, array $movieId
);
}

public function fetchTopLocations(int $userId) : array
{
return $this->dbConnection->executeQuery(
'SELECT location_id as id, name, COUNT(muwd.movie_id) AS count_plays
FROM location
JOIN movie_user_watch_dates muwd on location.id = muwd.location_id
WHERE location.user_id = ?
GROUP BY location_id',
[$userId],
)->fetchAllAssociative();
}

public function fetchTotalMinutesWatched(int $userId) : int
{
return (int)$this->dbConnection->executeQuery(
Expand Down
1 change: 1 addition & 0 deletions src/HttpController/Web/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function render(Request $request) : Response
'mostWatchedProductionCompanies' => $this->movieHistoryApi->fetchMostWatchedProductionCompanies($userId, 12),
'mostWatchedReleaseYears' => $this->movieHistoryApi->fetchMostWatchedReleaseYears($userId),
'watchlistItems' => $this->movieWatchlistApi->fetchWatchlistPaginated($userId, 6, 1),
'topLocations' => $this->movieHistoryApi->fetchTopLocations($userId),
'dashboardRows' => $dashboardRows,
]),
);
Expand Down
3 changes: 3 additions & 0 deletions src/Service/Dashboard/DashboardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public function createDefaultDashboardRows() : DashboardRowList
DashboardRow::createMostWatchedProductionCompanies(),
DashboardRow::createMostWatchedReleaseYears(),
DashboardRow::createWatchlist(),
DashboardRow::createTopLocations(),
);
}

// phpcs:ignore Generic.Metrics.CyclomaticComplexity
private function createDashboardRowById(int $rowId, bool $isVisible, bool $isExtended) : DashboardRow
{
return match (true) {
Expand All @@ -69,6 +71,7 @@ private function createDashboardRowById(int $rowId, bool $isVisible, bool $isExt
DashboardRow::createMostWatchedProductionCompanies()->getId() === $rowId => DashboardRow::createMostWatchedProductionCompanies($isVisible, $isExtended),
DashboardRow::createMostWatchedReleaseYears()->getId() === $rowId => DashboardRow::createMostWatchedReleaseYears($isVisible, $isExtended),
DashboardRow::createWatchlist()->getId() === $rowId => DashboardRow::createWatchlist($isVisible, $isExtended),
DashboardRow::createTopLocations()->getId() === $rowId => DashboardRow::createTopLocations($isVisible, $isExtended),

default => throw new RuntimeException('Not supported dashboard row id: ' . $rowId)
};
Expand Down
10 changes: 10 additions & 0 deletions src/Service/Dashboard/Dto/DashboardRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static function createWatchlist(bool $isVisible = true, bool $isExtended
return self::create(9, 'Latest in Watchlist', $isVisible, $isExtended);
}

public static function createTopLocations(bool $isVisible = true, bool $isExtended = false) : self
{
return self::create(10, 'Top Locations', $isVisible, $isExtended);
}

private static function create(int $id, string $name, bool $isVisible, bool $isExtended) : self
{
return new self($id, $name, $isVisible, $isExtended);
Expand Down Expand Up @@ -126,4 +131,9 @@ public function isWatchlist() : bool
{
return $this->getId() === self::createWatchlist()->getId();
}

public function isTopLocations() : bool
{
return $this->getId() === self::createTopLocations()->getId();
}
}
24 changes: 24 additions & 0 deletions templates/component/dashboard/row-top-locations.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% macro create(dashboardRow, topLocations, routeUsername) %}

{% import 'component/dashboard/row-item-toggle.html.twig' as rowItemToggle %}
{{ rowItemToggle.create(dashboardRow) }}

<li class="list-group-item {{ dashboardRow.isExtended ? 'activeItem' : 'inactiveItem' }}" style="padding: 1rem 0.4rem 0 0.4rem;">
<div class="row row-cols-3 row-cols-md-3 row-cols-lg-6">
{% for topLocation in topLocations %}
<div class="col" style="padding-bottom: 1rem;">
<div class="card h-100 position-relative"
style="cursor: pointer"
onclick="window.location='/users/{{ routeUsername }}/movies?loc={{ topLocation.id }}'">
<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;">{{ topLocation.name }}</small>
</div>
<div class="card-footer" style="padding: 0.1rem">
<small class="text-muted" style="font-size: 0.9rem;">{{ topLocation.count_plays }}x</small>
</div>
</div>
</div>
{% endfor %}
</div>
</li>
{% endmacro %}
3 changes: 3 additions & 0 deletions templates/page/dashboard.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
{% elseif dashboardRow.isWatchlist and dashboardRow.isVisible %}
{% import "component/dashboard/row-watchlist.html.twig" as dashboardWatchlist %}
{{ dashboardWatchlist.create(dashboardRow, watchlistItems, routeUsername) }}
{% elseif dashboardRow.isTopLocations and dashboardRow.isVisible %}
{% import "component/dashboard/row-top-locations.html.twig" as dashboardTopLocations %}
{{ dashboardTopLocations.create(dashboardRow, topLocations, routeUsername) }}
{% endif %}
{% endfor %}
</div>
Expand Down

0 comments on commit 008b531

Please sign in to comment.