check for existing server name

This commit is contained in:
Philipp Häfelfinger 2023-09-10 21:26:51 +02:00
parent 6e714ef77a
commit bdbff917ca
2 changed files with 54 additions and 2 deletions

View File

@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Infrastructure;
using PiwigoDirectorySync.Persistence; using PiwigoDirectorySync.Persistence;
using Spectre.Console;
using Spectre.Console.Cli; using Spectre.Console.Cli;
namespace PiwigoDirectorySync.Commands; namespace PiwigoDirectorySync.Commands;
@ -22,6 +23,13 @@ internal class PiwigoAddCommand : CancellableAsyncCommand<PiwigoAddSettings>
_logger.LogInformation("Adding piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, _logger.LogInformation("Adding 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).CountAsync(cancellation);
if (count > 0)
{
AnsiConsole.WriteLine($"[maroon]There is already a server with the name {settings.ServerName}[/]");
return -1;
}
var server = new ServerEntity var server = new ServerEntity
{ {
Url = settings.Url, Url = settings.Url,
@ -34,6 +42,8 @@ internal class PiwigoAddCommand : CancellableAsyncCommand<PiwigoAddSettings>
_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}[/]");
var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation); var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation);
PiwigoListCommand.PrintServers(piwigoServers); PiwigoListCommand.PrintServers(piwigoServers);

View File

@ -1,4 +1,5 @@
using Spectre.Console.Cli; using Spectre.Console;
using Spectre.Console.Cli;
namespace PiwigoDirectorySync.Commands; namespace PiwigoDirectorySync.Commands;
@ -11,4 +12,45 @@ internal class PiwigoAddSettings : CommandSettings
[CommandArgument(1, "<RootDirectory>")] [CommandArgument(1, "<RootDirectory>")]
public string RootDirectory { get; init; } = string.Empty; 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();
}
} }