From a21c6ecaa9827ed191823840ba6aad94a665bd5a Mon Sep 17 00:00:00 2001 From: mkelvers Date: Tue, 21 Apr 2026 01:56:19 +0200 Subject: [PATCH] fix: skip watchlist progress update for already-completed shows on finish --- api/playback/progress.go | 43 +++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/api/playback/progress.go b/api/playback/progress.go index 2020539..1c7c586 100644 --- a/api/playback/progress.go +++ b/api/playback/progress.go @@ -30,13 +30,24 @@ func (s *Service) SaveProgress(ctx context.Context, userID string, animeID int64 } } - if err := txQueries.SaveWatchProgress(ctx, database.SaveWatchProgressParams{ - CurrentEpisode: sql.NullInt64{Int64: int64(episode), Valid: true}, - CurrentTimeSeconds: timeSeconds, - UserID: userID, - AnimeID: animeID, - }); err != nil && !errors.Is(err, sql.ErrNoRows) { - return fmt.Errorf("failed to save watchlist progress: %w", err) + watchListEntry, watchListErr := txQueries.GetWatchListEntry(ctx, database.GetWatchListEntryParams{ + UserID: userID, + AnimeID: animeID, + }) + if watchListErr != nil && !errors.Is(watchListErr, sql.ErrNoRows) { + return fmt.Errorf("failed to load watchlist entry: %w", watchListErr) + } + + isCompleted := watchListErr == nil && watchListEntry.Status == "completed" + if !isCompleted { + if err := txQueries.SaveWatchProgress(ctx, database.SaveWatchProgressParams{ + CurrentEpisode: sql.NullInt64{Int64: int64(episode), Valid: true}, + CurrentTimeSeconds: timeSeconds, + UserID: userID, + AnimeID: animeID, + }); err != nil && !errors.Is(err, sql.ErrNoRows) { + return fmt.Errorf("failed to save watchlist progress: %w", err) + } } if _, err := txQueries.UpsertContinueWatchingEntry(ctx, database.UpsertContinueWatchingEntryParams{ @@ -95,6 +106,15 @@ func (s *Service) CompleteAnime(ctx context.Context, userID string, animeID int6 }); err != nil { return fmt.Errorf("failed to mark watchlist as completed: %w", err) } + + if err := txQueries.SaveWatchProgress(ctx, database.SaveWatchProgressParams{ + CurrentEpisode: sql.NullInt64{Int64: int64(episode), Valid: true}, + CurrentTimeSeconds: 0, + UserID: userID, + AnimeID: animeID, + }); err != nil && !errors.Is(err, sql.ErrNoRows) { + return fmt.Errorf("failed to reset watch progress: %w", err) + } } if err := txQueries.DeleteContinueWatchingEntry(ctx, database.DeleteContinueWatchingEntryParams{ @@ -104,15 +124,6 @@ func (s *Service) CompleteAnime(ctx context.Context, userID string, animeID int6 return fmt.Errorf("failed to clear continue entry: %w", err) } - if err := txQueries.SaveWatchProgress(ctx, database.SaveWatchProgressParams{ - CurrentEpisode: sql.NullInt64{Int64: int64(episode), Valid: true}, - CurrentTimeSeconds: 0, - UserID: userID, - AnimeID: animeID, - }); err != nil && !errors.Is(err, sql.ErrNoRows) { - return fmt.Errorf("failed to reset watch progress: %w", err) - } - if err := tx.Commit(); err != nil { return fmt.Errorf("failed to commit complete anime transaction: %w", err) }