Skip to content

Commit

Permalink
Merge pull request #71 from leepeuker/add-user-selection
Browse files Browse the repository at this point in the history
Add user selection
  • Loading branch information
leepeuker authored Jul 27, 2022
2 parents 2de9734 + 188fc6e commit 679ce1c
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 26 deletions.
6 changes: 6 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ if ('serviceWorker' in navigator) {
.catch(err => console.log('service worker not registered', err))
})
}

function changeUserContext (e) {
const currentUrlPath = window.location.pathname

window.location.href = currentUrlPath.replace(/\/[a-zA-Z0-9]+\//, '/' + e.value + '/')
}
8 changes: 4 additions & 4 deletions src/Application/Movie/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ public function fetchHistoryOrderedByWatchedAtDesc(int $userId) : array
return $this->historySelectService->fetchHistoryOrderedByWatchedAtDesc($userId);
}

public function fetchWithActor(int $personId) : EntityList
public function fetchWithActor(int $personId, int $userId) : EntityList
{
return $this->movieSelectService->fetchWithActor($personId);
return $this->movieSelectService->fetchWithActor($personId, $userId);
}

public function fetchWithDirector(int $personId) : EntityList
public function fetchWithDirector(int $personId, int $userId) : EntityList
{
return $this->movieSelectService->fetchWithDirector($personId);
return $this->movieSelectService->fetchWithDirector($personId, $userId);
}

public function findById(int $movieId) : ?array
Expand Down
14 changes: 8 additions & 6 deletions src/Application/Movie/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,33 +420,35 @@ public function fetchUniqueMovieInHistoryCount(int $userId) : int
)->fetchFirstColumn()[0];
}

public function fetchWithActor(int $personId) : array
public function fetchWithActor(int $personId, int $userId) : array
{
return $this->dbConnection->fetchAllAssociative(
<<<SQL
SELECT m.*
FROM movie m
JOIN movie_cast mc ON m.id = mc.movie_id
JOIN person p ON mc.person_id = p.id
WHERE p.id = ? AND m.id IN (SELECT DISTINCT movie_id FROM movie_user_watch_dates mh)
JOIN movie_user_watch_dates muwd ON m.id = muwd.movie_id
WHERE p.id = ? AND m.id IN (SELECT DISTINCT movie_id FROM movie_user_watch_dates mh) AND muwd.user_id = ?
ORDER BY m.title
SQL,
[$personId]
[$personId, $userId]
);
}

public function fetchWithDirector(int $personId) : array
public function fetchWithDirector(int $personId, int $userId) : array
{
return $this->dbConnection->fetchAllAssociative(
<<<SQL
SELECT m.*
FROM movie m
JOIN movie_crew mc ON m.id = mc.movie_id AND job = "Director"
JOIN person p ON mc.person_id = p.id
WHERE p.id = ? AND m.id IN (SELECT DISTINCT movie_id FROM movie_user_watch_dates mh)
JOIN movie_user_watch_dates muwd ON m.id = muwd.movie_id
WHERE p.id = ? AND m.id IN (SELECT DISTINCT movie_id FROM movie_user_watch_dates mh) AND muwd.user_id = ?
ORDER BY m.title
SQL,
[$personId]
[$personId, $userId]
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Application/Movie/Service/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function fetchAllOrderedByLastUpdatedAtTmdbAsc() : EntityList
return $this->repository->fetchAllOrderedByLastUpdatedAtTmdbAsc();
}

public function fetchWithActor(int $personId) : EntityList
public function fetchWithActor(int $personId, int $userId) : EntityList
{
return EntityList::createFromArray($this->repository->fetchWithActor($personId));
return EntityList::createFromArray($this->repository->fetchWithActor($personId, $userId));
}

public function fetchWithDirector(int $personId) : EntityList
public function fetchWithDirector(int $personId, int $userId) : EntityList
{
return EntityList::createFromArray($this->repository->fetchWithDirector($personId));
return EntityList::createFromArray($this->repository->fetchWithDirector($personId, $userId));
}

public function findById(int $movieId) : ?Entity
Expand Down
10 changes: 10 additions & 0 deletions src/Application/User/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public function fetchAll() : array
return $this->repository->fetchAll();
}

public function fetchAllHavingWatchedMovie(int $movieId) : array
{
return $this->repository->fetchAllHavingWatchedMovie($movieId);
}

public function fetchAllHavingWatchedMoviesWithPerson(int $personId) : array
{
return $this->repository->fetchAllHavingWatchedMoviesWithPerson($personId);
}

public function fetchDateFormatId(int $userId) : int
{
$dateFormat = $this->repository->findDateFormatId($userId);
Expand Down
23 changes: 23 additions & 0 deletions src/Application/User/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ public function fetchAll() : array
return $this->dbConnection->fetchAllAssociative('SELECT * FROM `user`');
}

