using AutoMapper; using DigitalData.Core.Abstraction.Application.Repository; using EnvelopeGenerator.Application.Common.Dto.Receiver; using EnvelopeGenerator.Application.Common.Query; using MediatR; using EnvelopeGenerator.Domain.Entities; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; namespace EnvelopeGenerator.Application.Receivers.Queries; /// /// Stellt eine Abfrage dar, um die Details eines Empfängers zu lesen. /// um spezifische Informationen über einen Empfänger abzurufen. /// public record ReadReceiverQuery : ReceiverQueryBase, IRequest> { /// /// Suchbegriff für eine teilweise Übereinstimmung in der E-Mail Adresse des Empfängers /// public virtual string? EmailAddressSearch { get; set; } /// /// Checks whether any of the specified query criteria have a value. /// /// /// This property returns true if at least one of the fields /// , , , or is not null. /// Usage example: The query can be executed only if at least one criterion is specified. /// [NotMapped] public override bool HasAnyCriteria => EmailAddressSearch is not null || base.HasAnyCriteria; } /// /// /// public class ReadReceiverQueryHandler : IRequestHandler> { private readonly IRepository _repository; private readonly IMapper _mapper; /// /// /// /// /// public ReadReceiverQueryHandler(IRepository repository, IMapper mapper) { _repository = repository; _mapper = mapper; } /// /// /// /// /// /// public async Task> Handle(ReadReceiverQuery request, CancellationToken cancellationToken) { var query = _repository.Query; if (request.Id is int id) { query = query.Where(r => r.Id == id); } if (request.EmailAddress is string email) { query = query.Where(r => r.EmailAddress == email); } if (!string.IsNullOrWhiteSpace(request.EmailAddressSearch)) { query = query.Where(r => EF.Functions.Like(r.EmailAddress, $"%{request.EmailAddressSearch}%")); } if (request.Signature is string signature) { query = query.Where(r => r.Signature == signature); } var receiver = await query.ToListAsync(cancellationToken); return _mapper.Map>(receiver); } }