public IEnumerable<Product> ReadAll(string SearchTerm, string Condition, string City, string Sort, string PostalCode, string Offertype, int? StartPrice, int? EndPrice, int? CategoryID) { Search search = new Search(); search.SearchTerm = SearchTerm; search.Condition = Condition; search.City = City; search.Condition = Condition; search.PostalCode = PostalCode; search.OfferType = Offertype; search.StartPrice = StartPrice; search.EndPrice = EndPrice; search.CategoryID = CategoryID; search.Sort = Sort; IEnumerable<Product> tempList = filter.FilteringList(facade.GetProductRepository().ReadAll(), search); if (tempList == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } else { return tempList; } }
public List<Product> FilteringList(List<Product> productList, Search searchObject) { if (searchObject.SearchTerm != "" && searchObject.SearchTerm != null) { productList = productList .Where(x => (x.Title.ToLower().Contains(searchObject.SearchTerm.ToLower())) || (x.Description.ToLower().Contains(searchObject.SearchTerm.ToLower()))) .ToList(); } if (searchObject.City != "" && searchObject.City != null) { productList = productList.Where(x => x.City.ToLower().Contains(searchObject.City.ToLower())).ToList(); } if (searchObject.Condition != "" && searchObject.Condition != null) { productList = productList.Where(x => x.Condition == searchObject.Condition).ToList(); } if (searchObject.OfferType != "" && searchObject.OfferType != null) { productList = productList.Where(x => x.OfferType == searchObject.OfferType).ToList(); } if (searchObject.PostalCode != null) { productList = productList.Where(x => x.PostalCode == searchObject.PostalCode).ToList(); } if (searchObject.StartPrice != null && searchObject.EndPrice != null) { productList = productList.Where(x => x.Price >= searchObject.StartPrice && x.Price <= searchObject.EndPrice).ToList(); } if (searchObject.StartPrice != null && searchObject.EndPrice == null) { productList = productList.Where(x => x.Price >= searchObject.StartPrice).ToList(); } if (searchObject.StartPrice == null && searchObject.EndPrice != null) { productList = productList.Where(x => x.Price <= searchObject.EndPrice).ToList(); } if (searchObject.CategoryID != null && searchObject.CategoryID != 0) { productList = productList.Where(x => x.ChildCategory.ID == searchObject.CategoryID).ToList(); } productList = Sort(productList, searchObject); return productList; }
private static List<Product> Sort(List<Product> productList, Search searchObject) { switch (searchObject.Sort) { case "Price: lowest first": productList = productList.OrderBy(x => x.Price).ToList(); break; case "Price: highest first": productList = productList.OrderByDescending(x => x.Price).ToList(); break; case "Date: newest first": productList = productList.OrderByDescending(x => x.CreationDate).ToList(); break; case "Date: oldest first": productList = productList.OrderBy(x => x.CreationDate).ToList(); break; } return productList; }