public IActionResult Index() { var allProducts = _productsService.GetAll(); var productsModels = allProducts .Select(p => new ProductsDetailsModel { IdProduct = p.IdProduct, ProductName = p.ProductName ?? "No First Name Provided", ProductNumber = p.ProductNumber, ProductDescription = p.ProductDescription, QuantityUnit = p.QuantityUnit, Discount = p.Discount, CategoryName = new List <SelectListItem>() { new SelectListItem(p.Categoriess.NameCategories, "1") } }).ToList(); var model = new ProductsIndexModel { Products = productsModels }; return(View(model)); }
public void CanReturnAllProducts() { //try finding all products List <Products> testProducts = MockProductsServices.GetAll(); //test if null Assert.IsNotNull(testProducts); //verify the correct number Assert.AreEqual(3, testProducts.Count); }
public async Task <IActionResult> Search(string sortOrder, string currentFilter, string searchString) { ViewData["CurrentSort"] = sortOrder; ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewData["UnitPriceSortParm"] = sortOrder == "Price" ? "Price_desc" : "Price"; if (searchString != null) { if (searchString.Length < 2) { ViewData["CurrentFilterError"] = "Min character Limt 2."; return(View()); } } else { searchString = currentFilter; } ViewData["CurrentFilter"] = searchString; List <Products> products = _productsServices.GetAll().ToList(); List <ProductsDto> productsResult = Mapper.Map <List <ProductsDto> >(products); if (!String.IsNullOrEmpty(searchString)) { productsResult = productsResult.Where(s => s.ProductName.Contains(searchString.Trim()) || s.Description.Contains(searchString.Trim())).ToList(); } switch (sortOrder) { case "name_desc": productsResult = productsResult.OrderByDescending(s => s.ProductName).ToList(); break; case "Price": productsResult = productsResult.OrderBy(s => s.UnitPrice).ToList(); break; case "Price_desc": productsResult = productsResult.OrderByDescending(s => s.UnitPrice).ToList(); break; default: productsResult = productsResult.OrderBy(s => s.ProductName).ToList(); break; } return(View(productsResult)); }