Example #1
0
        public ActionResult ReviewRequest(int?id)
        {
            if (id == null)
            {
                ViewBag.ReqMessage = "Request Error: ID does not exist!";
                return(RedirectToAction("AllRequests"));
            }

            var item = db.TempCalorieItems.Find(id);

            var vm = new ReviewTempCalorieItemViewModel()
            {
                TempCalorieItemId = item.TempCalorieItemId,
                Name        = item.Name,
                Calories    = item.Calories,
                Carbs       = item.Carbs,
                Protein     = item.Protein,
                Fat         = item.Fat,
                ServingSize = item.ServingSize
            };


            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name");

            return(View(vm));
        }
Example #2
0
        public ActionResult ReviewRequest([Bind(Include = "Name, Calories, Carbs, Protein, Fat, ServingSize,ImagePath, CategoryId")] ReviewTempCalorieItemViewModel calItem, int TempCalorieItemId)
        {
            if (ModelState.IsValid)
            {
                var item = new CalorieItem()
                {
                    Name        = calItem.Name,
                    Calories    = calItem.Calories,
                    Carbs       = calItem.Carbs,
                    Protein     = calItem.Protein,
                    Fat         = calItem.Fat,
                    ServingSize = calItem.ServingSize,
                    Category    = db.Categories.Find(calItem.CategoryId)
                };
                //add the new calorie Item
                db.CalorieItems.Add(item);
                db.SaveChanges();
                //add Image
                if (!String.IsNullOrEmpty(calItem.ImagePath))
                {
                    HttpPostedFileBase ImagePath = Request.Files["ImagePath"];
                    CalorieItemImage(item.CalorieItemId, ImagePath);
                }
                else
                {
                    item.ImagePath = @"/Content/Images/CalorieItemImages/dog_ate_img.jpeg";
                    db.SaveChanges();
                }

                //REMOVING TEMP AND ADDING NEW CALORIE ITEM INSTEAD
                //find calorie count of temp calorie item
                var temp  = db.TempCalorieItems.Find(TempCalorieItemId);
                var count = db.DailyCalorieCounts.Find(temp.DailyCalorieCountId);
                //remove temp and add the new, accpeted, calorie item
                //create intake item
                var calorieItemIntake = new CalorieItemIntake()
                {
                    CalorieItem       = item,
                    DailyCalorieCount = count,
                    Quantity          = temp.ServingSize
                };
                //add intake item, as to give it a ID
                db.CalorieItemIntakes.Add(calorieItemIntake);

                //// DO THE MATH IF : calorie values/ratio were to change the count would also to reflect this update, otherwise the total stays the same
                //minus the temp calories from total calories
                //count.TotalCalories -= temp.Calories;
                //add the new calorie
                //count.TotalCalories += Calculator.Sum(db.CalorieItems.Find(calorieItemIntake.CalorieItemId).Calories, calorieItemIntake.Quantity, db.CalorieItems.Find(calorieItemIntake.CalorieItemId).ServingSize);
                //db.Entry(count).State = EntityState.Modified;

                db.TempCalorieItems.Remove(temp);
                db.SaveChanges();

                return(RedirectToAction("AllRequests"));
            }


            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name");

            return(View(calItem));
        }