check for existing server name
This commit is contained in:
parent
6e714ef77a
commit
bdbff917ca
@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PiwigoDirectorySync.Infrastructure;
|
||||
using PiwigoDirectorySync.Persistence;
|
||||
using Spectre.Console;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
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,
|
||||
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<PiwigoAddSettings>
|
||||
_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);
|
||||
|
||||
|
@ -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, "<RootDirectory>")]
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user