示例#1
0
        public ActionResult SaveDiet(string food, float protein, float carb, float fat, float cal, string serving, string image, int pref_ID)
        {
            DateTime dateTime = DateTime.UtcNow.Date;

            try
            {
                string username = null;

                //check if user is logged in
                if (HttpContext.Session.GetString("LoggedIn") != null)
                {
                    //retrieve user details from respective database tables
                    username = HttpContext.Session.GetString("Username");

                    foreach (var foods in HttpContext.Session.GetCart())
                    {
                        using (var context = new DietPlanDBContext())
                        {
                            //var diet = new DietDb { Username = username, Date = dateTime, MealTime = foods.meal_time, Meals = foods.food };
                            //var nutrients = new NutritionalValue { Date = dateTime, FoodImg = foods.img, Protein = foods.nprotein, Carbs = foods.ncarb, Fat = foods.nfat, Calorie = foods.ncalorie };
                            //context.DietDb.Add(diet);
                            //context.NutritionalValue.Add(nutrients);
                            //context.SaveChanges();

                            var diet = new DietDb {
                                Username = username, Date = dateTime, MealTime = foods.meal_time, Meals = foods.food, PrefId = pref_ID
                            };

                            context.DietDb.Add(diet);
                            context.SaveChanges();

                            var nutrients = new NutritionalValue {
                                Food = foods.food, FoodId = diet.FoodId, Date = dateTime, FoodImg = foods.img, Protein = foods.nprotein, Carbs = foods.ncarb, Fat = foods.nfat, Calorie = foods.ncalorie, MealTime = foods.meal_time
                            };

                            context.NutritionalValue.Add(nutrients);
                            context.SaveChanges();
                        }
                    }
                }
                else
                {
                    return(RedirectToAction("Login"));
                }

                return(View());
            }
            catch (SqlException)
            {
                string action     = this.ControllerContext.RouteData.Values["action"].ToString();
                string controller = this.ControllerContext.RouteData.Values["controller"].ToString();
                return(RedirectToAction("Error", new { controllerName = controller, actionName = action }));
            }
        }
 public ManageAdminsController(DietPlanDBContext context)
 {
     try
     {
         _context = context;
     }
     catch (SqlException)
     {
         string action     = this.ControllerContext.RouteData.Values["action"].ToString();
         string controller = this.ControllerContext.RouteData.Values["controller"].ToString();
         RedirectToAction("Error", "Admin", new { controllerName = controller, actionName = action });
     }
 }
示例#3
0
 public UserController(DietPlanDBContext context)
 {
     db = context;
 }
 public AdminController(DietPlanDBContext context)
 {
     _context = context;
 }
示例#5
0
 public FoodDbsController(DietPlanDBContext context)
 {
     _context = context;
 }