From fd74c06659ce91036cf918150737318a0a335497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sun, 10 Sep 2023 21:43:22 +0200 Subject: [PATCH] adds update command and fixes add command settings --- PiwigoDirectorySync.sln.DotSettings | 1 + .../Commands/PiwigoAddCommand.cs | 7 +- .../Commands/PiwigoUpdateCommand.cs | 108 ++++++++++++++++++ .../Persistence/ExtensionMethods.cs | 4 + PiwigoDirectorySync/Program.cs | 2 +- 5 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs diff --git a/PiwigoDirectorySync.sln.DotSettings b/PiwigoDirectorySync.sln.DotSettings index f2694d2..c71516f 100644 --- a/PiwigoDirectorySync.sln.DotSettings +++ b/PiwigoDirectorySync.sln.DotSettings @@ -65,6 +65,7 @@ ExpressionBody False False + 50 CHOP_IF_LONG 180 False diff --git a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs index 34d29f8..1ab6e35 100644 --- a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs @@ -54,11 +54,10 @@ internal class PiwigoAddCommand : CancellableAsyncCommand")] public string ServerName { get; init; } = string.Empty; [CommandArgument(1, "")] public string UserName { get; init; } = string.Empty; - [CommandArgument(1, "")] public string Password { get; init; } = string.Empty; - [CommandArgument(1, "")] public string Url { get; init; } = string.Empty; + [CommandArgument(2, "")] public string Password { get; init; } = string.Empty; + [CommandArgument(3, "")] public string Url { get; init; } = string.Empty; - [CommandArgument(1, "")] - public string RootDirectory { get; init; } = string.Empty; + [CommandArgument(4, "")] public string RootDirectory { get; init; } = string.Empty; public override ValidationResult Validate() { diff --git a/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs b/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs new file mode 100644 index 0000000..77413f9 --- /dev/null +++ b/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs @@ -0,0 +1,108 @@ +using System.Diagnostics.CodeAnalysis; +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 PiwigoUpdateCommand : CancellableAsyncCommand +{ + private readonly ILogger _logger; + private readonly PersistenceContext _persistenceContext; + + public PiwigoUpdateCommand(ILogger logger, PersistenceContext persistenceContext) + { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _persistenceContext = persistenceContext ?? throw new ArgumentNullException(nameof(persistenceContext)); + } + + protected override async Task ExecuteAsync(CommandContext context, PiwigoUpdateSettings settings, CancellationToken cancellation) + { + _logger.LogInformation("Adding 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( + $"[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; + } + + var server = await _persistenceContext.PiwigoServers.FindByIdAsync(settings.PiwigoServerId, cancellation); + if (server is null) + { + AnsiConsole.WriteLine($"[maroon]Could not find server with id {settings.PiwigoServerId}[/]"); + return -2; + } + + server.Name = settings.ServerName ?? server.Name; + server.Username = settings.ServerName ?? server.Username; + server.Password = settings.Password ?? server.Password; + server.Url = settings.Url ?? server.Url; + server.RootDirectory = settings.RootDirectory ?? server.RootDirectory; + + await _persistenceContext.SaveChangesAsync(cancellation); + + AnsiConsole.WriteLine($"[green]Added server {settings.ServerName} with id {server.Id}[/]"); + + var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation); + PiwigoListCommand.PrintServers(piwigoServers); + + return 0; + } + + [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")] + internal class PiwigoUpdateSettings : CommonCommandSettings + { + [CommandArgument(0, "[ServerName]")] public string? ServerName { get; init; } + [CommandArgument(1, "[UserName]")] public string? UserName { get; init; } + [CommandArgument(2, "[Password]")] public string? Password { get; init; } + [CommandArgument(3, "[Password]")] public string? Url { get; init; } + [CommandArgument(4, "[RootDirectory]")] public string? RootDirectory { get; init; } + + public override ValidationResult Validate() + { + var baseResult = base.Validate(); + if (!baseResult.Successful) + { + return baseResult; + } + + if (string.IsNullOrWhiteSpace(ServerName)) + { + return ValidationResult.Error("Please specify a server name"); + } + + if (string.IsNullOrWhiteSpace(Url)) + { + return ValidationResult.Error("Please specify a server url"); + } + + if (string.IsNullOrWhiteSpace(UserName)) + { + return ValidationResult.Error("Please specify a valid username for the server"); + } + + if (string.IsNullOrWhiteSpace(Password)) + { + return ValidationResult.Error("Please specify a password"); + } + + if (string.IsNullOrWhiteSpace(RootDirectory)) + { + return ValidationResult.Error("Please specify a root directory"); + } + + if (!Directory.Exists(RootDirectory)) + { + return ValidationResult.Error($"The root directory {RootDirectory} does not exist or is not accessible"); + } + + return ValidationResult.Success(); + } + } +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Persistence/ExtensionMethods.cs b/PiwigoDirectorySync/Persistence/ExtensionMethods.cs index 8570870..d94122e 100644 --- a/PiwigoDirectorySync/Persistence/ExtensionMethods.cs +++ b/PiwigoDirectorySync/Persistence/ExtensionMethods.cs @@ -4,6 +4,10 @@ namespace PiwigoDirectorySync.Persistence; public static class ExtensionMethods { + public static Task FindByIdAsync(this DbSet dbSet, int serverId, CancellationToken ct) + { + return dbSet.Where(a => a.Id == serverId).FirstOrDefaultAsync(ct); + } public static Task GetByIdAsync(this DbSet dbSet, int serverId, CancellationToken ct) { return dbSet.Where(a => a.Id == serverId).FirstAsync(ct); diff --git a/PiwigoDirectorySync/Program.cs b/PiwigoDirectorySync/Program.cs index b8f8b2a..e425eb1 100644 --- a/PiwigoDirectorySync/Program.cs +++ b/PiwigoDirectorySync/Program.cs @@ -56,7 +56,7 @@ app.Configure(config => { c.AddCommand("list"); c.AddCommand("add"); - // c.AddCommand("update"); + c.AddCommand("update"); // c.AddCommand("remove"); }); });