From 94018d2a362213106ab1f3573bea81d70da62982 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 28 Jan 2026 12:51:40 +0100 Subject: [PATCH] Add query handler to fetch receiver's latest used name Introduced ReadReceiverNameQueryHandler to retrieve the most recently used name ("Anrede") for a receiver, supporting envelope generation. Updated ReadReceiverNameQuery to use MediatR and CQRS patterns, and implemented repository-based lookups for receiver identification and name retrieval. Added necessary using directives for repository and MediatR support. --- .../Queries/ReadReceiverNameQuery.cs | 58 ++++++++++++++++++- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/EnvelopeGenerator.Application/Envelopes/Queries/ReadReceiverNameQuery.cs b/EnvelopeGenerator.Application/Envelopes/Queries/ReadReceiverNameQuery.cs index 106bcb34..86daffc9 100644 --- a/EnvelopeGenerator.Application/Envelopes/Queries/ReadReceiverNameQuery.cs +++ b/EnvelopeGenerator.Application/Envelopes/Queries/ReadReceiverNameQuery.cs @@ -1,4 +1,8 @@ -using EnvelopeGenerator.Application.Receivers.Queries; +using DigitalData.Core.Abstraction.Application.Repository; +using EnvelopeGenerator.Application.Common.Query; +using MediatR; +using EnvelopeGenerator.Domain.Entities; +using Microsoft.EntityFrameworkCore; namespace EnvelopeGenerator.Application.Envelopes.Queries; @@ -6,6 +10,54 @@ namespace EnvelopeGenerator.Application.Envelopes.Queries; /// Eine Abfrage, um die zuletzt verwendete Anrede eines Empfängers zu ermitteln, /// damit diese für zukünftige Umschläge wiederverwendet werden kann. /// -public record ReadReceiverNameQuery() : ReadReceiverQuery +public record ReadReceiverNameQuery() : ReceiverQueryBase, IRequest; + +/// +/// +/// +public class ReadReceiverNameQueryHandler : IRequestHandler { -} + private readonly IRepository _envelopeReceiverRepository; + private readonly IRepository _receiverRepository; + + /// + /// + /// + /// + /// + public ReadReceiverNameQueryHandler(IRepository envelopeReceiverRepository, IRepository receiverRepository) + { + _envelopeReceiverRepository = envelopeReceiverRepository; + _receiverRepository = receiverRepository; + } + + /// + /// + /// + /// + /// + /// + public async Task Handle(ReadReceiverNameQuery request, CancellationToken cancellationToken) + { + var receiverQuery = _receiverRepository.Query.AsNoTracking(); + + if (request.Id is int id) + receiverQuery = receiverQuery.Where(r => r.Id == id); + if (request.EmailAddress is string email) + receiverQuery = receiverQuery.Where(r => r.EmailAddress == email); + if (request.Signature is string signature) + receiverQuery = receiverQuery.Where(r => r.Signature == signature); + + var receiver = await receiverQuery.FirstOrDefaultAsync(cancellationToken); + if (receiver is null) + return null; + + var erName = await _envelopeReceiverRepository.Query + .Where(er => er.ReceiverId == receiver.Id) + .OrderByDescending(er => er.AddedWhen) + .Select(er => er.Name) + .FirstOrDefaultAsync(cancellationToken); + + return erName; + } +} \ No newline at end of file