//Update meal public bool UpdateMeal(MealUpdate model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Meals .SingleOrDefault(m => m.MealId == model.MealId && m.OwnerId == _userId); entity.Title = model.Title; return(ctx.SaveChanges() == 1); } }
//GET : Meal/Edit public ActionResult Edit(int id) { var service = CreateMealService(); var detail = service.GetMealById(id); var model = new MealUpdate() { MealId = detail.MealId, Title = detail.Title }; return(View(model)); }
public ActionResult Edit(int id, MealUpdate model) { if (!ModelState.IsValid) { return(View(model)); } var service = CreateMealService(); if (service.UpdateMeal(model)) { TempData["SaveResult"] = "Meal was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Unable to update meal."); return(View(model)); }
/// <summary> /// Update a meal model with a meal update DTO. /// </summary> /// <param name="meal">The meal to update.</param> /// <param name="mealUpdate">The meal update DTO.</param> public void ToModel(ref Domain.Meal meal, MealUpdate mealUpdate) { if (null != mealUpdate.Name) { meal.ChangeName(mealUpdate.Name); } if (null != mealUpdate.Instructions) { meal.ChangeInstructions(mealUpdate.Instructions); } if (null != mealUpdate.MealIngredients) { var ingredients = ToModel(mealUpdate.MealIngredients); meal.SetIngredients(ingredients); } if (default(int) != mealUpdate.FeedsNumPeople) { meal.ChangeServingSize(mealUpdate.FeedsNumPeople); } }
public ActionResult <Meal> Patch(int id, [FromBody] MealUpdate mealUpdate) { if (!_mealService.TryGetMeal(id, out Domain.Meal meal)) { return(NotFound($"Meal with id \"{id}\" does not exist.")); } try { _dtoConverter.ToModel(ref meal, mealUpdate); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } var updatedMeal = _mealService.SaveMeal(meal); return(Ok(_dtoConverter.ToDto(updatedMeal))); }