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)); }
public ActionResult Add([Bind(Include = "CalorieItemId, Quantity, SelectedCalorieCount")] ItemIntakeViewModel ii) { //if the required items have assigned values! if (ModelState.IsValid) { //new insstance of a calorie count DailyCalorieCount dcc = new DailyCalorieCount(); //if there has been no existing calorie count selected if (ii.SelectedCalorieCount == null || ii.SelectedCalorieCount == 0) { // FUTURE :if not logged in save a session count? //create a new daily count based on today dcc = new DailyCalorieCount() { Date = DateTime.Now, Name = "New Count", UserId = User.Identity.GetUserId(), TotalCalories = 0 }; db.DailyCalorieCounts.Add(dcc); db.SaveChanges(); } else //otherwise if the user selected an existing daily calorie count save set instance to that { dcc = db.DailyCalorieCounts.Find(ii.SelectedCalorieCount); } //after deciding what daily count to add the item to //add the item to it based on the view model db.CalorieItemIntakes.Add(new CalorieItemIntake() { CalorieItemId = ii.CalorieItemId, CalorieItem = db.CalorieItems.Find(ii.CalorieItemId), Quantity = ii.Quantity, DailyCalorieCount = dcc, DailyCalorieCountId = dcc.DailyCalorieCountId }); //add the amount to the calories //based on the base calorie amount * (qnty eaten / base serving size) dcc.TotalCalories += Calculator.Sum(db.CalorieItems.Find(ii.CalorieItemId).Calories, ii.Quantity, db.CalorieItems.Find(ii.CalorieItemId).ServingSize); //FUTURE ADD OTHER TOTALS in the same way //add totals of other attributes //dcc.TotalCarbs += Calculator.Sum() //dcc.TotalProtien += Calculator.Sum() //dcc.TotalFat += Calculator.Sum() db.SaveChanges(); return(RedirectToAction("FoodIndex")); //update days total in nav bar? } // get user id var userId = User.Identity.GetUserId(); //get calorie item and send back to view if viewModel is not valid ii.CalorieItem = db.CalorieItems.Find(ii.CalorieItemId); //imagepath ii.ImagePath = db.CalorieItems.Find(ii.CalorieItemId).ImagePath; // re send the daily counts to view ii.DailyCalorieCountId = db.DailyCalorieCounts.Where(c => c.UserId == userId).ToList().Select(s => new SelectListItem() { Value = s.DailyCalorieCountId.ToString(), Text = s.DateToString() }).ToList(); return(View(ii)); }