Refactor envelope queries into unified ReadEnvelopeQuery
Consolidate envelope querying logic by extending ReadEnvelopeQuery to support user filtering and status options, and introduce ReadEnvelopeQueryHandler to process all envelope queries. Remove ReadUserEnvelopesQuery and its handler, reducing duplication and improving maintainability.
This commit is contained in:
@@ -1,18 +1,33 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using EnvelopeGenerator.Domain.Constants;
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
using EnvelopeGenerator.Application.Common.Query;
|
using EnvelopeGenerator.Application.Common.Query;
|
||||||
|
using EnvelopeGenerator.Application.Common.Dto;
|
||||||
|
using AutoMapper;
|
||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Application.Envelopes.Queries;
|
namespace EnvelopeGenerator.Application.Envelopes.Queries;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Repräsentiert eine Abfrage für Umschläge.
|
/// Repräsentiert eine Abfrage für Umschläge.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record ReadEnvelopeQuery : EnvelopeQueryBase, IRequest
|
public record ReadEnvelopeQuery : EnvelopeQueryBase, IRequest<IEnumerable<EnvelopeDto>>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Abfrage des Include des Umschlags
|
/// Abfrage des Include des Umschlags
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public EnvelopeStatusQuery? Status { get; init; }
|
public EnvelopeStatusQuery? Status { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optionaler Benutzerfilter; wenn gesetzt, werden nur Umschläge des Benutzers geladen.
|
||||||
|
/// </summary>
|
||||||
|
public int? UserId { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Setzt den Benutzerkontext für die Abfrage.
|
||||||
|
/// </summary>
|
||||||
|
public ReadEnvelopeQuery Authorize(int userId) => this with { UserId = userId };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -65,4 +80,62 @@ public record EnvelopeStatusQuery
|
|||||||
/// Eine Liste von Statuswerten, die ignoriert werden werden.
|
/// Eine Liste von Statuswerten, die ignoriert werden werden.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public EnvelopeStatus[]? Ignore { get; init; }
|
public EnvelopeStatus[]? Ignore { get; init; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verarbeitet <see cref="ReadEnvelopeQuery"/> und liefert passende <see cref="EnvelopeDto"/>-Ergebnisse.
|
||||||
|
/// </summary>
|
||||||
|
public class ReadEnvelopeQueryHandler : IRequestHandler<ReadEnvelopeQuery, IEnumerable<EnvelopeDto>>
|
||||||
|
{
|
||||||
|
private readonly IRepository<Envelope> _repository;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="repository"></param>
|
||||||
|
/// <param name="mapper"></param>
|
||||||
|
public ReadEnvelopeQueryHandler(IRepository<Envelope> repository, IMapper mapper)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancel"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IEnumerable<EnvelopeDto>> Handle(ReadEnvelopeQuery request, CancellationToken cancel)
|
||||||
|
{
|
||||||
|
var query = _repository.Query;
|
||||||
|
|
||||||
|
if (request.UserId is int userId)
|
||||||
|
query = query.Where(e => e.UserId == userId);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
var envelopes = await query
|
||||||
|
.Include(e => e.Documents)
|
||||||
|
.ToListAsync(cancel);
|
||||||
|
|
||||||
|
return _mapper.Map<IEnumerable<EnvelopeDto>>(envelopes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user