From 58206c93470f6437577d322a291cce6dda0acb15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Tue, 12 Sep 2023 18:51:05 +0200 Subject: [PATCH] adds pending changes command --- .../Commands/SyncPendingChangesCommand.cs | 41 +++++++++++++++++++ PiwigoDirectorySync/Program.cs | 1 + .../Services/AlbumSynchronizer.cs | 9 ++++ .../Services/IAlbumSynchronizer.cs | 1 + .../Services/IImageSynchronizer.cs | 1 + .../Services/ImageSynchronizer.cs | 9 ++++ PiwigoDirectorySync/Services/PendingChange.cs | 3 ++ 7 files changed, 65 insertions(+) create mode 100644 PiwigoDirectorySync/Commands/SyncPendingChangesCommand.cs create mode 100644 PiwigoDirectorySync/Services/PendingChange.cs diff --git a/PiwigoDirectorySync/Commands/SyncPendingChangesCommand.cs b/PiwigoDirectorySync/Commands/SyncPendingChangesCommand.cs new file mode 100644 index 0000000..c38d102 --- /dev/null +++ b/PiwigoDirectorySync/Commands/SyncPendingChangesCommand.cs @@ -0,0 +1,41 @@ +using PiwigoDirectorySync.Infrastructure; +using PiwigoDirectorySync.Services; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace PiwigoDirectorySync.Commands; + +internal class SyncPendingChangesCommand : CancellableAsyncCommand +{ + private readonly IAlbumSynchronizer _albumSynchronizer; + private readonly IImageSynchronizer _imageSynchronizer; + + public SyncPendingChangesCommand(IAlbumSynchronizer albumSynchronizer, IImageSynchronizer imageSynchronizer) + { + _albumSynchronizer = albumSynchronizer; + _imageSynchronizer = imageSynchronizer; + } + + protected override async Task ExecuteAsync(CommandContext context, SyncPendingChangesSettings settings, CancellationToken cancellation) + { + var pendingAlbumChanges = await _albumSynchronizer.GetPendingChangesAsync(settings.PiwigoServerId, cancellation); + var pendingImageChanges = await _imageSynchronizer.GetPendingChangesAsync(settings.PiwigoServerId, cancellation); + + var table = new Table(); + table.AddColumn("Entity"); + table.AddColumn(new TableColumn("Check or adds").Centered()); + table.AddColumn(new TableColumn("updates").Centered()); + table.AddColumn(new TableColumn("deletes").Centered()); + + table.AddRow("Albums", $"[orange3]{pendingAlbumChanges.CheckOrAdds}[/]", $"[orange3]{pendingAlbumChanges.Updates}[/]", $"[red]{pendingAlbumChanges.Deletes}[/]"); + table.AddRow("Images", $"[orange3]{pendingImageChanges.CheckOrAdds}[/]", $"[orange3]{pendingImageChanges.Updates}[/]", $"[red]{pendingImageChanges.Deletes}[/]"); + + AnsiConsole.Write(table); + + return 0; + } + + internal class SyncPendingChangesSettings : SyncCommandSettings + { + } +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Program.cs b/PiwigoDirectorySync/Program.cs index e73c805..bf772a2 100644 --- a/PiwigoDirectorySync/Program.cs +++ b/PiwigoDirectorySync/Program.cs @@ -60,6 +60,7 @@ app.Configure(config => c.AddCommand("full"); c.AddCommand("albums"); c.AddCommand("images"); + c.AddCommand("pending"); }); config.AddBranch("piwigo", c => { diff --git a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs index 7ec49ba..1180198 100644 --- a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs +++ b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs @@ -42,6 +42,15 @@ internal class AlbumSynchronizer : IAlbumSynchronizer } } + public async Task GetPendingChangesAsync(int piwigoServerId, CancellationToken ct) + { + var getOrAdds = await _persistenceContext.PiwigoAlbums.Where(a => a.ServerAlbumId == null).CountAsync(ct); + const int updates = 0; // not yet supported + const int deletes = 0; // not yet supported + + return new PendingChange(getOrAdds, updates, deletes); + } + private async Task AddMissingAlbumsToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) { var albumsToCreate = await _persistenceContext.PiwigoAlbums.Where(a => a.ServerAlbumId == null && a.ServerId == piwigoServer.Id) diff --git a/PiwigoDirectorySync/Services/IAlbumSynchronizer.cs b/PiwigoDirectorySync/Services/IAlbumSynchronizer.cs index e6332dc..c3cac2b 100644 --- a/PiwigoDirectorySync/Services/IAlbumSynchronizer.cs +++ b/PiwigoDirectorySync/Services/IAlbumSynchronizer.cs @@ -3,4 +3,5 @@ namespace PiwigoDirectorySync.Services; internal interface IAlbumSynchronizer { Task SynchronizeAlbums(int piwigoServerId, bool readOnly, CancellationToken ct); + Task GetPendingChangesAsync(int piwigoServerId, CancellationToken ct); } \ No newline at end of file diff --git a/PiwigoDirectorySync/Services/IImageSynchronizer.cs b/PiwigoDirectorySync/Services/IImageSynchronizer.cs index 36bd3df..b22bf75 100644 --- a/PiwigoDirectorySync/Services/IImageSynchronizer.cs +++ b/PiwigoDirectorySync/Services/IImageSynchronizer.cs @@ -4,4 +4,5 @@ internal interface IImageSynchronizer { Task SynchronizeImagesAsync(int piwigoServerId, bool readOnly, CancellationToken ct); Task DownloadImagesAsync(int piwigoServerId, CancellationToken ct); + Task GetPendingChangesAsync(int piwigoServerId, CancellationToken ct); } \ No newline at end of file diff --git a/PiwigoDirectorySync/Services/ImageSynchronizer.cs b/PiwigoDirectorySync/Services/ImageSynchronizer.cs index 1029ac8..eedb72e 100644 --- a/PiwigoDirectorySync/Services/ImageSynchronizer.cs +++ b/PiwigoDirectorySync/Services/ImageSynchronizer.cs @@ -21,6 +21,15 @@ internal class ImageSynchronizer : IImageSynchronizer _persistenceContext = persistenceContext; } + public async Task GetPendingChangesAsync(int piwigoServerId, CancellationToken ct) + { + var getOrAdds = await _persistenceContext.PiwigoImages.Where(i => i.ServerImageId == null).CountAsync(ct); + var updates = await _persistenceContext.PiwigoImages.Where(i => i.ServerImageId != null && i.UploadRequired).CountAsync(ct); + var deletes = await _persistenceContext.PiwigoImages.Where(i => i.ServerImageId != null && i.DeleteRequired).CountAsync(ct); + + return new PendingChange(getOrAdds, updates, deletes); + } + public async Task DownloadImagesAsync(int piwigoServerId, CancellationToken ct) { var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct); diff --git a/PiwigoDirectorySync/Services/PendingChange.cs b/PiwigoDirectorySync/Services/PendingChange.cs new file mode 100644 index 0000000..b682cad --- /dev/null +++ b/PiwigoDirectorySync/Services/PendingChange.cs @@ -0,0 +1,3 @@ +namespace PiwigoDirectorySync.Services; + +internal record PendingChange(int CheckOrAdds, int Updates, int Deletes); \ No newline at end of file