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 DigitalData.Core.Abstraction.Application.Repository;
|
||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Application.Dto;
|
||||||
using MediatR;
|
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>
|
/// <summary>
|
||||||
/// Handles queries for reading <see cref="EnvelopeDocument"/> data based on either the document ID or the envelope ID.
|
/// Handles queries for reading <see cref="EnvelopeDocument"/> data based on either the document ID or the envelope ID.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, ReadDocumentResponse?>
|
public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, EnvelopeDocumentDto?>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Repository for accessing <see cref="EnvelopeDocument"/> entities.
|
/// Repository for accessing <see cref="EnvelopeDocument"/> entities.
|
||||||
@ -24,23 +34,23 @@ public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, ReadD
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="query">The query containing the document ID or envelope ID to search for.</param>
|
/// <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>
|
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
|
||||||
/// <returns>
|
/// <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>
|
/// </returns>
|
||||||
/// <exception cref="InvalidOperationException">
|
/// <exception cref="InvalidOperationException">
|
||||||
/// Thrown when neither <see cref="ReadDocumentQuery.Id"/> nor <see cref="ReadDocumentQuery.EnvelopeId"/> is provided.
|
/// Thrown when neither <see cref="ReadDocumentQuery.Id"/> nor <see cref="ReadDocumentQuery.EnvelopeId"/> is provided.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
[Obsolete("Use MediatR")]
|
[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)
|
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)
|
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(
|
throw new InvalidOperationException(
|
||||||
$"Invalid {nameof(ReadDocumentQuery)}: either {nameof(query.Id)} or {nameof(query.EnvelopeId)} must be provided.");
|
$"Invalid {nameof(ReadDocumentQuery)}: either {nameof(query.Id)} or {nameof(query.EnvelopeId)} must be provided.");
|
||||||
@ -1,6 +1,6 @@
|
|||||||
using CommandDotNet;
|
using CommandDotNet;
|
||||||
using EnvelopeGenerator.Application.Contracts.Services;
|
using EnvelopeGenerator.Application.Contracts.Services;
|
||||||
using EnvelopeGenerator.Application.Documents.Queries.Read;
|
using EnvelopeGenerator.Application.Documents.Queries;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
@ -45,12 +45,12 @@ public class CommandManager
|
|||||||
{
|
{
|
||||||
ReadDocumentQuery query = new(id, envelopeId);
|
ReadDocumentQuery query = new(id, envelopeId);
|
||||||
var document = await _mediator.Send(query);
|
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)
|
if (save)
|
||||||
{
|
{
|
||||||
dir ??= AppContext.BaseDirectory;
|
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);
|
var path = Path.Combine(dir, fileName);
|
||||||
console.WriteLine("Save to " + path);
|
console.WriteLine("Save to " + path);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user