diff --git a/src/Domain/Movie/History/MovieHistoryApi.php b/src/Domain/Movie/History/MovieHistoryApi.php index e91ed678..d6fe7401 100644 --- a/src/Domain/Movie/History/MovieHistoryApi.php +++ b/src/Domain/Movie/History/MovieHistoryApi.php @@ -20,9 +20,9 @@ public function __construct( ) { } - public function createOrUpdatePlaysForDate(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment) : void + public function create(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment = null) : void { - $this->repository->createOrUpdatePlaysForDate($movieId, $userId, $watchedAt, $plays, $comment); + $this->repository->create($movieId, $userId, $watchedAt, $plays, $comment); } public function deleteByUserAndMovieId(int $userId, int $movieId) : void @@ -326,6 +326,11 @@ public function findHistoryEntryForMovieByUserOnDate(int $movieId, int $userId, return $this->movieRepository->findHistoryEntryForMovieByUserOnDate($movieId, $userId, $watchedAt); } + public function update(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment = null) : void + { + $this->repository->update($movieId, $userId, $watchedAt, $plays, $comment); + } + public function updateHistoryComment(int $movieId, int $userId, Date $watchAt, ?string $comment) : void { $this->repository->updateHistoryComment($movieId, $userId, $watchAt, $comment); diff --git a/src/Domain/Movie/History/MovieHistoryRepository.php b/src/Domain/Movie/History/MovieHistoryRepository.php index b4d76045..e0fb8e3a 100644 --- a/src/Domain/Movie/History/MovieHistoryRepository.php +++ b/src/Domain/Movie/History/MovieHistoryRepository.php @@ -11,10 +11,10 @@ public function __construct(private readonly Connection $dbConnection) { } - public function createOrUpdatePlaysForDate(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment) : void + public function create(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment) : void { $this->dbConnection->executeStatement( - 'REPLACE INTO movie_user_watch_dates (movie_id, user_id, watched_at, plays, `comment`) VALUES (?, ?, ?, ?, ?)', + 'INSERT INTO movie_user_watch_dates (movie_id, user_id, watched_at, plays, `comment`) VALUES (?, ?, ?, ?, ?)', [ $movieId, $userId, @@ -48,6 +48,20 @@ public function deleteHistoryByIdAndDate(int $movieId, int $userId, Date $watche ); } + public function update(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment) : void + { + $this->dbConnection->executeStatement( + 'UPDATE movie_user_watch_dates SET `comment` = ?, `plays` = ? WHERE movie_id = ? AND user_id = ? AND watched_at = ?', + [ + $comment, + $plays, + $movieId, + $userId, + (string)$watchedAt, + ], + ); + } + public function updateHistoryComment(int $movieId, int $userId, Date $watchedAt, ?string $comment) : void { $this->dbConnection->executeStatement( diff --git a/src/Domain/Movie/MovieApi.php b/src/Domain/Movie/MovieApi.php index c9020faa..0b61e575 100644 --- a/src/Domain/Movie/MovieApi.php +++ b/src/Domain/Movie/MovieApi.php @@ -314,18 +314,49 @@ public function increaseHistoryPlaysForMovieOnDate(int $movieId, int $userId, Da { $historyEntry = $this->findHistoryEntryForMovieByUserOnDate($movieId, $userId, $watchedDate); - $this->historyApi->createOrUpdatePlaysForDate( + if ($historyEntry === null) { + $this->historyApi->create( + $movieId, + $userId, + $watchedDate, + $playsToAdd, + ); + + return; + } + + $this->historyApi->update( $movieId, $userId, $watchedDate, - (int)$historyEntry?->getPlays() + $playsToAdd, - $historyEntry?->getComment(), + $historyEntry->getPlays() + $playsToAdd, + $historyEntry->getComment(), ); } public function replaceHistoryForMovieByDate(int $movieId, int $userId, Date $watchedAt, int $playsPerDate, ?string $comment = null) : void { - $this->historyApi->createOrUpdatePlaysForDate($movieId, $userId, $watchedAt, $playsPerDate, $comment); + $historyEntry = $this->findHistoryEntryForMovieByUserOnDate($movieId, $userId, $watchedAt); + + if ($historyEntry === null) { + $this->historyApi->create( + $movieId, + $userId, + $watchedAt, + $playsPerDate, + $comment + ); + + return; + } + + $this->historyApi->update( + $movieId, + $userId, + $watchedAt, + $playsPerDate, + $comment, + ); } public function updateCast(int $movieId, TmdbCast $tmdbCast) : void