Example #1
0
        public ActionResult Create(IngredientVM viewModel)
        {
            var userId = User.Identity.GetUserId();



            if (ModelState.IsValid)
            {
                var ingredient = db.Ingredients.SingleOrDefault(i => i.Title == viewModel.Title);
                if (ingredient == null)
                {
                    ingredient = new Ingredient
                    {
                        Title = viewModel.Title.ToLower(),
                    };
                }

                var fridgeIngredient = db.FridgeIngredients.SingleOrDefault(i => i.IngredientId == ingredient.Id && i.UserProfileId == userId && i.MeasurementId == viewModel.MeasurementId);
                if (fridgeIngredient == null)
                {
                    fridgeIngredient = new FridgeIngredient
                    {
                        Ingredient    = ingredient,
                        Amount        = viewModel.Amount,
                        MeasurementId = viewModel.MeasurementId,
                        UserProfileId = User.Identity.GetUserId(),
                    };

                    db.FridgeIngredients.Add(fridgeIngredient);
                }
                else
                {
                    fridgeIngredient.Amount         += viewModel.Amount;
                    db.Entry(fridgeIngredient).State = EntityState.Modified;
                }

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.IngredientId  = new SelectList(db.Ingredients, "Id", "Title");
            ViewBag.MeasurementId = new SelectList(db.Measurements, "Id", "unit", viewModel.MeasurementId);
            return(View(viewModel));
        }
Example #2
0
 public ActionResult Edit([Bind(Include = "Id,Title")] Ingredient ingredient)
 {
     if (ModelState.IsValid)
     {
         db.Entry(ingredient).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(ingredient));
 }
Example #3
0
 public ActionResult Edit([Bind(Include = "UserId,FirstName,LastName,EmailAddress")] UserProfile userProfile)
 {
     if (ModelState.IsValid)
     {
         db.Entry(userProfile).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(userProfile));
 }
 public ActionResult Edit([Bind(Include = "Id,UserProfileId,Amount,Name,Unit,RecipeTitle,RecipeUrl")] ListIngredient listIngredient)
 {
     if (ModelState.IsValid)
     {
         db.Entry(listIngredient).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.UserProfileId = new SelectList(db.UserProfiles, "UserId", "FirstName", listIngredient.UserProfileId);
     return(View(listIngredient));
 }