From 830d1af44adfcd35fb64d3fc3e5d76e0b781dfab Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 28 Jan 2026 12:54:22 +0100 Subject: [PATCH] Add ReadUserEnvelopesQuery and handler with filtering Implemented ReadUserEnvelopesQuery and its handler to fetch user envelopes with support for filtering by user ID, status (range, include, ignore), envelope ID, and UUID. Utilizes repository and AutoMapper to return EnvelopeDto results. --- .../Queries/ReadUserEnvelopesQuery.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 EnvelopeGenerator.Application/Envelopes/Queries/ReadUserEnvelopesQuery.cs diff --git a/EnvelopeGenerator.Application/Envelopes/Queries/ReadUserEnvelopesQuery.cs b/EnvelopeGenerator.Application/Envelopes/Queries/ReadUserEnvelopesQuery.cs new file mode 100644 index 00000000..e2f084b4 --- /dev/null +++ b/EnvelopeGenerator.Application/Envelopes/Queries/ReadUserEnvelopesQuery.cs @@ -0,0 +1,79 @@ +using EnvelopeGenerator.Application.Common.Dto; +using EnvelopeGenerator.Application.Common.Query; +using MediatR; +using AutoMapper; +using DigitalData.Core.Abstraction.Application.Repository; +using EnvelopeGenerator.Domain.Entities; +using Microsoft.EntityFrameworkCore; + +namespace EnvelopeGenerator.Application.Envelopes.Queries; + +/// +/// +/// +public record ReadUserEnvelopesQuery : EnvelopeQueryBase, IRequest> +{ + /// + /// + /// + public required int UserId { get; init; } + + /// + /// + /// + public EnvelopeStatusQuery? Status { get; init; } +} + +/// +/// +/// +public class ReadUserEnvelopesQueryHandler : IRequestHandler> +{ + private readonly IRepository _repository; + private readonly IMapper _mapper; + + /// + /// + /// + /// + /// + public ReadUserEnvelopesQueryHandler(IRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + /// + /// + /// + /// + /// + /// + public async Task> Handle(ReadUserEnvelopesQuery request, CancellationToken cancellationToken) + { + var query = _repository.Query + .Include(e => e.Documents) + .Where(e => e.UserId == request.UserId); + + if (request.Status is { } status) + { + if (status.Min is not null) + query = query.Where(e => e.Status >= status.Min); + if (status.Max is not null) + query = query.Where(e => e.Status <= status.Max); + if (status.Include?.Length > 0) + query = query.Where(e => status.Include.Contains(e.Status)); + if (status.Ignore?.Length > 0) + query = query.Where(e => !status.Ignore.Contains(e.Status)); + } + + if (request.Id is int id) + query = query.Where(e => e.Id == id); + + if (request.Uuid is string uuid) + query = query.Where(e => e.Uuid == uuid); + + var envelopes = await query.AsNoTracking().ToListAsync(cancellationToken); + return _mapper.Map>(envelopes); + } +} \ No newline at end of file