simplify ReadDocumentQuery
This commit is contained in:
parent
f42218802d
commit
415fe646b2
@ -1,18 +0,0 @@
|
||||
using AutoMapper;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries.Read;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class ReadDocumentMappingProfile : Profile
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public ReadDocumentMappingProfile()
|
||||
{
|
||||
CreateMap<EnvelopeDocument, ReadDocumentResponse>();
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
using MediatR;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries.Read;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a query to read a document based on its unique identifier or associated envelope identifier.
|
||||
/// </summary>
|
||||
/// <param name="Id">The unique identifier of the document. Optional.</param>
|
||||
/// <param name="EnvelopeId">The identifier of the envelope associated with the document. Optional.</param>
|
||||
public record ReadDocumentQuery(int? Id = null, int? EnvelopeId = null) : IRequest<ReadDocumentResponse?>
|
||||
{
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries.Read;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the response for reading a document.
|
||||
/// </summary>
|
||||
public class ReadDocumentResponse : ReadDocumentResponseBase
|
||||
{
|
||||
/// <summary>
|
||||
/// The binary data of the document, if available.
|
||||
/// </summary>
|
||||
public byte[]? ByteData { get; init; }
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries.Read;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the response for reading a document.
|
||||
/// </summary>
|
||||
public class ReadDocumentResponseBase
|
||||
{
|
||||
/// <summary>
|
||||
/// The unique identifier of the document.
|
||||
/// </summary>
|
||||
public int Guid { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The identifier of the associated envelope.
|
||||
/// </summary>
|
||||
public int EnvelopeId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The date and time when the document was added.
|
||||
/// </summary>
|
||||
public DateTime AddedWhen { get; init; }
|
||||
}
|
||||
@ -1,13 +1,23 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using EnvelopeGenerator.Application.Dto;
|
||||
using MediatR;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries.Read;
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a query to read a document based on its unique identifier or associated envelope identifier.
|
||||
/// </summary>
|
||||
/// <param name="Id">The unique identifier of the document. Optional.</param>
|
||||
/// <param name="EnvelopeId">The identifier of the envelope associated with the document. Optional.</param>
|
||||
public record ReadDocumentQuery(int? Id = null, int? EnvelopeId = null) : IRequest<EnvelopeDocumentDto?>
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles queries for reading <see cref="EnvelopeDocument"/> data based on either the document ID or the envelope ID.
|
||||
/// </summary>
|
||||
public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, ReadDocumentResponse?>
|
||||
public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, EnvelopeDocumentDto?>
|
||||
{
|
||||
/// <summary>
|
||||
/// Repository for accessing <see cref="EnvelopeDocument"/> entities.
|
||||
@ -24,23 +34,23 @@ public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, ReadD
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the <see cref="ReadDocumentQuery"/> and returns a <see cref="ReadDocumentResponse"/> based on the provided identifiers.
|
||||
/// Handles the <see cref="ReadDocumentQuery"/> and returns a <see cref="EnvelopeDocumentDto"/> based on the provided identifiers.
|
||||
/// </summary>
|
||||
/// <param name="query">The query containing the document ID or envelope ID to search for.</param>
|
||||
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="ReadDocumentResponse"/> if a matching document is found; otherwise, <c>null</c>.
|
||||
/// A <see cref="EnvelopeDocumentDto"/> if a matching document is found; otherwise, <c>null</c>.
|
||||
/// </returns>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when neither <see cref="ReadDocumentQuery.Id"/> nor <see cref="ReadDocumentQuery.EnvelopeId"/> is provided.
|
||||
/// </exception>
|
||||
[Obsolete("Use MediatR")]
|
||||
public async Task<ReadDocumentResponse?> Handle(ReadDocumentQuery query, CancellationToken cancellationToken)
|
||||
public async Task<EnvelopeDocumentDto?> Handle(ReadDocumentQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
if (query.Id is not null)
|
||||
return await _repo.ReadOrDefaultAsync<ReadDocumentResponse>(d => d.Id == query.Id);
|
||||
return await _repo.ReadOrDefaultAsync<EnvelopeDocumentDto>(d => d.Id == query.Id);
|
||||
else if (query.EnvelopeId is not null)
|
||||
return await _repo.ReadOrDefaultAsync<ReadDocumentResponse>(d => d.EnvelopeId == query.EnvelopeId);
|
||||
return await _repo.ReadOrDefaultAsync<EnvelopeDocumentDto>(d => d.EnvelopeId == query.EnvelopeId);
|
||||
|
||||
throw new InvalidOperationException(
|
||||
$"Invalid {nameof(ReadDocumentQuery)}: either {nameof(query.Id)} or {nameof(query.EnvelopeId)} must be provided.");
|
||||
@ -1,6 +1,6 @@
|
||||
using CommandDotNet;
|
||||
using EnvelopeGenerator.Application.Contracts.Services;
|
||||
using EnvelopeGenerator.Application.Documents.Queries.Read;
|
||||
using EnvelopeGenerator.Application.Documents.Queries;
|
||||
using MediatR;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
@ -45,12 +45,12 @@ public class CommandManager
|
||||
{
|
||||
ReadDocumentQuery query = new(id, envelopeId);
|
||||
var document = await _mediator.Send(query);
|
||||
console.WriteLine(JsonSerializer.Serialize(save ? document as ReadDocumentResponseBase : document, Options));
|
||||
console.WriteLine(JsonSerializer.Serialize(save ? document as ReadDocumentResponse : document, Options));
|
||||
|
||||
if (save)
|
||||
{
|
||||
dir ??= AppContext.BaseDirectory;
|
||||
fileName ??= $"D{document?.Guid}E{document?.EnvelopeId}.pdf";
|
||||
fileName ??= $"D{document?.Id}E{document?.EnvelopeId}.pdf";
|
||||
|
||||
var path = Path.Combine(dir, fileName);
|
||||
console.WriteLine("Save to " + path);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user