fixes some console coloring and adds remove command
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good

This commit is contained in:
Philipp Häfelfinger 2023-09-10 22:07:49 +02:00
parent fd74c06659
commit 551ef82c01
4 changed files with 66 additions and 7 deletions

View File

@ -26,7 +26,8 @@ internal class PiwigoAddCommand : CancellableAsyncCommand<PiwigoAddCommand.Piwig
var count = await _persistenceContext.PiwigoServers.Where(s => s.Name == settings.ServerName).CountAsync(cancellation); var count = await _persistenceContext.PiwigoServers.Where(s => s.Name == settings.ServerName).CountAsync(cancellation);
if (count > 0) 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; return -1;
} }
@ -42,7 +43,7 @@ internal class PiwigoAddCommand : CancellableAsyncCommand<PiwigoAddCommand.Piwig
_persistenceContext.PiwigoServers.Add(server); _persistenceContext.PiwigoServers.Add(server);
await _persistenceContext.SaveChangesAsync(cancellation); await _persistenceContext.SaveChangesAsync(cancellation);
AnsiConsole.WriteLine($"[green]Added server {settings.ServerName} with id {server.Id}[/]"); AnsiConsole.MarkupLine($"[green]Added server {settings.ServerName} with id {server.Id}[/]");
var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation); var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation);
PiwigoListCommand.PrintServers(piwigoServers); PiwigoListCommand.PrintServers(piwigoServers);

View File

@ -0,0 +1,56 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PiwigoDirectorySync.Infrastructure;
using PiwigoDirectorySync.Persistence;
using Spectre.Console;
using Spectre.Console.Cli;
namespace PiwigoDirectorySync.Commands;
internal class PiwigoRemoveCommand : CancellableAsyncCommand<PiwigoRemoveCommand.PiwigoRemoveSettings>
{
private readonly ILogger<PiwigoRemoveCommand> _logger;
private readonly PersistenceContext _persistenceContext;
public PiwigoRemoveCommand(ILogger<PiwigoRemoveCommand> logger, PersistenceContext persistenceContext)
{
_logger = logger;
_persistenceContext = persistenceContext;
}
protected override async Task<int> 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
{
}
}

View File

@ -21,13 +21,14 @@ internal class PiwigoUpdateCommand : CancellableAsyncCommand<PiwigoUpdateCommand
protected override async Task<int> ExecuteAsync(CommandContext context, PiwigoUpdateSettings settings, CancellationToken cancellation) protected override async Task<int> 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); settings.Url);
var count = await _persistenceContext.PiwigoServers.Where(s => s.Name == settings.ServerName && s.Id != settings.PiwigoServerId).CountAsync(cancellation); var count = await _persistenceContext.PiwigoServers.Where(s => s.Name == settings.ServerName && s.Id != settings.PiwigoServerId).CountAsync(cancellation);
if (count > 0) 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[/]"); $"[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; return -1;
} }
@ -35,7 +36,8 @@ internal class PiwigoUpdateCommand : CancellableAsyncCommand<PiwigoUpdateCommand
var server = await _persistenceContext.PiwigoServers.FindByIdAsync(settings.PiwigoServerId, cancellation); var server = await _persistenceContext.PiwigoServers.FindByIdAsync(settings.PiwigoServerId, cancellation);
if (server is null) if (server is null)
{ {
AnsiConsole.WriteLine($"[maroon]Could not find server with id {settings.PiwigoServerId}[/]"); _logger.LogWarning("Could not find server with id {SettingsPiwigoServerId}", settings.PiwigoServerId);
AnsiConsole.MarkupLine($"[maroon]Could not find server with id {settings.PiwigoServerId}[/]");
return -2; return -2;
} }
@ -47,7 +49,7 @@ internal class PiwigoUpdateCommand : CancellableAsyncCommand<PiwigoUpdateCommand
await _persistenceContext.SaveChangesAsync(cancellation); await _persistenceContext.SaveChangesAsync(cancellation);
AnsiConsole.WriteLine($"[green]Added server {settings.ServerName} with id {server.Id}[/]"); AnsiConsole.MarkupLine($"[green]Added server {settings.ServerName} with id {server.Id}[/]");
var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation); var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation);
PiwigoListCommand.PrintServers(piwigoServers); PiwigoListCommand.PrintServers(piwigoServers);

View File

@ -57,7 +57,7 @@ app.Configure(config =>
c.AddCommand<PiwigoListCommand>("list"); c.AddCommand<PiwigoListCommand>("list");
c.AddCommand<PiwigoAddCommand>("add"); c.AddCommand<PiwigoAddCommand>("add");
c.AddCommand<PiwigoUpdateCommand>("update"); c.AddCommand<PiwigoUpdateCommand>("update");
// c.AddCommand<PiwigoRemoveCommand>("remove"); c.AddCommand<PiwigoRemoveCommand>("remove");
}); });
}); });