public function fetchAllHavingWatchedMovie(int $movieId) : array
{
return $this->dbConnection->fetchAllAssociative(
'SELECT user.name
FROM `user`
JOIN movie_user_watch_dates muwd ON user.id = muwd.user_id
WHERE movie_id = ?',
[$movieId]
);
}

public function fetchAllHavingWatchedMoviesWithPerson(int $personId) : array
{
return $this->dbConnection->fetchAllAssociative(
'SELECT user.name
FROM `user`
JOIN movie_user_watch_dates muwd ON user.id = muwd.user_id
JOIN movie_cast mc ON muwd.movie_id = mc.movie_id
WHERE person_id = ?',
[$personId]
);
}

public function findAuthTokenExpirationDate(string $token) : ?DateTime
{
$expirationDate = $this->dbConnection->fetchOne('SELECT `expiration_date` FROM `user_auth_token` WHERE `token` = ?', [$token]);
Expand Down
1 change: 1 addition & 0 deletions src/HttpController/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function render(Request $request) : Response
return Response::create(
StatusCode::createOk(),
$this->twig->render('page/dashboard.html.twig', [
'users' => $this->userApi->fetchAll(),
'totalPlayCount' => $this->movieApi->fetchHistoryCount($userId),
'uniqueMoviesCount' => $this->movieApi->fetchHistoryCountUnique($userId),
'totalHoursWatched' => $this->movieHistorySelectService->fetchTotalHoursWatched($userId),
Expand Down
1 change: 1 addition & 0 deletions src/HttpController/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function renderHistory(Request $request) : Response
return Response::create(
StatusCode::createOk(),
$this->twig->render('page/history.html.twig', [
'users' => $this->userApi->fetchAll(),
'historyEntries' => $historyPaginated,
'paginationElements' => $paginationElements,
'searchTerm' => $searchTerm,
Expand Down
1 change: 1 addition & 0 deletions src/HttpController/MostWatchedActorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function renderPage(Request $request) : Response
return Response::create(
StatusCode::createOk(),
$this->twig->render('page/mostWatchedActors.html.twig', [
'users' => $this->userApi->fetchAll(),
'mostWatchedActors' => $mostWatchedActors,
'paginationElements' => $paginationElements,
'searchTerm' => $searchTerm,
Expand Down
1 change: 1 addition & 0 deletions src/HttpController/MostWatchedDirectorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function renderPage(Request $request) : Response
return Response::create(
StatusCode::createOk(),
$this->twig->render('page/mostWatchedDirectors.html.twig', [
'users' => $this->userApi->fetchAll(),
'mostWatchedDirectors' => $mostWatchedActors,
'paginationElements' => $paginationElements,
'searchTerm' => $searchTerm,
Expand Down
1 change: 1 addition & 0 deletions src/HttpController/MovieController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function renderPage(Request $request) : Response
return Response::create(
StatusCode::createOk(),
$this->twig->render('page/movie.html.twig', [
'users' => $this->userApi->fetchAllHavingWatchedMovie($movieId),
'movie' => $movie,
'movieGenres' => $this->movieApi->findGenresByMovieId($movieId),
'castMembers' => $this->movieApi->findCastByMovieId($movieId),
Expand Down
12 changes: 10 additions & 2 deletions src/HttpController/PersonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Movary\Application\Movie;
use Movary\Application\Person;
use Movary\Application\User;
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
use Movary\ValueObject\Http\StatusCode;
Expand All @@ -14,20 +15,27 @@ class PersonController
public function __construct(
private readonly Person\Api $personApi,
private readonly Movie\Api $movieApi,
private readonly User\Api $userApi,
private readonly Environment $twig,
) {
}

public function renderPage(Request $request) : Response
{
$userId = $this->userApi->findUserByName((string)$request->getRouteParameters()['username'])?->getId();
if ($userId === null) {
return Response::createNotFound();
}

$personId = (int)$request->getRouteParameters()['id'];

return Response::create(
StatusCode::createOk(),
$this->twig->render('page/actor.html.twig', [
'users' => $this->userApi->fetchAllHavingWatchedMoviesWithPerson($personId),
'person' => $this->personApi->findById($personId),
'moviesAsActor' => $this->movieApi->fetchWithActor($personId),
'moviesAsDirector' => $this->movieApi->fetchWithDirector($personId),
'moviesAsActor' => $this->movieApi->fetchWithActor($personId, $userId),
'moviesAsDirector' => $this->movieApi->fetchWithDirector($personId, $userId),
]),
);
}
Expand Down
3 changes: 0 additions & 3 deletions templates/component/navbar.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
</li>
{% endif %}
<li><a class="dropdown-item {% if requestUrlPath matches '#\/.+\/dashboard#' %}active{% endif %}" href="/{{ routeUsername }}/dashboard">Dashboard</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item {% if requestUrlPath matches '#\/.+\/history#' %}active{% endif %}" href="/{{ routeUsername }}/history">History</a></li>
<li><a class="dropdown-item {% if requestUrlPath matches '#\/.+\/most-watched-actors#' %}active{% endif %}" href="/{{ routeUsername }}/most-watched-actors">Top Actors</a></li>
<li><a class="dropdown-item {% if requestUrlPath matches '#\/.+\/most-watched-directors#' %}active{% endif %}" href="/{{ routeUsername }}/most-watched-directors">Top Directors</a></li>
Expand Down
11 changes: 11 additions & 0 deletions templates/component/user-select.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div style="margin-top: 1rem;margin-bottom: 1rem">
<div class="input-group">
<span class="input-group-text">User</span>
<select class="form-select" onchange="changeUserContext(this)">
<option disabled>Select user</option>
{% for user in users %}
<option value="{{ user.name }}" {% if routeUsername == user.name %}selected{% endif %}>{{ user.name }}</option>
{% endfor %}
</select>
</div>
</div>
5 changes: 4 additions & 1 deletion templates/page/actor.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
{% block body %}
<main role="main" class="container">
{{ include('component/navbar.html.twig') }}
<h2 style="display: flex;padding-top: 0.5rem;">{{ person.name }}</h2>
{{ include('component/user-select.html.twig') }}

<h2 style="display: flex;">{{ person.name }}</h2>

<div style="display: flex;">
<div style="text-align: center">
<img src="{{ tmdb.generatePosterImageUrl(person.tmdbPosterPath) }}" width="200rem" style="width: 11rem;align-self: start;">
Expand Down
7 changes: 5 additions & 2 deletions templates/page/dashboard.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
{% block body %}
<main role="main" class="container">
{{ include('component/navbar.html.twig') }}
<div style="text-align: center;padding-top: 1rem">
<div style="text-align: center">
{{ include('component/user-select.html.twig') }}

<h2 style="padding-bottom: 0.5rem">Overview</h2>
<div class="row justify-content-md-center">
<div class="col ">
Expand Down Expand Up @@ -80,7 +82,8 @@
<div class="row row-cols-3 row-cols-md-3 row-cols-lg-6">
{% for mostWatchedActor in mostWatchedActors %}
<div class="col" style="padding-bottom: 1rem;">
<div class="card h-100 position-relative" onclick="window.location='/{{ routeUsername }}/person/{{ mostWatchedActor.id }}'" style="cursor: pointer;">
<div class="card h-100 position-relative" onclick="window.location='/{{ routeUsername }}/person/{{ mostWatchedActor.id }}'"
style="cursor: pointer;">
<div class="card-header text-truncate" style="padding: 0.2rem">
<small class="text-muted" style="font-size: 0.9rem;">{{ mostWatchedActor.name }}</small>
</div>
Expand Down
4 changes: 3 additions & 1 deletion templates/page/history.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<main role="main" class="container">
{{ include('component/navbar.html.twig') }}

<div style="text-align: center;padding-top: 1rem">
<div style="text-align: center">
{{ include('component/user-select.html.twig') }}

<form action="/{{ routeUsername }}/history" method="GET">
<div class="input-group mb-3">
<input type="text" class="form-control" name="s" placeholder="Search" value="{{ (searchTerm is null) ? '' : searchTerm }}">
Expand Down
4 changes: 3 additions & 1 deletion templates/page/mostWatchedActors.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<main role="main" class="container">
{{ include('component/navbar.html.twig') }}

<div style="text-align: center;padding-top: 1rem">
<div style="text-align: center;">
{{ include('component/user-select.html.twig') }}

<form action="/most-watched-actors" method="GET">
<div class="input-group mb-3">
<input type="text" class="form-control" name="s" placeholder="Search" value="{{ (searchTerm is null) ? '' : searchTerm }}">
Expand Down
4 changes: 3 additions & 1 deletion templates/page/mostWatchedDirectors.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<main role="main" class="container">
{{ include('component/navbar.html.twig') }}

<div style="text-align: center;padding-top: 1rem">
<div style="text-align: center;m">
{{ include('component/user-select.html.twig') }}

<form action="/most-watched-directors" method="GET">
<div class="input-group mb-3">
<input type="text" class="form-control" name="s" placeholder="Search" value="{{ (searchTerm is null) ? '' : searchTerm }}">
Expand Down
3 changes: 2 additions & 1 deletion templates/page/movie.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
{% block body %}
<main role="main" class="container">
{{ include('component/navbar.html.twig') }}
{{ include('component/user-select.html.twig') }}

<h2 style="display: flex;padding-top: 0.5rem;margin-bottom: 0.3rem">{{ movie.title }} ({{ movie.releaseDate|date("Y") }})</h2>
<h2 style="display: flex;margin-bottom: 0.3rem">{{ movie.title }} ({{ movie.releaseDate|date("Y") }})</h2>


<input type="hidden" id="movieId" value="{{ movie.id }}">
Expand Down

0 comments on commit 679ce1c

Please sign in to comment.