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