Esempio n. 1
0
        public IActionResult AddMealAsFood(MealItemViewModel[] model)
        {
            var member = DiabetCalcService.EnsureMemberExists(_diabetWebData, User.Identity.Name);
            var meals  = _diabetWebData.GetMealItems(User.Identity.Name);
            var item   = new FoodItem();
            var tw     = (double)0;

            if ((meals != null) && (meals.Length > 0))
            {
                for (int i = 0; i < meals.Length; i++)
                {
                    tw += (double)meals[i].Weight;
                }
                if (tw > 0)
                {
                    for (int i = 0; i < meals.Length; i++)
                    {
                        item.Protein       += meals[i].FoodItem.Protein * (double)meals[i].Weight / tw;
                        item.Fat           += meals[i].FoodItem.Fat * (double)meals[i].Weight / tw;
                        item.Carbohydrates += meals[i].FoodItem.Carbohydrates * (double)meals[i].Weight / tw;
                    }
                }
                item.Protein       = Math.Round(item.Protein, 2);
                item.Fat           = Math.Round(item.Fat, 2);
                item.Carbohydrates = Math.Round(item.Carbohydrates, 2);
                DiabetCalcService.CalcEnergy(item);
            }
            return(RedirectToAction(nameof(AddFoodItem), item));
        }
Esempio n. 2
0
        public IActionResult FoodSelect(HomeSelectViewModel model, string submit)
        {
            if (ModelState.IsValid)
            {
                var pm = DiabetCalcService.EnsureMemberExists(_diabetWebData, User.Identity.Name);
                if ((model.SelectedItemIds == null) || (model.SelectedItemIds.Length == 0))
                {
                    return(RedirectToAction(nameof(FoodSelect)));
                }


                /// Clear Add
                /// Update and Calculate
                /// Set Favorites
                if ("Set Favorites".Equals(submit))
                {
                    //set favorites on the selected list
                    var fooditems    = _diabetWebData.GetSelectItems(model.SelectedItemIds.ToArray <int>());
                    var updateditems = fooditems.Select(fi => { fi.Favorites = model.SetFavorite; return(fi); }).ToArray <FoodItem>();
                    _diabetWebData.UpdateFoodItems(updateditems);
                    return(RedirectToAction(nameof(FoodSelect)));
                }
                else
                {
                    //if clear add - remove existing
                    if ("Remove/Add and Calc".Equals(submit))
                    {
                        var temp = _diabetWebData.GetMealItems(User.Identity.Name);
                        if ((temp != null) && (temp.Length > 0))
                        {
                            _diabetWebData.DeleteMealItems(temp);
                        }
                    }

                    // update meal by adding more items
                    var fooditems = _diabetWebData.GetSelectItems(model.SelectedItemIds.ToArray <int>());
                    var meals     = new List <MealItem>();
                    foreach (var fooditem in fooditems)
                    {
                        var mealitem = new MealItem {
                            MemberItem = pm, FoodItem = fooditem, Weight = 0, DosePart = 0
                        };
                        meals.Add(mealitem);
                    }
                    _diabetWebData.AddMealItems(meals.ToArray <MealItem>());
                    if ("Add and Stay".Equals(submit))
                    {
                        return(RedirectToAction(nameof(FoodSelect)));
                    }
                    else
                    {
                        return(RedirectToAction(nameof(DiabetCalc)));
                    }
                }
            }
            else
            {
                return(View());
            }
        }
