69 lines
3.0 KiB
C#
69 lines
3.0 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using MediatR;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AutoMapper;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
|
|
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, EnvelopeDocumentDto?>
|
|
{
|
|
/// <summary>
|
|
/// TempRepo for accessing <see cref="EnvelopeDocument"/> entities.
|
|
/// </summary>
|
|
private readonly IRepository<EnvelopeDocument> _repo;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="ReadDocumentQueryHandler"/> class.
|
|
/// </summary>
|
|
/// <param name="envelopeDocumentRepository">The repository used to access <see cref="EnvelopeDocument"/> entities.</param>
|
|
/// <param name="mapper"></param>
|
|
public ReadDocumentQueryHandler(IRepository<EnvelopeDocument> envelopeDocumentRepository, IMapper mapper)
|
|
{
|
|
_repo = envelopeDocumentRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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="cancel">A token to monitor for cancellation requests.</param>
|
|
/// <returns>
|
|
/// 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>
|
|
public async Task<EnvelopeDocumentDto?> Handle(ReadDocumentQuery query, CancellationToken cancel)
|
|
{
|
|
if (query.Id is not null)
|
|
{
|
|
var doc = await _repo.ReadOnly().Where(d => d.Id == query.Id).FirstOrDefaultAsync(cancel);
|
|
return _mapper.Map<EnvelopeDocumentDto>(doc);
|
|
}
|
|
else if (query.EnvelopeId is not null)
|
|
{
|
|
var doc = await _repo.ReadOnly().Where(d => d.EnvelopeId == query.EnvelopeId).FirstOrDefaultAsync(cancel);
|
|
return _mapper.Map<EnvelopeDocumentDto>(doc);
|
|
}
|
|
|
|
throw new InvalidOperationException(
|
|
$"Invalid {nameof(ReadDocumentQuery)}: either {nameof(query.Id)} or {nameof(query.EnvelopeId)} must be provided.");
|
|
}
|
|
} |