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.
79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record ReadUserEnvelopesQuery : EnvelopeQueryBase, IRequest<IEnumerable<EnvelopeDto>>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public required int UserId { get; init; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public EnvelopeStatusQuery? Status { get; init; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ReadUserEnvelopesQueryHandler : IRequestHandler<ReadUserEnvelopesQuery, IEnumerable<EnvelopeDto>>
|
|
{
|
|
private readonly IRepository<Envelope> _repository;
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repository"></param>
|
|
/// <param name="mapper"></param>
|
|
public ReadUserEnvelopesQueryHandler(IRepository<Envelope> repository, IMapper mapper)
|
|
{
|
|
_repository = repository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<EnvelopeDto>> 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<IEnumerable<EnvelopeDto>>(envelopes);
|
|
}
|
|
} |