using CommandDotNet; using EnvelopeGenerator.Application.Documents.Queries; using MediatR; using System.Reflection; using System.Text.Json; using EnvelopeGenerator.Application.Common.Interfaces.Services; namespace EnvelopeGenerator.Terminal; public class CommandManager { private static JsonSerializerOptions Options = new () { WriteIndented = true }; [Obsolete("Use MediatR")] private readonly IEnvelopeReceiverService _envelopeReceiverService; private readonly IMediator _mediator; [Obsolete("Use MediatR")] 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}"); } [Obsolete("Use MediatR")] [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(document, Options)); if (save) { dir ??= AppContext.BaseDirectory; fileName ??= $"D{document?.Id}E{document?.EnvelopeId}.pdf"; var path = Path.Combine(dir, fileName); console.WriteLine("Save to " + path); File.WriteAllBytes(path, document?.ByteData ?? Array.Empty()); } } }