Developer 02 2966d64455 feat(terminal): ReadDocument-Befehl um PDF-Speicheroptionen erweitert
Optionen `save`, `dir` und `fileName` zum `ReadDocument`-Befehl hinzugefügt.
Wenn `save` aktiviert ist, wird das PDF an dem angegebenen oder dem Standardpfad gespeichert.
Ermöglicht dem Benutzer mehr Kontrolle über Speicherort und Dateinamen.
2025-04-25 19:33:29 +02:00

59 lines
2.1 KiB
C#

using CommandDotNet;
using EnvelopeGenerator.Application.Contracts.Services;
using EnvelopeGenerator.Application.Documents.Queries.Read;
using MediatR;
using System.Reflection;
using System.Text.Json;
namespace EnvelopeGenerator.Terminal;
public class CommandManager
{
private static JsonSerializerOptions Options = new ()
{
WriteIndented = true
};
private readonly IEnvelopeReceiverService _envelopeReceiverService;
private readonly IMediator _mediator;
public CommandManager(IEnvelopeReceiverService envelopeReceiverService, IMediator mediator)
{
_envelopeReceiverService = envelopeReceiverService;
_mediator = mediator;
}
[DefaultCommand]
public void Execute([Option(Description = "print envelope generator termianal version.")] bool version)
{
if(version)
Console.WriteLine($"v{Assembly.GetExecutingAssembly().GetName().Version}");
}
[Subcommand]
public IEnvelopeReceiverService EnvelopeReceiver => _envelopeReceiverService;
[Command(ArgumentSeparatorStrategy = ArgumentSeparatorStrategy.EndOfOptions)]
public async Task ReadDocument(IConsole console,
[Option(Description = "ID of the document.")] int? id = null,
[Option(Description = "ID of the envelope containing the document.")] int? envelopeId = null,
[Option(Description = "Path to save the PDF")] bool save = false,
[Option(Description = "Directory to save the PDF")] string? dir = null,
[Option(Description = "Name of file to save the PDF")] string? fileName = null)
{
ReadDocumentQuery query = new(id, envelopeId);
var document = await _mediator.Send(query);
console.WriteLine(JsonSerializer.Serialize(save ? document as ReadDocumentResponseBase : document, Options));
if (save)
{
dir ??= AppContext.BaseDirectory;
fileName ??= $"D{document?.Guid}E{document?.EnvelopeId}.pdf";
var path = Path.Combine(dir, fileName);
console.WriteLine("Save to " + path);
File.WriteAllBytes(path, document?.ByteData ?? Array.Empty<byte>());
}
}
}