Skip to content

Commit

Permalink
Merge pull request #181 from leepeuker/fix-broken-rating-updates
Browse files Browse the repository at this point in the history
Fix issue with broken rating updates
  • Loading branch information
leepeuker authored Dec 23, 2022
2 parents c8ca042 + b32824d commit 78b3e29
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Domain/Movie/History/MovieHistoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/Domain/Movie/MovieApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
56 changes: 27 additions & 29 deletions src/Domain/Movie/MovieRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -744,14 +754,9 @@ public function findUserRating(int $movieId, int $userId) : ?PersonalRating

public function insertUserRating(int $movieId, int $userId, PersonalRating $rating) : 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, $rating->asInt(), (string)DateTime::create()],
);
}

Expand Down Expand Up @@ -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 $rating) : 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 = ?',
[$rating->asInt(), (string)DateTime::create(), $movieId, $userId],
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/ValueObject/PersonalRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public function asInt() : int
{
return $this->rating;
}

public function isEqual(PersonalRating $personalRating) : bool
{
return $this->asInt() === $personalRating->asInt();
}
}
11 changes: 11 additions & 0 deletions tests/unit/ValueObject/PersonalRatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 78b3e29

Please sign in to comment.