adds pending changes command
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good

This commit is contained in:
Philipp Häfelfinger 2023-09-12 18:51:05 +02:00
parent 3b8dad1af2
commit 58206c9347
7 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,41 @@
using PiwigoDirectorySync.Infrastructure;
using PiwigoDirectorySync.Services;
using Spectre.Console;
using Spectre.Console.Cli;
namespace PiwigoDirectorySync.Commands;
internal class SyncPendingChangesCommand : CancellableAsyncCommand<SyncPendingChangesCommand.SyncPendingChangesSettings>
{
private readonly IAlbumSynchronizer _albumSynchronizer;
private readonly IImageSynchronizer _imageSynchronizer;
public SyncPendingChangesCommand(IAlbumSynchronizer albumSynchronizer, IImageSynchronizer imageSynchronizer)
{
_albumSynchronizer = albumSynchronizer;
_imageSynchronizer = imageSynchronizer;
}
protected override async Task<int> 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
{
}
}

View File

@ -60,6 +60,7 @@ app.Configure(config =>
c.AddCommand<SyncFullCommand>("full");
c.AddCommand<SyncAlbumsCommand>("albums");
c.AddCommand<SyncImagesCommand>("images");
c.AddCommand<SyncPendingChangesCommand>("pending");
});
config.AddBranch("piwigo", c =>
{

View File

@ -42,6 +42,15 @@ internal class AlbumSynchronizer : IAlbumSynchronizer
}
}
public async Task<PendingChange> 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)

View File

@ -3,4 +3,5 @@ namespace PiwigoDirectorySync.Services;
internal interface IAlbumSynchronizer
{
Task SynchronizeAlbums(int piwigoServerId, bool readOnly, CancellationToken ct);
Task<PendingChange> GetPendingChangesAsync(int piwigoServerId, CancellationToken ct);
}

View File

@ -4,4 +4,5 @@ internal interface IImageSynchronizer
{
Task SynchronizeImagesAsync(int piwigoServerId, bool readOnly, CancellationToken ct);
Task DownloadImagesAsync(int piwigoServerId, CancellationToken ct);
Task<PendingChange> GetPendingChangesAsync(int piwigoServerId, CancellationToken ct);
}

View File

@ -21,6 +21,15 @@ internal class ImageSynchronizer : IImageSynchronizer
_persistenceContext = persistenceContext;
}
public async Task<PendingChange> 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);

View File

@ -0,0 +1,3 @@
namespace PiwigoDirectorySync.Services;
internal record PendingChange(int CheckOrAdds, int Updates, int Deletes);