diff --git a/EnvelopeGenerator.Application/Envelopes/Queries/ReadEnvelopeQuery.cs b/EnvelopeGenerator.Application/Envelopes/Queries/ReadEnvelopeQuery.cs index cad7dfb2..0f68e78c 100644 --- a/EnvelopeGenerator.Application/Envelopes/Queries/ReadEnvelopeQuery.cs +++ b/EnvelopeGenerator.Application/Envelopes/Queries/ReadEnvelopeQuery.cs @@ -1,18 +1,33 @@ using MediatR; using EnvelopeGenerator.Domain.Constants; 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; /// /// Repräsentiert eine Abfrage für Umschläge. /// -public record ReadEnvelopeQuery : EnvelopeQueryBase, IRequest +public record ReadEnvelopeQuery : EnvelopeQueryBase, IRequest> { /// /// Abfrage des Include des Umschlags /// public EnvelopeStatusQuery? Status { get; init; } + + /// + /// Optionaler Benutzerfilter; wenn gesetzt, werden nur Umschläge des Benutzers geladen. + /// + public int? UserId { get; init; } + + /// + /// Setzt den Benutzerkontext für die Abfrage. + /// + public ReadEnvelopeQuery Authorize(int userId) => this with { UserId = userId }; } /// @@ -65,4 +80,62 @@ public record EnvelopeStatusQuery /// Eine Liste von Statuswerten, die ignoriert werden werden. /// public EnvelopeStatus[]? Ignore { get; init; } +} + +/// +/// Verarbeitet und liefert passende -Ergebnisse. +/// +public class ReadEnvelopeQueryHandler : IRequestHandler> +{ + private readonly IRepository _repository; + private readonly IMapper _mapper; + + /// + /// + /// + /// + /// + public ReadEnvelopeQueryHandler(IRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + /// + /// + /// + /// + /// + /// + public async Task> 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>(envelopes); + } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Envelopes/Queries/ReadUserEnvelopesQuery.cs b/EnvelopeGenerator.Application/Envelopes/Queries/ReadUserEnvelopesQuery.cs deleted file mode 100644 index e2f084b4..00000000 --- a/EnvelopeGenerator.Application/Envelopes/Queries/ReadUserEnvelopesQuery.cs +++ /dev/null @@ -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; - -/// -/// -/// -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