public void CreateOrUpdate(IngredientBindingModel model) { Ingredient tempIngredient = model.Id.HasValue ? null : new Ingredient { Id = 1 }; foreach (var ingredient in source.Ingredients) { if (ingredient.IngredientName == model.IngredientName && ingredient.Id != model.Id) { throw new Exception("Уже есть ингредиент с таким названием"); } if (!model.Id.HasValue && ingredient.Id >= tempIngredient.Id) { tempIngredient.Id = ingredient.Id + 1; } else if (model.Id.HasValue && ingredient.Id == model.Id) { tempIngredient = ingredient; } } if (model.Id.HasValue) { if (tempIngredient == null) { throw new Exception("Элемент не найден"); } CreateModel(model, tempIngredient); } else { source.Ingredients.Add(CreateModel(model, tempIngredient)); } }
// GET: Ingredients/Edit/5 public ActionResult Edit(Guid id) { var entity = manager.Find(id); var model = new IngredientBindingModel(entity); return(View(model)); }
public void CreateOrUpdate(IngredientBindingModel model) { Ingredient element = source.Ingredients.FirstOrDefault(rec => rec.IngredientName == model.IngredientName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть ингредиент с таким названием"); } if (model.Id.HasValue) { element = source.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { int maxId = source.Ingredients.Count > 0 ? source.Ingredients.Max(rec => rec.Id) : 0; element = new Ingredient { Id = maxId + 1 }; source.Ingredients.Add(element); } element.IngredientName = model.IngredientName; }
public void CreateOrUpdate(IngredientBindingModel model) { using (var context = new AbstractSweetShopDatabase()) { Ingredient element = context.Ingredients.FirstOrDefault(rec => rec.IngredientName == model.IngredientName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть элемент с таким названием"); } if (model.Id.HasValue) { element = context.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Ingredient(); context.Ingredients.Add(element); } element.IngredientName = model.IngredientName; context.SaveChanges(); } }
private IngredientDomModel ConvertToDomModel(Guid id, IngredientBindingModel model) { var result = new IngredientDomModel { Id = id, Name = model.Name, Quantity = model.Quantity }; return(result); }
public List <IngredientViewModel> Read(IngredientBindingModel model) { return(source.Ingredients .Where(rec => model == null || rec.Id == model.Id) .Select(rec => new IngredientViewModel { Id = rec.Id, IngredientName = rec.IngredientName }) .ToList()); }
public void Delete(IngredientBindingModel model) { for (int i = 0; i < source.Ingredients.Count; ++i) { if (source.Ingredients[i].Id == model.Id.Value) { source.Ingredients.RemoveAt(i); return; } } throw new Exception("Элемент не найден"); }
public void Delete(IngredientBindingModel model) { Ingredient element = source.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { source.Ingredients.Remove(element); } else { throw new Exception("Элемент не найден"); } }
public List <IngredientViewModel> Read(IngredientBindingModel model) { using (var context = new AbstractSweetShopDatabase()) { return(context.Ingredients .Where(rec => model == null || rec.Id == model.Id) .Select(rec => new IngredientViewModel { Id = rec.Id, IngredientName = rec.IngredientName }) .ToList()); } }
public void UpdElement(IngredientBindingModel model) { Ingredient element = source.Ingredients.FirstOrDefault(rec => rec.IngredientName == model.IngredientName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть ингредиент с таким названием"); } element = source.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } element.IngredientName = model.IngredientName; }
public void Delete(IngredientBindingModel model) { using (var context = new AbstractSweetShopDatabase()) { Ingredient element = context.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Ingredients.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } } }
public void AddElement(IngredientBindingModel model) { Ingredient element = source.Ingredients.FirstOrDefault(rec => rec.IngredientName == model.IngredientName); if (element != null) { throw new Exception("Уже есть ингредиент с таким названием"); } int maxId = source.Ingredients.Count > 0 ? source.Ingredients.Max(rec => rec.Id) : 0; source.Ingredients.Add(new Ingredient { Id = maxId + 1, IngredientName = model.IngredientName }); }
public ActionResult Edit(Guid id, IngredientBindingModel model) { if (!ModelState.IsValid) { return(RedirectToAction("Edit", "Ingredient", id)); } try { IngredientDomModel ingredient = this.ConvertToDomModel(id, model); this.manager.Edit(ingredient); return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError("Error", e.Message); return(View()); } }
public List <IngredientViewModel> Read(IngredientBindingModel model) { List <IngredientViewModel> result = new List <IngredientViewModel>(); foreach (var ingredient in source.Ingredients) { if (model != null) { if (ingredient.Id == model.Id) { result.Add(CreateViewModel(ingredient)); break; } continue; } result.Add(CreateViewModel(ingredient)); } return(result); }
public async Task <IActionResult> CreateIngredient(IngredientBindingModel ingredientModel) { if (!this.ingredientService.CheckIfIngredientTypeExistById(ingredientModel.IngredientTypeId)) { this.ModelState.AddModelError(string.Empty, ErrorMessageInvalidIngredientType); return(this.View()); } bool isIngredientFromDb = this.ingredientService.IngredientExistsByName(ingredientModel.Name); if (isIngredientFromDb) { this.ModelState.AddModelError(string.Empty, string.Format(ErrorMessageIngredientAlreadyExists, ingredientModel.Name)); return(this.View(ingredientModel)); } await this.ingredientService.CreateIngredient <IngredientBindingModel>(ingredientModel); this.TempData["StatusMessage"] = string.Format(SuccessMessageCreateIngredient, ingredientModel.Name); return(this.RedirectToAction("AllIngredients", "Ingredients")); }
public ActionResult Create(IngredientBindingModel model) //IFormCollection collection { if (!ModelState.IsValid) { return(View()); } try { var ingredientDomModel = new IngredientDomModel { Name = model.Name, Quantity = model.Quantity }; var result = this.manager.Add(ingredientDomModel); return(RedirectToAction(nameof(Index))); } catch (Exception e) { ModelState.AddModelError("Error", e.Message); return(View()); } }
private Ingredient CreateModel(IngredientBindingModel model, Ingredient ingredient) { ingredient.IngredientName = model.IngredientName; return(ingredient); }