public ProductListResultDto GetProductByQuerySearch(SearchProductInput input) { if (input.MaxResultCount <= 0) { input.MaxResultCount = AppConsts.MaxResultCount; } if (String.IsNullOrEmpty(input.Sorting)) { input.Sorting = AppConsts.DefaultSortingField; } var products = _productRepository.GetAllIncluding(x => x.Variants) .WhereIf(input.TenantId.HasValue, t => t.TenantId == input.TenantId.Value) .WhereIf(!String.IsNullOrEmpty(input.SearchText), t => t.Code.Contains(input.SearchText) || t.Name.Contains(input.SearchText) || t.Description.Contains(input.SearchText)) .WhereIf(input.Category.HasValue, t => t.CategoryId == input.Category.Value) .OrderBy(input.Sorting) .PageBy(input) ; if (!String.IsNullOrEmpty(input.Price)) { var priceList = input.Price.Split('|'); var prices = priceList.ToList().Select(s => Double.Parse(s)); if (prices.Count() > 1) { products = products.Where(t => t.Variants.FirstOrDefault().Price >= prices.First() && t.Variants.FirstOrDefault().Price <= prices.Last()); } } var productsCount = products.ToList().Count(); return(new ProductListResultDto() { TotalCount = productsCount, Items = products.MapTo <List <ProductOutput> >() }); }
public SearchProductOutput Search(SearchProductInput input) { using (var dbContext = new AllureContext()) { IQueryable <Product> query = dbContext .Set <Product>() .Include(p => p.Localized) .Include(p => p.Images); if (input.BrandId.HasValue) { query = query.Where(p => p.BrandId == input.BrandId.Value); } if (!input.Number.IsNullOrEmpty()) { query = query.Where(p => p.Number == input.Number); } if (!input.Locale.IsNullOrEmpty()) { query = query.Where(p => p.Locale.Localized.Any(l => l.Name.Contains(input.Locale))); } if (!input.Name.IsNullOrEmpty()) { query = query.Where(p => p.Name.Contains(input.Name)); } if (input.MaxPrice.HasValue) { query = query.Where(p => p.Price <= input.MaxPrice.Value); } if (input.MinPrice.HasValue) { query = query.Where(p => p.Price >= input.MinPrice.Value); } var orderBy = "DisplayOrder"; if (!input.OrderBy.IsNullOrEmpty()) { orderBy = input.OrderBy + "," + orderBy; } var result = new SearchProductOutput(); result.Count = query.Count(); var pageSize = input.PageSize.GetValueOrDefault(10); var pageNumber = input.PageNumber.GetValueOrDefault(1) - 1; result.Products = query .OrderBy(orderBy) .Skip(pageNumber * pageSize) .Take(pageSize) .Where(p => p.Status == DataStatus.Normal) .ToArray() .Select(p => new ProductOutput(p)) .ToArray(); return(result); } }