Skip to content

Commit

Permalink
Fix regression in sync after addition of Recycle Bin
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Dec 2, 2024
1 parent 3197783 commit 7b44c6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/J.App/LibraryProviderAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ await MutateAsync(
() =>
{
foreach (var id in movieIds)
libraryProvider.DeleteMovie(id);
libraryProvider.MoveMovieToRecycleBin(id);
},
updateProgress,
cancel
Expand All @@ -218,7 +218,7 @@ await MutateAsync(
() =>
{
foreach (var id in movieIds)
libraryProvider.RestoreMovie(id);
libraryProvider.RestoreMovieFromRecycleBin(id);
},
updateProgress,
cancel
Expand All @@ -236,7 +236,7 @@ await MutateAsync(
() =>
{
foreach (var id in movieIds)
libraryProvider.PermanentlyDeleteMovie(id);
libraryProvider.PermanentlyDeleteMovieFromRecycleBin(id);
},
updateProgress,
cancel
Expand Down
35 changes: 20 additions & 15 deletions src/J.Core/LibraryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,13 @@ public void UpdateMovie(Movie movie) =>
}
);

public void DeleteMovie(MovieId id)
public void DeleteMovieInDatabase(MovieId id)
{
Execute(
"""
UPDATE movies SET deleted = 1 WHERE id = @id;
DELETE FROM movie_files WHERE movie_id = @id;
DELETE FROM movie_tags WHERE movie_id = @id;
DELETE FROM movies WHERE id = @id;
""",
p =>
{
Expand All @@ -364,11 +366,11 @@ public void DeleteMovie(MovieId id)
);
}

public void RestoreMovie(MovieId id)
public void MoveMovieToRecycleBin(MovieId id)
{
Execute(
"""
UPDATE movies SET deleted = 0 WHERE id = @id;
UPDATE movies SET deleted = 1 WHERE id = @id;
""",
p =>
{
Expand All @@ -377,25 +379,28 @@ public void RestoreMovie(MovieId id)
);
}

public void PermanentlyDeleteMovie(MovieId id)
public void RestoreMovieFromRecycleBin(MovieId id)
{
var movie = GetMovie(id);

// Let's double check that this was from the recycle bin.
if (!movie.Deleted)
throw new Exception("This movie is not in the Recycle Bin.");

Execute(
"""
DELETE FROM movie_files WHERE movie_id = @id;
DELETE FROM movie_tags WHERE movie_id = @id;
DELETE FROM movies WHERE id = @id;
UPDATE movies SET deleted = 0 WHERE id = @id;
""",
p =>
{
p.AddWithValue("@id", id.Value);
}
);
}

public void PermanentlyDeleteMovieFromRecycleBin(MovieId id)
{
var movie = GetMovie(id);

// Let's double check that this was from the recycle bin.
if (!movie.Deleted)
throw new Exception("This movie is not in the Recycle Bin.");

DeleteMovieInDatabase(id);

using var s3 = _accountSettingsProvider.CreateAmazonS3Client();
var bucket = _accountSettingsProvider.Current.Bucket;
Expand Down Expand Up @@ -960,7 +965,7 @@ await s3.GetObjectAsync(settings.Bucket, "library.zip", cancel).ConfigureAwait(f
cancel.ThrowIfCancellationRequested();
foreach (var movieId in localMovies.Keys.Except(remoteMovies.Keys))
DeleteMovie(movieId);
DeleteMovieInDatabase(movieId);
cancel.ThrowIfCancellationRequested();
// Populate the movie_files table.
Expand Down

0 comments on commit 7b44c6a

Please sign in to comment.