private CreateCoffeeViewModel createCoffeeViewModel(Guid?id) { var coffeeViewModel = new CreateCoffeeViewModel(); try { coffeeViewModel.availableIngredients = _repository.GetAvailableIngredients(id).Select(item => new IngredientQuantityViewModel { Ingredient = item, QuantityInCoffee = 1 }).ToList(); if (id != null) { var coffeeToEdit = _repository.FindCoffee(id); coffeeViewModel.CoffeeId = id; coffeeViewModel.selectedIngredients = _repository.GetIngredientsForCoffee(id) .Select(x => x.IngredientId.ToString()) .ToList(); coffeeViewModel.Name = coffeeToEdit.Name; coffeeViewModel.selectedIngredientsQuantity = _repository.GetSelectedIngredientQuantitiesForCoffee(id, coffeeViewModel.selectedIngredients) .ToList(); coffeeViewModel.IncomeCoef = coffeeToEdit.IncomeCoef; coffeeViewModel.ImgUrl = coffeeToEdit.ImgUrl; coffeeViewModel.BasePrice = coffeeToEdit.BasePrice; coffeeViewModel.Description = coffeeToEdit.Description; coffeeViewModel.QuantityInStock = coffeeToEdit.QuantityInStock; } return(coffeeViewModel); } catch (Exception) { throw new Exception(); } }
private ActionResult AddShoppingCartCustom(CreateCoffeeViewModel coffeeViewModel, HttpPostedFileBase file) { if (file != null) { string pic = Path.GetFileName(file.FileName); string path = Path.Combine( Server.MapPath("~/Content/Images/Coffee"), pic); file.SaveAs(path); // save the image path path to the database using (MemoryStream ms = new MemoryStream()) { file.InputStream.CopyTo(ms); byte[] array = ms.GetBuffer(); } coffeeViewModel.ImgUrl = "/Content/Images/Coffee/" + pic; } else { coffeeViewModel.ImgUrl = "/Content/Images/Coffee/default_coffee.JPG"; } try { coffeeViewModel.selectedIngredientsQuantity = coffeeViewModel.selectedIngredientsQuantity.Where(quan => quan != 0) .ToList(); _repository.CreateCoffee(coffeeViewModel, User.Identity.GetUserId()); return(RedirectToAction("Index")); } catch (Exception) { return(HttpNotFound()); } }
public ActionResult Edit(CreateCoffeeViewModel coffeeViewModel, HttpPostedFileBase file, string description) { try { if (coffeeViewModel.BasePrice < 0) { ModelState.AddModelError("BasePrice", "Base price can't be negative"); } if (coffeeViewModel.IncomeCoef < 1) { ModelState.AddModelError("IncomeCoef", "Income coefficient can't be less than 1."); } if (ModelState.IsValid) { coffeeViewModel.Description = description; if (file != null) { string pic = Path.GetFileName(file.FileName); string path = Path.Combine( Server.MapPath("~/Content/Images/Coffee"), pic); file.SaveAs(path); // save the image path path to the database using (MemoryStream ms = new MemoryStream()) { file.InputStream.CopyTo(ms); byte[] array = ms.GetBuffer(); } coffeeViewModel.ImgUrl = "/Content/Images/Coffee/" + pic; } coffeeViewModel.selectedIngredientsQuantity = coffeeViewModel.selectedIngredientsQuantity.Where(quan => quan != 0).ToList(); _repository.EditCoffee(coffeeViewModel); return(RedirectToAction("Index")); } return(View(createCoffeeViewModel(coffeeViewModel.CoffeeId))); } catch (Exception) { return(HttpNotFound()); } }
public ActionResult CreateCustomCoffee(CreateCoffeeViewModel coffeeViewModel, HttpPostedFileBase file, string description) { try { if (ModelState.IsValid) { return(Create(coffeeViewModel, file, description, custom: true)); } coffeeViewModel.availableIngredients = _repository.GetAvailableIngredients(null).Select(item => new IngredientQuantityViewModel { Ingredient = item, QuantityInCoffee = 1 }).ToList(); return(View("CreateCustom", coffeeViewModel)); } catch (Exception) { return(HttpNotFound()); } }
public ActionResult Create(CreateCoffeeViewModel coffeeViewModel, HttpPostedFileBase file, string description, bool custom = false) { try { if (!custom) { if (coffeeViewModel.BasePrice < 0) { ModelState.AddModelError("BasePrice", "Base price can't be negative"); } if (coffeeViewModel.IncomeCoef < 1) { ModelState.AddModelError("IncomeCoef", "Income coefficient can't be less than 1."); } } if (ModelState.IsValid) { coffeeViewModel.Description = description; if (file != null) { string pic = Path.GetFileName(file.FileName); string path = Path.Combine( Server.MapPath("~/Content/Images/Coffee"), pic); file.SaveAs(path); // save the image path path to the database using (MemoryStream ms = new MemoryStream()) { file.InputStream.CopyTo(ms); byte[] array = ms.GetBuffer(); } coffeeViewModel.ImgUrl = "/Content/Images/Coffee/" + pic; } else { coffeeViewModel.ImgUrl = "/Content/Images/Coffee/default_coffee.JPG"; } coffeeViewModel.selectedIngredientsQuantity = coffeeViewModel.selectedIngredientsQuantity.Where(quan => quan != 0) .ToList(); if (custom) { _repository.CreateCoffee(coffeeViewModel, User.Identity.GetUserId()); } else { _repository.CreateCoffee(coffeeViewModel, null); } ViewBag.Title = "Coffee Shop blabla"; return(RedirectToAction("Index")); } coffeeViewModel.availableIngredients = _repository.GetAvailableIngredients(null).Select(item => new IngredientQuantityViewModel { Ingredient = item, QuantityInCoffee = 1 }).ToList(); return(View(coffeeViewModel)); } catch (Exception) { if (custom) { throw new Exception(); } else { return(HttpNotFound()); } } }