using DigitalData.Core.Application.Interfaces.Repository; using EnvelopeGenerator.Domain.Entities; using MediatR; namespace EnvelopeGenerator.Application.Documents.Queries.Read; /// /// Handles queries for reading data based on either the document ID or the envelope ID. /// public class ReadDocumentQueryHandler : IRequestHandler { /// /// Repository for accessing entities. /// private readonly IRepository _repo; /// /// Initializes a new instance of the class. /// /// The repository used to access entities. public ReadDocumentQueryHandler(IRepository envelopeDocumentRepository) { _repo = envelopeDocumentRepository; } /// /// Handles the and returns a based on the provided identifiers. /// /// The query containing the document ID or envelope ID to search for. /// A token to monitor for cancellation requests. /// /// A if a matching document is found; otherwise, null. /// /// /// Thrown when neither nor is provided. /// public async Task Handle(ReadDocumentQuery query, CancellationToken cancellationToken) { if (query.Id is not null) return await _repo.ReadOrDefaultAsync(d => d.Id == query.Id); else if (query.EnvelopeId is not null) return await _repo.ReadOrDefaultAsync(d => d.EnvelopeId == query.EnvelopeId); throw new InvalidOperationException( $"Invalid {nameof(ReadDocumentQuery)}: either {nameof(query.Id)} or {nameof(query.EnvelopeId)} must be provided."); } }