示例#1
0
        public ActionResult Add(int id)
        {
            //find item by id
            CalorieItem ci = db.CalorieItems.Find(id);
            //find its category
            var cat = db.Categories.Find(ci.CategoryId);

            //set its category
            ci.Category = cat;
            //find user id
            var userId = User.Identity.GetUserId();
            //create a list of that users daily calorie counts
            //this is only seen if a member is logged in
            IList <DailyCalorieCount> list = db.DailyCalorieCounts.Where(d => d.UserId == userId).OrderByDescending(d => d.Date).ToList();

            //view model
            var addItem = new ItemIntakeViewModel()
            {
                CalorieItemId = ci.CalorieItemId, // to send to post
                CalorieItem   = ci,               //to grab info from view
                Quantity      = ci.ServingSize,
                // a list of counts are individually created into selectListItems
                // with value and text set
                // this was to set the format of the date how i wanted!
                DailyCalorieCountId = list.Select(s => new SelectListItem
                {
                    Value = s.DailyCalorieCountId.ToString(),
                    Text  = (s.Name + ": " + s.Date.ToString("ddd - dd/MM"))
                }).ToList(),
                ImagePath = ci.ImagePath
            };

            return(View(addItem));
        }
示例#2
0
        public ActionResult CreateCalorieItem([Bind(Include = "Name,Calories,Carbs,Protein,Fat,ServingSize,CategoryId,ImagePath")] CalorieItem calorieItem)
        {
            if (ModelState.IsValid)
            {
                db.CalorieItems.Add(calorieItem);
                db.SaveChanges();

                if (!String.IsNullOrEmpty(calorieItem.ImagePath))
                {
                    HttpPostedFileBase ImagePath = Request.Files["ImagePath"];
                    CalorieItemImage(calorieItem.CalorieItemId, ImagePath);
                }
                else
                {
                    calorieItem.ImagePath = @"/Content/Images/CalorieItemImages/dog_ate_img.jpeg";
                    db.SaveChanges();
                }


                return(RedirectToAction("AllCalorieItems"));
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name");

            return(View(calorieItem));
        }
示例#3
0
        public ActionResult EditCalorieItem([Bind(Include = "CalorieItemId,Name,Calories,Carbs,Protein,Fat,ServingSize,CategoryId,ImagePath")] CalorieItem item)
        {
            if (ModelState.IsValid)
            {
                var    temp             = db.CalorieItems.Find(item.CalorieItemId);
                string currentImagePath = "";
                if (!String.IsNullOrEmpty(temp.ImagePath) && String.IsNullOrEmpty(item.ImagePath))
                {
                    currentImagePath = temp.ImagePath;
                }

                db.Entry(temp).State = EntityState.Detached;
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();

                if (!String.IsNullOrEmpty(item.ImagePath))
                {
                    HttpPostedFileBase ImagePath = Request.Files["ImagePath"];

                    CalorieItemImage(item.CalorieItemId, ImagePath);
                }
                else
                {
                    item.ImagePath = currentImagePath;
                    db.SaveChanges();
                }

                return(RedirectToAction("AllCalorieItems"));
            }
            IList <Category> cats = db.Categories.ToList();

            IEnumerable <SelectListItem> catList = cats.Select(s => new SelectListItem
            {
                Value = s.CategoryId.ToString(),
                Text  = s.Name
            }).ToList();

            foreach (SelectListItem s in catList)
            {
                if (s.Value == item.CategoryId.ToString())
                {
                    s.Selected = true;
                }
            }

            ViewBag.CategoryId = catList;
            return(View(item));
        }
示例#4
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));
        }