public async Task <IActionResult> GetProducts([FromQuery] FilterProductsDTO filter) { var products = await productService.FilterProducts(filter); // await Task.Delay(4000); return(JsonResponseStatus.Success(products)); }
public async Task <FilterProductsDTO> FilterProducts(FilterProductsDTO filter) { var productsQuery = productRepository.GetEntitiesQuery().AsQueryable(); switch (filter.OrderBy) { case ProductOrderBy.PriceAsc: productsQuery = productsQuery.OrderBy(s => s.Price); break; case ProductOrderBy.PriceDec: productsQuery = productsQuery.OrderByDescending(s => s.Price); break; } if (!string.IsNullOrEmpty(filter.Title)) { productsQuery = productsQuery.Where(s => s.ProductName.Contains(filter.Title)); } if (filter.StartPrice != 0) { productsQuery = productsQuery.Where(s => s.Price >= filter.StartPrice); } if (filter.EndPrice != 0) { productsQuery = productsQuery.Where(s => s.Price <= filter.EndPrice); } productsQuery = productsQuery.Where(s => s.Price >= filter.StartPrice); if (filter.Categories != null && filter.Categories.Any()) { productsQuery = productsQuery.SelectMany(s => s.ProductSelectedCategories.Where(f => filter.Categories.Contains(f.ProductCategoryId)).Select(t => t.Product)); } if (filter.EndPrice != 0) { productsQuery = productsQuery.Where(s => s.Price <= filter.EndPrice); } var count = (int)Math.Ceiling(productsQuery.Count() / (double)filter.TakeEntity); var pager = Pager.Build(count, filter.PageId, filter.TakeEntity); var products = await productsQuery.Paging(pager).ToListAsync(); return(filter.SetProducts(products).SetPaging(pager)); }
public async Task <FilterProductsDTO> FilterProducts(FilterProductsDTO filter) { //IQueryRable<Product> = _context.product; var productQuery = _productRepository.GetEntitiesQuery().AsQueryable(); //FilterPrice switch (filter.OrderBy) { case ProductOrderBy.PriceAsc: productQuery = productQuery.OrderBy(s => s.Price); break; case ProductOrderBy.PriceDec: productQuery = productQuery.OrderByDescending(s => s.Price); break; } //Search Title Filter if (!string.IsNullOrEmpty(filter.Title)) { productQuery = productQuery.Where(s => s.ProductName.Contains(filter.Title)); } //Price Filter productQuery = productQuery.Where(s => s.Price >= filter.StartPrice); if (filter.EndPrice != 0) { productQuery = productQuery.Where(c => c.Price <= filter.EndPrice); } if (filter.categories != null && filter.categories.Any()) { productQuery = productQuery.SelectMany(s => s.ProductSelectedCategorys.Where(f => filter.categories.Contains(f.ProductCategoryId)).Select(t => t.Product)); } //اعداد محصولات var count = (int)Math.Ceiling(productQuery.Count() / (double)filter.TakeEntity); var pager = Pager.Build(count, filter.pageId, filter.TakeEntity); var products = await productQuery.Paging(pager).ToListAsync(); //برای این ما خود فیلتر را برمیگردونیم که اگر در فیلتر محصولات استرینگی پر باشد پر باقی بماند return(filter.SetProducts(products).SetPaging(pager)); }
public JsonResult Filter([FromBody] FilterProductsDTO data) { IEnumerable <ProductDTO> products = _productRepository.Filter(data); PagingInfo pagingInfo = new PagingInfo { TotalItems = products.Count(), CurrentPage = data.Page }; products = products .Skip((pagingInfo.CurrentPage - 1) * pagingInfo.ItemsPerPage) .Take(pagingInfo.ItemsPerPage); HttpContext.Response.Headers.Add("Paging", JsonConvert.SerializeObject(pagingInfo)); return(Json(products, JsonHelper.SerializerSettings)); }
public IEnumerable <ProductDTO> Filter(FilterProductsDTO filter) { return(_context.Products .Where(p => p.Brand.Name.Contains(filter.Brand)) .Where(p => p.Category.CategoryName.Contains(filter.Category)) .Where(p => p.PartNumber.Contains(filter.PartNumber)) .Where(p => p.Section.SectionName.Contains(filter.Section)) .Where(p => p.ProductSectionGroups .Contains(p.ProductSectionGroups .Where(psg => psg.SectionGroup.SectionGroupName.Contains(filter.SectionGroup)).FirstOrDefault())) .Where(p => p.ProductMachines.Contains(p.ProductMachines.Where(pm => pm.Machine.ModelName.Contains(filter.Machine)).FirstOrDefault())) //.Include(p => p.Category) .Select(p => new ProductDTO { ProductId = p.ProductId, PartNumber = p.PartNumber, Category = p.Category.CategoryName })); }