adds update command and fixes add command settings

This commit is contained in:
Philipp Häfelfinger 2023-09-10 21:43:22 +02:00
parent 2dd4b85e06
commit fd74c06659
5 changed files with 117 additions and 5 deletions

View File

@ -65,6 +65,7 @@
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/METHOD_OR_OPERATOR_BODY/@EntryValue">ExpressionBody</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_EXPR_MEMBER_ARRANGEMENT/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_USER_LINEBREAKS/@EntryValue">False</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/MAX_ATTRIBUTE_LENGTH_FOR_SAME_LINE/@EntryValue">50</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_METHOD_CALLS/@EntryValue">CHOP_IF_LONG</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">180</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">False</s:Boolean>

View File

@ -54,11 +54,10 @@ internal class PiwigoAddCommand : CancellableAsyncCommand<PiwigoAddCommand.Piwig
{
[CommandArgument(0, "<ServerName>")] public string ServerName { get; init; } = string.Empty;
[CommandArgument(1, "<UserName>")] public string UserName { get; init; } = string.Empty;
[CommandArgument(1, "<Password>")] public string Password { get; init; } = string.Empty;
[CommandArgument(1, "<Password>")] public string Url { get; init; } = string.Empty;
[CommandArgument(2, "<Password>")] public string Password { get; init; } = string.Empty;
[CommandArgument(3, "<Password>")] public string Url { get; init; } = string.Empty;
[CommandArgument(1, "<RootDirectory>")]
public string RootDirectory { get; init; } = string.Empty;
[CommandArgument(4, "<RootDirectory>")] public string RootDirectory { get; init; } = string.Empty;
public override ValidationResult Validate()
{

View File

@ -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<PiwigoUpdateCommand.PiwigoUpdateSettings>
{
private readonly ILogger<PiwigoUpdateCommand> _logger;
private readonly PersistenceContext _persistenceContext;
public PiwigoUpdateCommand(ILogger<PiwigoUpdateCommand> logger, PersistenceContext persistenceContext)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_persistenceContext = persistenceContext ?? throw new ArgumentNullException(nameof(persistenceContext));
}
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,
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();
}
}
}

View File

@ -4,6 +4,10 @@ namespace PiwigoDirectorySync.Persistence;
public static class ExtensionMethods
{
public static Task<ServerEntity?> FindByIdAsync(this DbSet<ServerEntity> dbSet, int serverId, CancellationToken ct)
{
return dbSet.Where(a => a.Id == serverId).FirstOrDefaultAsync(ct);
}
public static Task<ServerEntity> GetByIdAsync(this DbSet<ServerEntity> dbSet, int serverId, CancellationToken ct)
{
return dbSet.Where(a => a.Id == serverId).FirstAsync(ct);

View File

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