Esempio n. 3
0
 public IActionResult Update2FoodItem(FoodItemEditModel model)
 {
     if (ModelState.IsValid)
     {
         var fi = _diabetWebData.GetFoodItem(model.Id);
         fi.Id            = model.Id;
         fi.Name          = model.Name;
         fi.Description   = model.Description;
         fi.Protein       = model.Protein;
         fi.Fat           = model.Fat;
         fi.Carbohydrates = model.Carbohydrates;
         fi.GlycemicIndex = model.GlycemicIndex;
         fi.Attribute     = model.Attribute;
         fi.Category      = model.Category;
         DiabetCalcService.CalcEnergy(fi);
         if (Request.Form.ContainsKey("update"))
         {
             fi = _diabetWebData.UpdateFoodItem(fi);
             return(RedirectToAction(nameof(FoodItemDetails), new { id = fi.Id }));
         }
         else if (Request.Form.ContainsKey("delete"))
         {
             _diabetWebData.DeleteFoodItem(fi);
             return(RedirectToAction(nameof(FoodItems)));
         }
         return(View());
     }
     else
     {
         return(View());
     }
 }
 public IActionResult OnPost(string submit)
 {
     if (ModelState.IsValid)
     {
         if (Request.Form.ContainsKey("update"))
         {
             var fi = _diabetWebData.GetFoodItem(FoodItem.Id);
             fi.Id            = FoodItem.Id;
             fi.Name          = FoodItem.Name;
             fi.Description   = FoodItem.Description;
             fi.Protein       = FoodItem.Protein;
             fi.Fat           = FoodItem.Fat;
             fi.Carbohydrates = FoodItem.Carbohydrates;
             fi.GlycemicIndex = FoodItem.GlycemicIndex;
             fi.Attribute     = FoodItem.Attribute;
             fi.Category      = FoodItem.Category;
             fi.Favorites     = FoodItem.Favorites;
             DiabetCalcService.CalcEnergy(fi);
             _diabetWebData.UpdateFoodItem(FoodItem);
             return(RedirectToAction("FoodItemDetails", "Home", new { id = FoodItem.Id }));
         }
         else if (Request.Form.ContainsKey("delete"))
         {
             _diabetWebData.DeleteFoodItem(FoodItem);
             return(RedirectToAction("FoodItems", "Home"));
         }
     }
     return(Page());
 }
Esempio n. 5
0
        public IActionResult DiabetCalc()
        {
            var member    = DiabetCalcService.EnsureMemberExists(_diabetWebData, User.Identity.Name);
            var mealitems = _diabetWebData.GetMealItems(User.Identity.Name);

            if ((mealitems == null) || (mealitems.Length == 0))
            {
                return(View());
            }
            var meals = new List <MealItemViewModel>();

            for (int i = 0; i < mealitems.Length; i++)
            {
                var mealitem = new MealItemViewModel {
                    Id = mealitems[i].Id, MemberItem = member, FoodItem = mealitems[i].FoodItem, Weight = mealitems[i].Weight, DosePart = mealitems[i].DosePart
                };
                meals.Add(mealitem);
            }
            return(View(meals.ToArray <MealItemViewModel>()));
        }
Esempio n. 6
0
 public IActionResult DiabetCalc(MealItemViewModel[] model)
 {
     if (ModelState.IsValid && (model != null) && (model.Length > 0))
     {
         var member = DiabetCalcService.EnsureMemberExists(_diabetWebData, User.Identity.Name);
         member.K1 = model[0].MemberItem.K1;
         member.K2 = model[0].MemberItem.K2;
         member.K3 = model[0].MemberItem.K3;
         member.F1 = model[0].MemberItem.F1;
         member.F2 = model[0].MemberItem.F2;
         member.F3 = model[0].MemberItem.F3;
         var meals = _diabetWebData.GetMealItems(User.Identity.Name);
         for (int i = 0; i < model.Length; i++)
         {
             meals[i].Weight = model[i].Weight;
         }
         DiabetCalcService.CalcDose(member, meals);
         _diabetWebData.UpdateMealItems(meals.ToArray <MealItem>());
         member = _diabetWebData.UpdateMemberItem(member);
         return(RedirectToAction(nameof(DiabetCalc)));
     }
     return(View(model));
 }
Esempio n. 7
0
        public IActionResult AddFoodItem(FoodItemEditModel model)
        {
            if (ModelState.IsValid)
            {
                var foodItem = new FoodItem();
                foodItem.Name          = model.Name;
                foodItem.Description   = model.Description;
                foodItem.Protein       = model.Protein;
                foodItem.Fat           = model.Fat;
                foodItem.Carbohydrates = model.Carbohydrates;
                foodItem.GlycemicIndex = model.GlycemicIndex;
                foodItem.Attribute     = model.Attribute;
                foodItem.Category      = model.Category;
                foodItem.Favorites     = model.Favorites;
                DiabetCalcService.CalcEnergy(foodItem);
                foodItem = _diabetWebData.AddFoodItem(foodItem);

                return(RedirectToAction(nameof(FoodItemDetails), new { id = foodItem.Id }));
            }
            else
            {
                return(View());
            }
        }