Пример #1
0
        //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);
            }
        }
Пример #2
0
        //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));
        }
Пример #3
0
        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));
        }
Пример #4
0
 /// <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);
     }
 }
Пример #5
0
        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)));
        }