From 5468d7b2aa15004d4cbba770c65fe23a6c0cd2bb Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 26 Aug 2025 22:05:33 +0200 Subject: [PATCH] feat: add QueryExtensions for filtering by Envelope and Receiver - Introduced extension methods on IQueryable to filter entities by Envelope (Id or Uuid) and Receiver (Id, EmailAddress, or Signature). - Throws BadRequestException if no valid identifier is provided when notnull = true. - Improves query handling consistency across application layer. --- .../Extensions/QueryExtensions.cs | 57 ++++++++++++++++++- .../Interfaces/IHasEnvelope.cs | 2 +- .../Interfaces/IHasReceiver.cs | 2 +- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/EnvelopeGenerator.Application/Extensions/QueryExtensions.cs b/EnvelopeGenerator.Application/Extensions/QueryExtensions.cs index 4235dd54..a43cec9b 100644 --- a/EnvelopeGenerator.Application/Extensions/QueryExtensions.cs +++ b/EnvelopeGenerator.Application/Extensions/QueryExtensions.cs @@ -1,8 +1,63 @@ -namespace EnvelopeGenerator.Application.Extensions; +using DigitalData.Core.Exceptions; +using EnvelopeGenerator.Application.Model; +using EnvelopeGenerator.Domain.Interfaces; + +namespace EnvelopeGenerator.Application.Extensions; /// /// /// public static class QueryExtensions { + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static IQueryable Where(this IQueryable root, IHasEnvelopeQuery query, bool notnull = true) + where TEntity : IHasEnvelope where TEnvelopeQuery : EnvelopeQueryBase + { + if (query.Envelope.Id is not null) + root = root.Where(e => e.Envelope!.Id == query.Envelope.Id); + else if (query.Envelope.Uuid is not null) + root = root.Where(e => e.Envelope!.Uuid == query.Envelope.Uuid); + else if (notnull) + throw new BadRequestException( + "Either Envelope Id or Envelope Uuid must be provided in the query." + ); + + return root; + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static IQueryable Where(this IQueryable root, IHasReceiverQuery query, bool notnull = true) + where TEntity : IHasReceiver where TReceiverQueryBase : ReceiverQueryBase + { + if (query.Receiver.Id is not null) + root = root.Where(e => e.Receiver!.Id == query.Receiver.Id); + else if (query.Receiver.EmailAddress is not null) + root = root.Where(e => e.Receiver!.EmailAddress == query.Receiver.EmailAddress); + else if (query.Receiver.Signature is not null) + root = root.Where(e => e.Receiver!.Signature == query.Receiver.Signature); + else if (notnull) + throw new BadRequestException( + "Receiver must have at least one identifier (Id, EmailAddress, or Signature)." + ); + + return root; + } } diff --git a/EnvelopeGenerator.Domain/Interfaces/IHasEnvelope.cs b/EnvelopeGenerator.Domain/Interfaces/IHasEnvelope.cs index bfc38712..93eea3bd 100644 --- a/EnvelopeGenerator.Domain/Interfaces/IHasEnvelope.cs +++ b/EnvelopeGenerator.Domain/Interfaces/IHasEnvelope.cs @@ -5,7 +5,7 @@ { #endif -interface IHasEnvelope +public interface IHasEnvelope { #if NET public diff --git a/EnvelopeGenerator.Domain/Interfaces/IHasReceiver.cs b/EnvelopeGenerator.Domain/Interfaces/IHasReceiver.cs index d14c9ca6..0b105a3e 100644 --- a/EnvelopeGenerator.Domain/Interfaces/IHasReceiver.cs +++ b/EnvelopeGenerator.Domain/Interfaces/IHasReceiver.cs @@ -5,7 +5,7 @@ { #endif -interface IHasReceiver +public interface IHasReceiver { #if NET public