private IQueryable <Product> AdminAddFilterOnQuery( GetAllProductsFilter filter, IQueryable <Product> queryable ) { if (!string.IsNullOrEmpty(filter?.Name)) { queryable = queryable.Where(x => x.Name.Contains(filter.Name)); } if (filter?.CategoryIds.Length > 0) { queryable = queryable.Where(x => filter.CategoryIds.Contains(x.CategoryId)); } if (filter?.StorageId > 0) { queryable = queryable.Where(x => x.StorageId == filter.StorageId); } if (filter?.ProductTier1FromPrice != null) { queryable = queryable.Where(x => x.ProductTiers .SingleOrDefault( pt => pt.Tier.TierOption == TierEnum.tier1 ) .AfterDiscountPrice >= filter.ProductTier1FromPrice); } if (filter?.ProductTier1ToPrice != null && filter.ProductTier1ToPrice > 0) { queryable = queryable.Where(x => x.ProductTiers .SingleOrDefault( pt => pt.Tier.TierOption == TierEnum.tier1 ) .AfterDiscountPrice <= filter.ProductTier1ToPrice); } if (filter?.ProductTier2FromPrice != null) { queryable = queryable.Where(x => x.ProductTiers .SingleOrDefault( pt => pt.Tier.TierOption == TierEnum.tier2 ) .AfterDiscountPrice >= filter.ProductTier2FromPrice); } if (filter?.ProductTier2ToPrice != null && filter.ProductTier2ToPrice > 0) { queryable = queryable.Where(x => x.ProductTiers .SingleOrDefault( pt => pt.Tier.TierOption == TierEnum.tier2 ) .AfterDiscountPrice <= filter.ProductTier2ToPrice); } return(queryable); }
public async Task <IActionResult> Get([FromQuery] GetAllProductsFilter filter) { return(Ok(await Mediator.Send(new GetAllProductsQuery() { PageSize = filter.PageSize, PageNumber = filter.PageNumber }))); }
public static string GenerateProductFilterQuery(this string uri, GetAllProductsFilter filter) { return(uri.AddQuery(nameof(filter.Name), filter.Name) .AddQuery(nameof(filter.Description), filter.Description) .AddQuery(nameof(filter.PriceFrom), filter.PriceFrom) .AddQuery(nameof(filter.PriceTo), filter.PriceTo) .AddQuery(nameof(filter.RatingFrom), filter.RatingFrom)); }
private string GenerateKey( GetAllProductsFilter filter, PaginationFilter pagination, SortingFilter sortingFilter) { return(MyModelCacheKey.GeneratePaginationQuery(pagination) .GenerateProductFilterQuery(filter) .GenerateSortingQuery(sortingFilter)); }
public async Task <int> AdminCountAllAsync( PaginationFilter pagination, GetAllProductsFilter filter ) { var queryable = _context.Products.AsQueryable(); queryable = AdminAddFilterOnQuery(filter, queryable); return(await queryable.CountAsync()); }
public async Task <int> CountAllAsync( PaginationFilter pagination, GetAllProductsFilter filter = null ) { var queryable = _context.Products.AsQueryable(); queryable = queryable.Where(x => x.IsDeleted == false); queryable = AddFilterOnQuery(filter, queryable); return(await queryable.CountAsync()); }
public Task <List <Product> > GetAllAsync( GetAllProductsFilter filter, PaginationFilter pagination, SortingFilter sortingFilter) { IQueryable <Product> queryable = Context.Products.Include(x => x.Ratings); queryable = FilterProducts(queryable, filter); queryable = ApplySort(queryable, sortingFilter); return(queryable.ApplyPagination(pagination).ToListAsync()); }
public Products GetProducts(GetAllProductsFilter filter = null) { var queryable = _context.Products.AsQueryable(); if (filter != null && !string.IsNullOrEmpty(filter?.Name)) { queryable = queryable.Where(x => x.Name == filter.Name); } var products = new Products(queryable.OrderBy(p => p.Name).ToList() ?? new List <Product>()); return(products); }
public Uri GetAllProductsUri( PaginationQuery pagination, GetAllProductsFilter filter, SortingFilter sorting) { var uri = new Uri(baseUri + ApiRoutes.Products.GetAll); var modifiedUri = uri.AbsoluteUri.GeneratePaginationQuery(pagination) .GenerateProductFilterQuery(filter) .GenerateSortingQuery(sorting); return(new Uri(modifiedUri)); }
public Task <List <Product> > GetAllAsync( GetAllProductsFilter filter, PaginationFilter pagination, SortingFilter sortingFilter) { var key = GenerateKey(filter, pagination, sortingFilter); return(cache.GetOrCreate(key, entry => { entry.SetOptions(cacheOptions); return productRepository.GetAllAsync(filter, pagination, sortingFilter); })); }
public async Task <IEnumerable <Product> > AdminGetAllAsync( PaginationFilter pagination, GetAllProductsFilter filter ) { var queryable = _context.Products.AsQueryable(); queryable = AdminAddFilterOnQuery(filter, queryable); var skip = (pagination.PageNumber - 1) * pagination.PageSize; return(await queryable .Skip(skip) .Take(pagination.PageSize) .Include(p => p.ProductImages) .Include(p => p.ProductTiers) .ThenInclude(pt => pt.Tier) .ToListAsync()); }
public PagedResponse <ProductResponse> CreateProductPaginatedResponse( PaginationFilter pagination, GetAllProductsFilter filter, SortingFilter sorting, List <ProductResponse> response) { var nextPage = pagination.PageNumber >= 1 ? uriService .GetAllProductsUri(new PaginationQuery(pagination.PageNumber + 1, pagination.PageSize), filter, sorting) .ToString() : null; var previousPage = pagination.PageNumber - 1 >= 1 ? uriService .GetAllProductsUri(new PaginationQuery(pagination.PageNumber - 1, pagination.PageSize), filter, sorting) .ToString() : null; return(CreatePaginatedResponse(pagination, sorting, response, nextPage, previousPage)); }
private static IQueryable <Product> FilterProducts(IQueryable <Product> queryable, GetAllProductsFilter filter) { if (!string.IsNullOrWhiteSpace(filter.Name)) { queryable = queryable.Where(x => x.Name.ToLower().Contains(filter.Name.ToLower())); } if (filter.PriceFrom.HasValue) { queryable = queryable.Where(x => x.Price >= filter.PriceFrom.Value); } if (filter.PriceTo.HasValue) { queryable = queryable.Where(x => x.Price <= filter.PriceTo.Value); } if (!string.IsNullOrWhiteSpace(filter.Description)) { queryable = queryable.Where(x => x.Description.ToLower().Contains(filter.Description.ToLower())); } if (filter.RatingFrom.HasValue) { queryable = queryable.Where(x => x.Ratings.Sum(x => x.Value) >= filter.RatingFrom.Value); } return(queryable); }