63 lines
2.2 KiB
C#
63 lines
2.2 KiB
C#
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Model;
|
|
using EnvelopeGenerator.Domain.Interfaces;
|
|
|
|
namespace EnvelopeGenerator.Application.Extensions;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class QueryExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
/// <param name="root"></param>
|
|
/// <param name="query"></param>
|
|
/// <param name="notnull"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="BadRequestException"></exception>
|
|
public static IQueryable<TEntity> Where<TEntity>(this IQueryable<TEntity> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
/// <typeparam name="TReceiverQueryBase"></typeparam>
|
|
/// <param name="root"></param>
|
|
/// <param name="query"></param>
|
|
/// <param name="notnull"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="BadRequestException"></exception>
|
|
public static IQueryable<TEntity> Where<TEntity, TReceiverQueryBase>(this IQueryable<TEntity> 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;
|
|
}
|
|
}
|