public static string GetAll( PagingQueryParams paging = null, SortingQueryParams sorting = null, FilterQueryParams filtering = null) { var baseUrl = $"{BaseUrl}"; var queryParams = new Dictionary <string, string>(); if (paging != null) { queryParams.Add(nameof(PagingQueryParams.Page), $"{paging.Page}"); queryParams.Add(nameof(PagingQueryParams.PageSize), $"{paging.PageSize}"); } if (filtering != null) { queryParams.Add(nameof(SortingQueryParams.SortQueryExpression), $"{sorting.SortQueryExpression}"); } if (sorting != null) { queryParams.Add(nameof(FilterQueryParams.SearchTerm), $"{filtering.SearchTerm}"); } return(QueryHelpers.AddQueryString(baseUrl, queryParams)); }
public async Task <IActionResult> GetAll( [FromQuery] PagingQueryParams pagingParams, [FromQuery] SortingQueryParams sortingParams, [FromQuery] FilterQueryParams filteringParams, CancellationToken cancellationToken = default) { return(Ok(await Mediator.Send(new GetItemsQuery(pagingParams, sortingParams, filteringParams), cancellationToken))); }
/// <summary> /// Builds and retrieves Item filter function /// </summary> /// <param name="parameters">Describes search term for name, description or category name for item</param> /// <returns>Expression which is used in LINQ to SQL on database level for filtering</returns> internal Expression <Func <Item, bool> > GetFilterFn(FilterQueryParams parameters) { if (string.IsNullOrWhiteSpace(parameters.SearchTerm)) { return(null); } // normalize search term var searchTerm = parameters.SearchTerm.Trim().ToLower(); // define expression input item => item.... var input = Expression.Parameter(typeof(Item)); // create expression constant to be used var searchTermConstant = Expression.Constant(searchTerm); // define all conditions Expression <Func <Item, string, bool> > nameMatchDelegate = (item, value) => !string.IsNullOrWhiteSpace(item.Name) && item.Name.Trim().ToLower().Contains(value); var nameMatchInvoke = Expression.Invoke(nameMatchDelegate, input, searchTermConstant); Expression <Func <Item, string, bool> > descriptionMatchDelegate = (item, value) => !string.IsNullOrWhiteSpace(item.Description) && item.Description.Trim().ToLower().Contains(value); var descriptionMatchInvoke = Expression.Invoke(descriptionMatchDelegate, input, searchTermConstant); Expression <Func <Item, string, bool> > categoryMatchDelegate = (item, value) => item.Category != null && !string.IsNullOrWhiteSpace(item.Category.Name) && item.Category.Name.Trim().ToLower().Contains(value); var categoryMatchInvoke = Expression.Invoke(categoryMatchDelegate, input, searchTermConstant); var conditions = new List <Expression> { nameMatchInvoke, descriptionMatchInvoke, categoryMatchInvoke }; // item => nameMatch(item) || descriptionMatch(item) || categoryMatch(item) var body = conditions .Skip(1) .Aggregate( seed: conditions.FirstOrDefault(), func: Expression.Or); return(Expression.Lambda <Func <Item, bool> >(body, input)); }
public GetItemsQuery(PagingQueryParams pagingParams, SortingQueryParams sortingParams, FilterQueryParams filteringParams) { PagingParams = pagingParams; SortingParams = sortingParams; FilteringParams = filteringParams; }