From 188fc6eb868399d2a376a667644b379412796d05 Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Wed, 27 Jul 2022 13:01:32 +0200 Subject: [PATCH] Add user selection --- public/js/app.js | 6 +++++ src/Application/Movie/Api.php | 8 +++---- src/Application/Movie/Repository.php | 14 ++++++----- src/Application/Movie/Service/Select.php | 8 +++---- src/Application/User/Api.php | 10 ++++++++ src/Application/User/Repository.php | 23 +++++++++++++++++++ src/HttpController/DashboardController.php | 1 + src/HttpController/HistoryController.php | 1 + .../MostWatchedActorsController.php | 1 + .../MostWatchedDirectorsController.php | 1 + src/HttpController/MovieController.php | 1 + src/HttpController/PersonController.php | 12 ++++++++-- templates/component/navbar.html.twig | 3 --- templates/component/user-select.html.twig | 11 +++++++++ templates/page/actor.html.twig | 5 +++- templates/page/dashboard.html.twig | 7 ++++-- templates/page/history.html.twig | 4 +++- templates/page/mostWatchedActors.html.twig | 4 +++- templates/page/mostWatchedDirectors.html.twig | 4 +++- templates/page/movie.html.twig | 3 ++- 20 files changed, 101 insertions(+), 26 deletions(-) create mode 100644 templates/component/user-select.html.twig diff --git a/public/js/app.js b/public/js/app.js index d353d322..bc9298c0 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -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 + '/') +} diff --git a/src/Application/Movie/Api.php b/src/Application/Movie/Api.php index 5581ee84..5fe225bb 100644 --- a/src/Application/Movie/Api.php +++ b/src/Application/Movie/Api.php @@ -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 diff --git a/src/Application/Movie/Repository.php b/src/Application/Movie/Repository.php index d2fc4441..eb27ac79 100644 --- a/src/Application/Movie/Repository.php +++ b/src/Application/Movie/Repository.php @@ -420,7 +420,7 @@ 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( <<dbConnection->fetchAllAssociative( <<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 diff --git a/src/Application/User/Api.php b/src/Application/User/Api.php index 92816436..e8c0e9f7 100644 --- a/src/Application/User/Api.php +++ b/src/Application/User/Api.php @@ -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); diff --git a/src/Application/User/Repository.php b/src/Application/User/Repository.php index 7b18fbcd..5892d36b 100644 --- a/src/Application/User/Repository.php +++ b/src/Application/User/Repository.php @@ -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]); diff --git a/src/HttpController/DashboardController.php b/src/HttpController/DashboardController.php index d388f920..8ce3b277 100644 --- a/src/HttpController/DashboardController.php +++ b/src/HttpController/DashboardController.php @@ -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), diff --git a/src/HttpController/HistoryController.php b/src/HttpController/HistoryController.php index 458faf29..0ed5a590 100644 --- a/src/HttpController/HistoryController.php +++ b/src/HttpController/HistoryController.php @@ -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, diff --git a/src/HttpController/MostWatchedActorsController.php b/src/HttpController/MostWatchedActorsController.php index 7a997849..d38825a7 100644 --- a/src/HttpController/MostWatchedActorsController.php +++ b/src/HttpController/MostWatchedActorsController.php @@ -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, diff --git a/src/HttpController/MostWatchedDirectorsController.php b/src/HttpController/MostWatchedDirectorsController.php index 651e94f5..9cc0760a 100644 --- a/src/HttpController/MostWatchedDirectorsController.php +++ b/src/HttpController/MostWatchedDirectorsController.php @@ -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, diff --git a/src/HttpController/MovieController.php b/src/HttpController/MovieController.php index 8f67268c..f39baae3 100644 --- a/src/HttpController/MovieController.php +++ b/src/HttpController/MovieController.php @@ -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), diff --git a/src/HttpController/PersonController.php b/src/HttpController/PersonController.php index 129d7050..2c064eb9 100644 --- a/src/HttpController/PersonController.php +++ b/src/HttpController/PersonController.php @@ -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; @@ -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), ]), ); } diff --git a/templates/component/navbar.html.twig b/templates/component/navbar.html.twig index c4a8f431..9cf42db3 100644 --- a/templates/component/navbar.html.twig +++ b/templates/component/navbar.html.twig @@ -13,9 +13,6 @@ {% endif %}
  • Dashboard
  • -
  • - -
  • History
  • Top Actors
  • Top Directors
  • diff --git a/templates/component/user-select.html.twig b/templates/component/user-select.html.twig new file mode 100644 index 00000000..ff51443c --- /dev/null +++ b/templates/component/user-select.html.twig @@ -0,0 +1,11 @@ +
    +
    + User + +
    +
    diff --git a/templates/page/actor.html.twig b/templates/page/actor.html.twig index edb86e53..39032747 100644 --- a/templates/page/actor.html.twig +++ b/templates/page/actor.html.twig @@ -8,7 +8,10 @@ {% block body %}
    {{ include('component/navbar.html.twig') }} -

    {{ person.name }}

    + {{ include('component/user-select.html.twig') }} + +

    {{ person.name }}

    +
    diff --git a/templates/page/dashboard.html.twig b/templates/page/dashboard.html.twig index a30388c9..eafae0da 100644 --- a/templates/page/dashboard.html.twig +++ b/templates/page/dashboard.html.twig @@ -13,7 +13,9 @@ {% block body %}
    {{ include('component/navbar.html.twig') }} -
    +
    + {{ include('component/user-select.html.twig') }} +

    Overview

    @@ -80,7 +82,8 @@
    {% for mostWatchedActor in mostWatchedActors %}
    -
    +
    {{ mostWatchedActor.name }}
    diff --git a/templates/page/history.html.twig b/templates/page/history.html.twig index 6a4a75a8..194f57bf 100644 --- a/templates/page/history.html.twig +++ b/templates/page/history.html.twig @@ -14,7 +14,9 @@
    {{ include('component/navbar.html.twig') }} -
    +
    + {{ include('component/user-select.html.twig') }} +
    diff --git a/templates/page/mostWatchedActors.html.twig b/templates/page/mostWatchedActors.html.twig index 529df2ab..42079126 100644 --- a/templates/page/mostWatchedActors.html.twig +++ b/templates/page/mostWatchedActors.html.twig @@ -14,7 +14,9 @@
    {{ include('component/navbar.html.twig') }} -
    +
    + {{ include('component/user-select.html.twig') }} +
    diff --git a/templates/page/mostWatchedDirectors.html.twig b/templates/page/mostWatchedDirectors.html.twig index 2b18f78b..60840b7e 100644 --- a/templates/page/mostWatchedDirectors.html.twig +++ b/templates/page/mostWatchedDirectors.html.twig @@ -14,7 +14,9 @@
    {{ include('component/navbar.html.twig') }} -
    +
    + {{ include('component/user-select.html.twig') }} +
    diff --git a/templates/page/movie.html.twig b/templates/page/movie.html.twig index 64f6c2f2..030e8cbb 100644 --- a/templates/page/movie.html.twig +++ b/templates/page/movie.html.twig @@ -18,8 +18,9 @@ {% block body %}
    {{ include('component/navbar.html.twig') }} + {{ include('component/user-select.html.twig') }} -

    {{ movie.title }} ({{ movie.releaseDate|date("Y") }})

    +

    {{ movie.title }} ({{ movie.releaseDate|date("Y") }})