public IActionResult Update(ProductModels productModels) { if (!ModelState.IsValid) { foreach (var err in ModelState.Values) { foreach (var error in err.Errors) { ModelState.AddModelError(string.Empty, error.ErrorMessage); } } return(RedirectToAction(nameof(Index), productModels)); } var repo = new RepoProducts(); string message = string.Empty; bool result = repo.UpdateProduct(productModels, out message); if (!result) { TempData["ErrorResult"] = message; return(RedirectToAction(nameof(Edit), productModels)); } TempData["Result"] = message; return(RedirectToAction(nameof(Index))); }
public IActionResult ViewProducts() { var repo = new RepoProducts(); var products = repo.GetProductModels(); return(View(products)); }
public JsonResult GetProducts() { var repo = new RepoProducts(); var products = repo.GetProductModels(); return(Json(new{ productResult = products })); }
public IActionResult Edit(string key) { var repo = new RepoProducts(); int id = key.ConvertToNumber(); var product = repo.GetProduct(id); if (product == null) { return(Content("This product is not found!")); } return(View(product)); }
public IActionResult Delete(string key) { var repo = new RepoProducts(); string message = string.Empty; int id = key.ConvertToNumber(); bool result = repo.DeleteProduct(id, out message); if (!result) { return(Content(message)); } TempData["Result"] = message; return(RedirectToAction(nameof(Index))); }
public async Task <IList <ProductRepoModel> > GetProducts(int?brandId, int?categoryId, string brand, string category, string searchString, double?minPrice, double?maxPrice) { if (!RepoSucceeds) { return(new List <ProductRepoModel>()); } IList <ProductRepoModel> result = RepoProducts; IEnumerable <ProductRepoModel> toRemove; if (brandId != null && brandId > 0) { toRemove = result.Where(p => p.BrandId != brandId).ToList(); if (toRemove.Count() > 0) { foreach (ProductRepoModel product in toRemove) { RepoProducts.Remove(product); } } } if (categoryId != null && categoryId > 0) { toRemove = result.Where(p => p.CategoryId != categoryId).ToList(); if (toRemove.Count() > 0) { foreach (ProductRepoModel product in toRemove) { RepoProducts.Remove(product); } } } if (minPrice != null && minPrice > 0) { toRemove = result.Where(p => p.Price < minPrice).ToList(); if (toRemove.Count() > 0) { foreach (ProductRepoModel product in toRemove) { RepoProducts.Remove(product); } } } if (maxPrice != null && maxPrice > 0) { toRemove = result.Where(p => p.Price > maxPrice).ToList(); if (toRemove.Count() > 0) { foreach (ProductRepoModel product in toRemove) { RepoProducts.Remove(product); } } } if (!string.IsNullOrEmpty(searchString)) { toRemove = result.Where(p => !p.Name.Contains(searchString) && !p.Description.Contains(searchString)).ToList(); if (toRemove.Count() > 0) { foreach (ProductRepoModel product in toRemove) { RepoProducts.Remove(product); } } } if (!string.IsNullOrEmpty(brand)) { toRemove = result.Where(p => p.Brand != brand).ToList(); if (toRemove.Count() > 0) { foreach (ProductRepoModel product in toRemove) { RepoProducts.Remove(product); } } } if (!string.IsNullOrEmpty(category)) { toRemove = result.Where(p => p.Category != category).ToList(); if (toRemove.Count() > 0) { foreach (ProductRepoModel product in toRemove) { RepoProducts.Remove(product); } } } return(result); }