48 lines
2.2 KiB
C#
48 lines
2.2 KiB
C#
using DigitalData.Core.Abstractions.Infrastructure;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
|
|
namespace EnvelopeGenerator.Application.Documents.Queries.Read;
|
|
|
|
/// <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?>
|
|
{
|
|
/// <summary>
|
|
/// Repository for accessing <see cref="EnvelopeDocument"/> entities.
|
|
/// </summary>
|
|
private readonly IRepository<EnvelopeDocument> _repo;
|
|
|
|
/// <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>
|
|
public ReadDocumentQueryHandler(IRepository<EnvelopeDocument> envelopeDocumentRepository)
|
|
{
|
|
_repo = envelopeDocumentRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the <see cref="ReadDocumentQuery"/> and returns a <see cref="ReadDocumentResponse"/> 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>.
|
|
/// </returns>
|
|
/// <exception cref="InvalidOperationException">
|
|
/// Thrown when neither <see cref="ReadDocumentQuery.Id"/> nor <see cref="ReadDocumentQuery.EnvelopeId"/> is provided.
|
|
/// </exception>
|
|
public async Task<ReadDocumentResponse?> Handle(ReadDocumentQuery query, CancellationToken cancellationToken)
|
|
{
|
|
if (query.Id is not null)
|
|
return await _repo.ReadOrDefaultAsync<ReadDocumentResponse>(d => d.Id == query.Id);
|
|
else if (query.EnvelopeId is not null)
|
|
return await _repo.ReadOrDefaultAsync<ReadDocumentResponse>(d => d.EnvelopeId == query.EnvelopeId);
|
|
|
|
throw new InvalidOperationException(
|
|
$"Invalid {nameof(ReadDocumentQuery)}: either {nameof(query.Id)} or {nameof(query.EnvelopeId)} must be provided.");
|
|
}
|
|
}
|