- Updated DocumentController to use class-level [Authorize] and method-level role-based authorization for sender and receiver endpoints. - Replaced ReadEnvelopeReceiverQuery with ReadDocumentQuery for sender document retrieval; simplified response logic. - Added a new endpoint for fully authenticated receivers to fetch documents by envelope ID from user claims. - Refactored ReadDocumentQuery and handler to always return DocumentDto, throw NotFoundException when needed, and use _repo.Query. - Cleaned up using directives and removed legacy error handling and logging.
69 lines
2.8 KiB
C#
69 lines
2.8 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using MediatR;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AutoMapper;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
using DigitalData.Core.Exceptions;
|
|
|
|
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<DocumentDto>
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles queries for reading <see cref="Document"/> data based on either the document ID or the envelope ID.
|
|
/// </summary>
|
|
public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, DocumentDto>
|
|
{
|
|
/// <summary>
|
|
/// TempRepo for accessing <see cref="Document"/> entities.
|
|
/// </summary>
|
|
private readonly IRepository<Document> _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="Document"/> entities.</param>
|
|
/// <param name="mapper"></param>
|
|
public ReadDocumentQueryHandler(IRepository<Document> envelopeDocumentRepository, IMapper mapper)
|
|
{
|
|
_repo = envelopeDocumentRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the <see cref="ReadDocumentQuery"/> and returns a <see cref="DocumentDto"/> 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="DocumentDto"/> 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<DocumentDto> Handle(ReadDocumentQuery query, CancellationToken cancel)
|
|
{
|
|
if (query.Id is not null)
|
|
{
|
|
var doc = await _repo.Query.Where(d => d.Id == query.Id).FirstOrDefaultAsync(cancel);
|
|
return _mapper.Map<DocumentDto>(doc);
|
|
}
|
|
else if (query.EnvelopeId is not null)
|
|
{
|
|
var doc = await _repo.Query.Where(d => d.EnvelopeId == query.EnvelopeId).FirstOrDefaultAsync(cancel);
|
|
return _mapper.Map<DocumentDto>(doc);
|
|
}
|
|
|
|
throw new NotFoundException();
|
|
}
|
|
} |