using DigitalData.Core.Exceptions;
using EnvelopeGenerator.Application.Common.Model;
using EnvelopeGenerator.Domain.Interfaces;
namespace EnvelopeGenerator.Application.Extensions;
///
///
///
public static class QueryExtensions
{
///
///
///
///
///
///
///
///
///
public static IQueryable Where(this IQueryable root, EnvelopeQueryBase query, bool notnull = true)
where TEntity : IHasEnvelope
{
if (query.Id is not null)
root = root.Where(e => e.Envelope!.Id == query.Id);
else if (query.Uuid is not null)
root = root.Where(e => e.Envelope!.Uuid == query.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, ReceiverQueryBase query, bool notnull = true)
where TEntity : IHasReceiver
{
if (query.Id is not null)
root = root.Where(e => e.Receiver!.Id == query.Id);
else if (query.EmailAddress is not null)
root = root.Where(e => e.Receiver!.EmailAddress == query.EmailAddress);
else if (query.Signature is not null)
root = root.Where(e => e.Receiver!.Signature == query.Signature);
else if (notnull)
throw new BadRequestException(
"Receiver must have at least one identifier (Id, EmailAddress, or Signature)."
);
return root;
}
///
///
///
///
///
///
///
///
///
///
public static IQueryable Where(this IQueryable root, EnvelopeReceiverQueryBase query, bool notnull = true)
where TEntity : IHasEnvelope, IHasReceiver
where TEnvelopeQuery : EnvelopeQueryBase, new()
where TReceiverQuery : ReceiverQueryBase, new()
=> root.Where(query.Envelope, notnull).Where(query.Receiver, notnull);
}