From d13af31fd2ce80ea4f4eae9d79f515825313d1c3 Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Fri, 23 Dec 2022 15:09:11 +0100 Subject: [PATCH 1/3] Fix issue with broken rating updates --- src/Domain/Movie/History/MovieHistoryApi.php | 2 +- src/Domain/Movie/MovieApi.php | 12 +++- src/Domain/Movie/MovieRepository.php | 58 ++++++++++---------- src/ValueObject/PersonalRating.php | 5 ++ 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/Domain/Movie/History/MovieHistoryApi.php b/src/Domain/Movie/History/MovieHistoryApi.php index a8a28cf5..c237c03d 100644 --- a/src/Domain/Movie/History/MovieHistoryApi.php +++ b/src/Domain/Movie/History/MovieHistoryApi.php @@ -74,7 +74,7 @@ public function fetchActors( public function fetchAveragePersonalRating(int $userId) : float { - return round($this->movieRepository->fetchPersonalRating($userId), 1); + return round($this->movieRepository->fetchAveragePersonalRating($userId), 1); } public function fetchAveragePlaysPerDay(int $userId) : float diff --git a/src/Domain/Movie/MovieApi.php b/src/Domain/Movie/MovieApi.php index 4fb9a6bc..b51e86e0 100644 --- a/src/Domain/Movie/MovieApi.php +++ b/src/Domain/Movie/MovieApi.php @@ -402,10 +402,18 @@ public function updateUserRating(int $movieId, int $userId, ?PersonalRating $rat return; } - if ($this->repository->updateUserRating($movieId, $userId, $rating) > 0) { + $currentRating = $this->repository->findPersonalMovieRating($movieId, $userId); + + if ($currentRating === null) { + $this->repository->insertUserRating($movieId, $userId, $rating); + + return; + } + + if ($currentRating->isEqual($rating) === true) { return; } - $this->repository->insertUserRating($movieId, $userId, $rating); + $this->repository->updateUserRating($movieId, $userId, $rating); } } diff --git a/src/Domain/Movie/MovieRepository.php b/src/Domain/Movie/MovieRepository.php index ff902f7a..b466a7a9 100644 --- a/src/Domain/Movie/MovieRepository.php +++ b/src/Domain/Movie/MovieRepository.php @@ -176,6 +176,16 @@ public function fetchAllOrderedByLastUpdatedAtTmdbAsc(?int $limit = null) : \Tra return $this->dbConnection->prepare($query)->executeQuery()->iterateAssociative(); } + public function fetchAveragePersonalRating(int $userId) : float + { + return (float)$this->dbConnection->fetchFirstColumn( + 'SELECT AVG(rating) + FROM movie_user_rating + WHERE user_id = ?', + [$userId], + )[0]; + } + public function fetchAverageRuntime(int $userId) : float { return (float)$this->dbConnection->executeQuery( @@ -439,16 +449,6 @@ public function fetchMoviesByProductionCompany(int $productionCompanyId, int $us ); } - public function fetchPersonalRating(int $userId) : float - { - return (float)$this->dbConnection->fetchFirstColumn( - 'SELECT AVG(rating) - FROM movie_user_rating - WHERE user_id = ?', - [$userId], - )[0]; - } - public function fetchPlaysForMovieIdAtDate(int $movieId, int $userId, Date $watchedAt) : int { $result = $this->dbConnection->fetchOne( @@ -718,6 +718,16 @@ public function findByTraktId(TraktId $traktId) : ?MovieEntity return $data === false ? null : MovieEntity::createFromArray($data); } + public function findPersonalMovieRating(int $movieId, int $userId) : ?PersonalRating + { + $data = $this->dbConnection->fetchOne( + 'SELECT * FROM `movie_user_rating` WHERE movie_id = ? AND user_id = ?', + [$movieId, $userId], + ); + + return $data === false ? null : PersonalRating::create($data); + } + public function findPlaysForMovieIdAndDate(int $movieId, int $userId, Date $watchedAt) : ?int { $result = $this->dbConnection->fetchFirstColumn( @@ -742,16 +752,11 @@ public function findUserRating(int $movieId, int $userId) : ?PersonalRating return $userRating !== null ? PersonalRating::create($userRating) : null; } - public function insertUserRating(int $movieId, int $userId, PersonalRating $rating) : void + public function insertUserRating(int $movieId, int $userId, PersonalRating $newPersonalRating) : void { - $this->dbConnection->insert( - 'movie_user_rating', - [ - 'movie_id' => $movieId, - 'user_id' => $userId, - 'rating' => $rating->asInt(), - 'created_at' => (string)DateTime::create(), - ], + $this->dbConnection->executeQuery( + 'INSERT INTO movie_user_rating (movie_id, user_id, rating, created_at) VALUES (?, ?, ?, ?)', + [$movieId, $userId, $newPersonalRating->asInt(), (string)DateTime::create()], ); } @@ -808,18 +813,11 @@ public function updateTraktId(int $id, TraktId $traktId) : void $this->dbConnection->update('movie', ['trakt_id' => $traktId->asInt(), 'updated_at' => (string)DateTime::create()], ['id' => $id]); } - public function updateUserRating(int $movieId, int $userId, PersonalRating $personalRating) : int + public function updateUserRating(int $movieId, int $userId, PersonalRating $newPersonalRating) : void { - return (int)$this->dbConnection->update( - 'movie_user_rating', - [ - 'rating' => $personalRating->asInt() - ], - [ - 'movie_id' => $movieId, - 'user_id' => $userId, - 'updated_at' => (string)DateTime::create(), - ], + $this->dbConnection->executeQuery( + 'UPDATE movie_user_rating SET rating = ?, updated_at = ? WHERE movie_id = ? AND user_id = ?', + [$newPersonalRating->asInt(), (string)DateTime::create(), $movieId, $userId], ); } diff --git a/src/ValueObject/PersonalRating.php b/src/ValueObject/PersonalRating.php index 30c06bfa..4f8365f2 100644 --- a/src/ValueObject/PersonalRating.php +++ b/src/ValueObject/PersonalRating.php @@ -29,4 +29,9 @@ public function asInt() : int { return $this->rating; } + + public function isEqual(PersonalRating $personalRating) : bool + { + return $this->asInt() === $personalRating->asInt(); + } } From e5835a32d0df8d7c23d3121199d899c1a36b8937 Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Fri, 23 Dec 2022 15:10:20 +0100 Subject: [PATCH 2/3] Naming improvments --- src/Domain/Movie/MovieRepository.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Domain/Movie/MovieRepository.php b/src/Domain/Movie/MovieRepository.php index b466a7a9..17a9be6a 100644 --- a/src/Domain/Movie/MovieRepository.php +++ b/src/Domain/Movie/MovieRepository.php @@ -752,11 +752,11 @@ public function findUserRating(int $movieId, int $userId) : ?PersonalRating return $userRating !== null ? PersonalRating::create($userRating) : null; } - public function insertUserRating(int $movieId, int $userId, PersonalRating $newPersonalRating) : void + public function insertUserRating(int $movieId, int $userId, PersonalRating $rating) : void { $this->dbConnection->executeQuery( 'INSERT INTO movie_user_rating (movie_id, user_id, rating, created_at) VALUES (?, ?, ?, ?)', - [$movieId, $userId, $newPersonalRating->asInt(), (string)DateTime::create()], + [$movieId, $userId, $rating->asInt(), (string)DateTime::create()], ); } @@ -813,11 +813,11 @@ public function updateTraktId(int $id, TraktId $traktId) : void $this->dbConnection->update('movie', ['trakt_id' => $traktId->asInt(), 'updated_at' => (string)DateTime::create()], ['id' => $id]); } - public function updateUserRating(int $movieId, int $userId, PersonalRating $newPersonalRating) : void + public function updateUserRating(int $movieId, int $userId, PersonalRating $rating) : void { $this->dbConnection->executeQuery( 'UPDATE movie_user_rating SET rating = ?, updated_at = ? WHERE movie_id = ? AND user_id = ?', - [$newPersonalRating->asInt(), (string)DateTime::create(), $movieId, $userId], + [$rating->asInt(), (string)DateTime::create(), $movieId, $userId], ); } From b32824dd8f8c1b2a5ae6828f3c99e2054bb0953c Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Fri, 23 Dec 2022 15:13:46 +0100 Subject: [PATCH 3/3] Extend unit test --- tests/unit/ValueObject/PersonalRatingTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/unit/ValueObject/PersonalRatingTest.php b/tests/unit/ValueObject/PersonalRatingTest.php index ddf130be..b332a64f 100644 --- a/tests/unit/ValueObject/PersonalRatingTest.php +++ b/tests/unit/ValueObject/PersonalRatingTest.php @@ -32,6 +32,17 @@ public function testCreateThrowsExceptionIfPersonalRatingIsLowerThanOne() : void PersonalRating::create(0); } + public function testIsEqual() : void + { + $subject = PersonalRating::create(5); + + $equalToSubject = PersonalRating::create(5); + $notEqualToSubject = PersonalRating::create(4); + + self::assertTrue($subject->isEqual($equalToSubject)); + self::assertFalse($subject->isEqual($notEqualToSubject)); + } + public function testToString() : void { $subject = PersonalRating::create(5);