From bdbff917cab5553467edd549a4b57c91ba6eb492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sun, 10 Sep 2023 21:26:51 +0200 Subject: [PATCH] check for existing server name --- .../Commands/PiwigoAddCommand.cs | 12 ++++- .../Commands/PiwigoAddSettings.cs | 44 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs index ad91f5d..e969b02 100644 --- a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; +using Spectre.Console; using Spectre.Console.Cli; namespace PiwigoDirectorySync.Commands; @@ -21,7 +22,14 @@ internal class PiwigoAddCommand : CancellableAsyncCommand { _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).CountAsync(cancellation); + if (count > 0) + { + AnsiConsole.WriteLine($"[maroon]There is already a server with the name {settings.ServerName}[/]"); + return -1; + } + var server = new ServerEntity { Url = settings.Url, @@ -34,6 +42,8 @@ internal class PiwigoAddCommand : CancellableAsyncCommand _persistenceContext.PiwigoServers.Add(server); 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); diff --git a/PiwigoDirectorySync/Commands/PiwigoAddSettings.cs b/PiwigoDirectorySync/Commands/PiwigoAddSettings.cs index 6d78ed5..d56227c 100644 --- a/PiwigoDirectorySync/Commands/PiwigoAddSettings.cs +++ b/PiwigoDirectorySync/Commands/PiwigoAddSettings.cs @@ -1,4 +1,5 @@ -using Spectre.Console.Cli; +using Spectre.Console; +using Spectre.Console.Cli; namespace PiwigoDirectorySync.Commands; @@ -11,4 +12,45 @@ internal class PiwigoAddSettings : CommandSettings [CommandArgument(1, "")] public string RootDirectory { get; init; } = string.Empty; + + 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