diff --git a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs index 1ab6e35..548d0e9 100644 --- a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs @@ -26,7 +26,8 @@ internal class PiwigoAddCommand : CancellableAsyncCommand s.Name == settings.ServerName).CountAsync(cancellation); if (count > 0) { - AnsiConsole.WriteLine($"[maroon]There is already a server with the name {settings.ServerName}[/]"); + _logger.LogWarning("Server with name {SettingsServerName} already exists. Not updated", settings.ServerName); + AnsiConsole.MarkupLine($"[maroon]There is already a server with the name {settings.ServerName}[/]"); return -1; } @@ -42,7 +43,7 @@ internal class PiwigoAddCommand : CancellableAsyncCommand +{ + private readonly ILogger _logger; + private readonly PersistenceContext _persistenceContext; + + public PiwigoRemoveCommand(ILogger logger, PersistenceContext persistenceContext) + { + _logger = logger; + _persistenceContext = persistenceContext; + } + + protected override async Task ExecuteAsync(CommandContext context, PiwigoRemoveSettings settings, CancellationToken cancellation) + { + var server = await _persistenceContext.PiwigoServers.FindByIdAsync(settings.PiwigoServerId, cancellation); + if (server is null) + { + _logger.LogWarning("Could not find server with id {SettingsPiwigoServerId}", settings.PiwigoServerId); + AnsiConsole.MarkupLine($"[maroon]Could not find server with id {settings.PiwigoServerId}[/]"); + return -1; + } + + if (!AnsiConsole.Confirm($"[orange3]Do you really want to remove {server.Name} ({server.Id}) from this database?[/]", false)) + { + _logger.LogInformation("Do not remove server {ServerName}", server.Name); + AnsiConsole.MarkupLine("[green]Remove cancelled![/]"); + return 1; + } + + AnsiConsole.MarkupLine($"[orange3]Removing image metadata of server {server.Name}![/]"); + await _persistenceContext.PiwigoImages.Where(i => i.Album.ServerId == server.Id).ExecuteDeleteAsync(cancellation); + await _persistenceContext.SaveChangesAsync(cancellation); + + AnsiConsole.MarkupLine($"[orange3]Removing album metadata of server {server.Name}![/]"); + await _persistenceContext.PiwigoAlbums.Where(a => a.ServerId == server.Id).ExecuteDeleteAsync(cancellation); + await _persistenceContext.SaveChangesAsync(cancellation); + + AnsiConsole.MarkupLine($"[orange3]Removing server {server.Name}![/]"); + await _persistenceContext.PiwigoServers.Where(s => s.Id == server.Id).ExecuteDeleteAsync(cancellation); + await _persistenceContext.SaveChangesAsync(cancellation); + + return 0; + } + + internal class PiwigoRemoveSettings : CommonCommandSettings + { + } +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs b/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs index 77413f9..c470f72 100644 --- a/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs @@ -21,13 +21,14 @@ internal class PiwigoUpdateCommand : CancellableAsyncCommand ExecuteAsync(CommandContext context, PiwigoUpdateSettings settings, CancellationToken cancellation) { - _logger.LogInformation("Adding piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, + _logger.LogInformation("Updating piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, settings.Url); var count = await _persistenceContext.PiwigoServers.Where(s => s.Name == settings.ServerName && s.Id != settings.PiwigoServerId).CountAsync(cancellation); if (count > 0) { - AnsiConsole.WriteLine( + _logger.LogWarning("Server with name {SettingsServerName} already exists. Not updated", settings.ServerName); + AnsiConsole.MarkupLine( $"[maroon]You can not rename the server with id {settings.PiwigoServerId} to {settings.ServerName} as there is already a server with this name[/]"); return -1; } @@ -35,7 +36,8 @@ internal class PiwigoUpdateCommand : CancellableAsyncCommand c.AddCommand("list"); c.AddCommand("add"); c.AddCommand("update"); - // c.AddCommand("remove"); + c.AddCommand("remove"); }); });