public static Expression <Func <T, bool> > GetSearchExpression <T>(IListArgs args) { if (args?.Search == null || args?.SearchOn == null) { return(null); } var param = Expression.Parameter(typeof(T), args?.SearchOn); var expr = GetExpression <T>(param, new ListFilter() { PropertyName = args?.SearchOn, FilterValues = new List <ListFilterValue>() { new ListFilterValue() { Operator = ListFilterOperator.Equal, Term = args?.Search, WildcardPositions = new List <int>() { 0, 1 } } } }); return(Expression.Lambda <Func <T, bool> >(expr, param)); }
// The IListArgs model describes list arguments that let api users query data expressively with query params. public async Task <ListPage <Order> > ListOrdersForBillingAddress(IListArgs args) { var me = await _user.OcClient.Me.GetAsync(); if (me.xp.FranchiseRole != "Owner") { throw new UnAuthorizedException(); } var locationID = me.xp.BillingAddressID; var billingAddressFilter = new ListFilter("BillingAddress.ID", locationID); // Add a filter on top of any user-defined filters. Only return orders where Order.BillingAddress.ID equals the user's. args.Filters.Add(billingAddressFilter); // Request orders from Ordercloud from the admin endpoint with elevated access. var orders = await _oc.Orders.ListAsync(OrderDirection.Incoming, // Apply all the list arguments to the request page : args.Page, pageSize : args.PageSize, sortBy : string.Join(',', args.SortBy), search : args.Search, searchOn : args.SearchOn, filters : args.ToFilterString()); return(orders); }
public static Expression <Func <T, bool> > GetSearchExpression <T>(IListArgs args) { if (args?.Search == null || args?.SearchOn == null) { return(null); } var param = Expression.Parameter(typeof(T), args?.SearchOn); var expr = GetExpression <T>(param, new ListFilter(args?.SearchOn, $"*{args?.Search}*")); return(Expression.Lambda <Func <T, bool> >(expr, param)); }
public static Expression <Func <T, bool> > GetFilterExpression <T>(IListArgs args) { if (args?.Filters == null || args?.Filters.Count == 0) { return(null); } var filters = args?.Filters; var param = Expression.Parameter(typeof(T), typeof(T).Name); Expression exp = filters.ANDExpressions(filter => GetExpression <T>(param, filter)); return(Expression.Lambda <Func <T, bool> >(exp, param)); }
public async Task <ListPage <OrchestrationLog> > List(IListArgs args) { var query = _store.Query(new FeedOptions() { EnableCrossPartitionQuery = true }) .Search(args) .Filter(args) .Sort(args); var list = await query.WithPagination(args.Page, args.PageSize).ToPagedListAsync(); var count = await query.CountAsync(); return(list.ToListPage(args.Page, args.PageSize, count)); }
public virtual void LoadFromQueryString(IQueryCollection query, IListArgs listArgs) { listArgs.Filters = new List <ListFilter>(); foreach (var(key, value) in query) { int i; switch (key.ToLower()) { case "sortby": listArgs.SortBy = value.ToString().Split(',').Distinct().ToArray(); break; case "page": if (int.TryParse(value, out i) && i >= 1) { listArgs.Page = i; } else { throw new OrderCloudIntegrationException.UserErrorException("page must be an integer greater than or equal to 1."); } break; case "pagesize": if (int.TryParse(value, out i) && i >= 1 && i <= 100) { listArgs.PageSize = i; } else { throw new OrderCloudIntegrationException.UserErrorException($"pageSize must be an integer between 1 and 100."); } break; case "search": listArgs.Search = value.ToString(); break; case "searchon": listArgs.SearchOn = value.ToString(); break; default: listArgs.Filters.Add(ListFilter.Parse(key, value)); break; } } }
public static IQueryable <T> Search <T>(this IQueryable <T> source, IListArgs args) where T : class { var expr = ExpressionBuilder.GetSearchExpression <T>(args); if (expr == null) { return(source); } var genericMethod = typeof(Queryable) .GetMethods() .First(m => m.Name == "Where" && m.IsGenericMethodDefinition && m.GetParameters().ToList().Count == 2) .MakeGenericMethod(typeof(T)); var query = (IQueryable <T>)genericMethod.Invoke(genericMethod, new object[] { source, expr }); return(query); }
public static IOrderedQueryable <T> Sort <T>(this IQueryable <T> source, IListArgs args) where T : class { var expr = ExpressionBuilder.GetSortByExpression <T>(args); if (expr == null) { return(source as IOrderedQueryable <T>); } var direction = args.SortBy[0].Contains("!") ? "OrderByDescending" : "OrderBy"; var genericMethod = typeof(Queryable) .GetMethods() .First(m => m.Name == direction && m.GetParameters().Count() == 2) .MakeGenericMethod(typeof(T), expr.Item2); var query = (IOrderedQueryable <T>)genericMethod.Invoke(null, new object[] { source, expr.Item1 }); return(query); }
public static Tuple <Expression, Type> GetSortByExpression <TSource>(IListArgs args) { if (args?.SortBy.Count == 0) { return(null); } var sort = args?.SortBy; var param = Expression.Parameter(typeof(TSource), typeof(TSource).Name); Tuple <Expression, Type> exp; switch (sort?.Count) { case 1: exp = GetExpression <TSource>(param, sort[0]); break; default: throw new NotImplementedException("Multiple sort is not supported"); } return(Tuple.Create(Expression.Lambda(exp.Item1, param) as Expression, exp.Item2)); }