Skip to content

Commit

Permalink
Merge pull request #273 from leepeuker/fix-sqlit-error-when-logging-m…
Browse files Browse the repository at this point in the history
…ovie-on-existing-date

Fix error when logging play on existing date with sqlite
  • Loading branch information
leepeuker authored Feb 3, 2023
2 parents e2c84c6 + 7ede3a8 commit 2637d84
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/Domain/Movie/History/MovieHistoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions src/Domain/Movie/History/MovieHistoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
39 changes: 35 additions & 4 deletions src/Domain/Movie/MovieApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2637d84

Please sign in to comment.