2021-11-26 15:47:11 +01:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Raven.Client.Documents.Linq;
|
|
|
|
|
using Raven.Client.Documents.Session;
|
|
|
|
|
|
2021-11-29 18:25:50 +01:00
|
|
|
namespace zero.Api.Models;
|
2021-11-26 15:47:11 +01:00
|
|
|
|
|
|
|
|
public static class ListQueryExtensions
|
|
|
|
|
{
|
|
|
|
|
public static ListQuery<T, TFilter> AsList<T, TFilter>(this string options) where TFilter : IListSpecificQuery
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<ListQuery<T, TFilter>>(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ListQuery<T> AsList<T>(this string options) where T : IListSpecificQuery
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<ListQuery<T>>(options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 17:34:42 +01:00
|
|
|
public static IQueryable<T> Filter<T>(this IQueryable<T> source, ListQuery<T> listQuery) where T : ZeroIdEntity
|
2021-11-26 15:47:11 +01:00
|
|
|
{
|
2021-11-27 16:09:02 +01:00
|
|
|
if (listQuery == null)
|
|
|
|
|
{
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 15:47:11 +01:00
|
|
|
Type collectionType = typeof(T);
|
|
|
|
|
Type zeroType = typeof(ZeroEntity);
|
|
|
|
|
bool isZeroType = zeroType.IsAssignableFrom(collectionType);
|
|
|
|
|
|
|
|
|
|
IQueryable<T> queryable = source;
|
2021-12-17 13:51:16 +01:00
|
|
|
|
|
|
|
|
if (listQuery.AdditionalQuery != null)
|
|
|
|
|
{
|
|
|
|
|
queryable = listQuery.AdditionalQuery.Compile()(queryable);
|
|
|
|
|
}
|
2021-11-26 15:47:11 +01:00
|
|
|
|
|
|
|
|
if (listQuery.Ids.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
queryable = queryable.Where(x => x.Id.In(listQuery.Ids));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!listQuery.Search.IsNullOrEmpty() && listQuery.SearchSelectors.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var selector in listQuery.SearchSelectors)
|
|
|
|
|
{
|
|
|
|
|
queryable = queryable.SearchIf(selector, listQuery.Search, "*", "*", Raven.Client.Documents.Queries.SearchOperator.And);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!listQuery.Search.IsNullOrEmpty() && listQuery.SearchSelector != null)
|
|
|
|
|
{
|
|
|
|
|
queryable = queryable.SearchIf(listQuery.SearchSelector, listQuery.Search, "*", "*", Raven.Client.Documents.Queries.SearchOperator.And);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (listQuery.OrderQuery != null)
|
|
|
|
|
{
|
|
|
|
|
queryable = listQuery.OrderQuery(queryable);
|
|
|
|
|
}
|
|
|
|
|
else if (!listQuery.OrderBy.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
queryable = queryable.OrderBy(listQuery.OrderBy, listQuery.OrderIsDescending, listQuery.OrderType == ListQueryOrderType.String ? OrderingType.String : OrderingType.Double);
|
|
|
|
|
}
|
|
|
|
|
else if (isZeroType)
|
|
|
|
|
{
|
|
|
|
|
queryable = queryable.OrderByDescending(x => (x as ZeroEntity).CreatedDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return queryable;
|
|
|
|
|
}
|
|
|
|
|
}
|