From ccf6090ee3937d605bf3b9237c66a73ac2b15d79 Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Fri, 3 Feb 2023 21:04:31 +0100 Subject: [PATCH 1/2] Fix error when logging play on existing date with sqlite --- src/Domain/Movie/History/MovieHistoryApi.php | 9 ++++- .../Movie/History/MovieHistoryRepository.php | 19 ++++++++- src/Domain/Movie/MovieApi.php | 39 +++++++++++++++++-- 3 files changed, 59 insertions(+), 8 deletions(-) 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..446261be 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,21 @@ public function deleteHistoryByIdAndDate(int $movieId, int $userId, Date $watche ); } + public function update(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment) : void + { + file_put_contents(__DIR__ . '/txt', "$movieId $userId, $watchedAt, $plays, $comment"); + $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 From 7ede3a8dce085f440a4e849160d77c26dc52d4d1 Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Fri, 3 Feb 2023 21:05:22 +0100 Subject: [PATCH 2/2] Remove debugging code --- src/Domain/Movie/History/MovieHistoryRepository.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Domain/Movie/History/MovieHistoryRepository.php b/src/Domain/Movie/History/MovieHistoryRepository.php index 446261be..e0fb8e3a 100644 --- a/src/Domain/Movie/History/MovieHistoryRepository.php +++ b/src/Domain/Movie/History/MovieHistoryRepository.php @@ -50,7 +50,6 @@ public function deleteHistoryByIdAndDate(int $movieId, int $userId, Date $watche public function update(int $movieId, int $userId, Date $watchedAt, int $plays, ?string $comment) : void { - file_put_contents(__DIR__ . '/txt', "$movieId $userId, $watchedAt, $plays, $comment"); $this->dbConnection->executeStatement( 'UPDATE movie_user_watch_dates SET `comment` = ?, `plays` = ? WHERE movie_id = ? AND user_id = ? AND watched_at = ?', [