Skip to content

Commit

Permalink
Merge pull request #43 from leepeuker/watch-date-constraint-issue
Browse files Browse the repository at this point in the history
Fix issue with unique constraint on movie watch date
  • Loading branch information
leepeuker authored Jul 11, 2022
2 parents e4ed0d2 + 788357a commit b8ce2d6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class SetCorrectConstraintForMovieWatchDates extends AbstractMigration
{
public function down() : void
{
$this->execute(
<<<SQL
ALTER TABLE movie_user_watch_dates DROP PRIMARY KEY, ADD UNIQUE INDEX combinedKey (movie_id, watched_at);
SQL
);
}

public function up() : void
{
$this->execute(
<<<SQL
ALTER TABLE movie_user_watch_dates DROP CONSTRAINT combinedKey, ADD PRIMARY KEY (movie_id, user_id, watched_at);
SQL
);
}
}
2 changes: 1 addition & 1 deletion src/Application/Movie/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,6 @@ public function updateUserRating(int $movieId, int $userId, ?PersonalRating $rat
return;
}

$this->movieUpdateService->setPersonalRating($movieId, $userId, $rating);
$this->movieUpdateService->setUserRating($movieId, $userId, $rating);
}
}
12 changes: 6 additions & 6 deletions src/Application/Movie/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,19 +534,19 @@ public function updateLetterboxdId(int $id, string $letterboxdId) : void
$this->dbConnection->update('movie', ['letterboxd_id' => $letterboxdId], ['id' => $id]);
}

public function updatePersonalRating(int $id, int $userId, PersonalRating $personalRating) : void
public function updateTraktId(int $id, TraktId $traktId) : void
{
$this->dbConnection->update('movie', ['trakt_id' => $traktId->asInt()], ['id' => $id]);
}

public function updateUserRating(int $id, int $userId, PersonalRating $personalRating) : void
{
$this->dbConnection->executeQuery(
'INSERT INTO movie_user_rating (movie_id, user_id, rating) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE rating=?',
[$id, $userId, $personalRating, $personalRating]
);
}

public function updateTraktId(int $id, TraktId $traktId) : void
{
$this->dbConnection->update('movie', ['trakt_id' => $traktId->asInt()], ['id' => $id]);
}

private function fetchById(int $id) : Entity
{
$data = $this->dbConnection->fetchAssociative('SELECT * FROM `movie` WHERE id = ?', [$id]);
Expand Down
10 changes: 5 additions & 5 deletions src/Application/Movie/Service/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function __construct(
) {
}

public function setUserRating(int $id, int $userId, PersonalRating $rating) : void
{
$this->repository->updateUserRating($id, $userId, $rating);
}

public function updateCast(int $movieId, Cast $tmdbCast) : void
{
$this->movieCasteDeleteService->deleteByMovieId($movieId);
Expand Down Expand Up @@ -103,11 +108,6 @@ public function updateProductionCompanies(int $movieId, Company\EntityList $genr
}
}

public function setPersonalRating(int $id, int $userId, PersonalRating $rating) : void
{
$this->repository->updatePersonalRating($id, $userId, $rating);
}

public function updateTraktId(int $movieId, TraktId $traktId) : void
{
$this->repository->updateTraktId($movieId, $traktId);
Expand Down

0 comments on commit b8ce2d6

Please sign in to comment.