Ejemplo n.º 1
0
        public void GetIntakeList()
        {
            //Get Network Data
            DownloadData download       = new DownloadData();
            string       intakeListData = download.IntakeList();

            DataParsing dataParsing = new DataParsing();

            _intakeList = dataParsing.ParseIntakeList(intakeListData);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnGetAsync(int id, string date)
        {
            //don't let user see pages in the future
            if (Date > DateTime.Now.Date)
            {
                Response.Redirect("./Dashboard");
            }

            ApplicationUser applicationUser = await _userManager.GetUserAsync(User);

            string activeUserId = HttpContext.Session.GetString("activeUserId");

            if (String.IsNullOrEmpty(activeUserId))
            {
                HttpContext.Session.SetString("activeUserId", applicationUser.Id);
                HttpContext.Session.SetString("fullName", applicationUser.FullName);
            }
            else
            {
                applicationUser = await _userManager.FindByIdAsync(activeUserId);
            }

            //Set isTrainer in session
            if (_context.Users.Where(u => u.TrainerId == applicationUser.Id).Count() > 0 && HttpContext.Session.GetString("isTrainer") != "true")
            {
                HttpContext.Session.SetString("isTrainer", "true");
            }

            //ApplicationUser applicationUser = await _userManager.FindByIdAsync(_userManager.GetUserAsync(User).Result.ActiveUserId);

            userFullName = applicationUser.FullName;
            UserIdvar    = applicationUser.Id;

            //get all intake for today, grouping by meal type
            List <Intake> temp1 = await _context.Intakes.Include(s => s.Food).Include(s => s.CustomFood).Where(s => s.User == applicationUser && s.Date.Date == Date.Date).Where(s => s.MealType == "Breakfast" || s.MealType == "Lunch").OrderBy(s => s.MealType).ToListAsync();

            List <Intake> temp2 = await _context.Intakes.Include(s => s.Food).Include(s => s.CustomFood).Where(s => s.User == applicationUser && s.Date.Date == Date.Date).Where(s => s.MealType == "Dinner" || s.MealType == "Snack").OrderBy(s => s.MealType).ToListAsync();

            IntakeList = temp1.Concat(temp2).ToList();

            if (applicationUser.GoalUnit == "Calorie")
            {
                SelectedGoalUnit = "Calories";
                AbrevGoalUnit    = "kCal";

                foreach (var item in IntakeList)
                {
                    if (item.FoodId != null)
                    {
                        item.Food.Enegry = applicationUser.ConvertToCalorie((int)item.Food.Enegry);
                    }
                    else if (item.CustomFoodId != null)
                    {
                        item.CustomFood.Enegry = applicationUser.ConvertToCalorie((int)item.CustomFood.Enegry);
                    }
                }
            }
            else
            {
                SelectedGoalUnit = "KiloJoules";
                AbrevGoalUnit    = "KJ";
            }


            List <int> numbers = new List <int>(Array.ConvertAll(applicationUser.Macros.Split(','), int.Parse));

            //get goals for progress bars
            GoalName1 = getGoalName(numbers[0]);
            GoalName2 = getGoalName(numbers[1]);
            GoalName3 = getGoalName(numbers[2]);

            //get goals for progress bars
            Goal1 = getGoalValue(applicationUser, numbers[0]);
            Goal2 = getGoalValue(applicationUser, numbers[1]);
            Goal3 = getGoalValue(applicationUser, numbers[2]);

            //calculate macro totals
            var TotalFoodGoal1 = IntakeList.Where(i => i.FoodId != null).Select(i => i.CalculateValueToFloat(getFoodValue(i.Food, numbers[0]), i.Food.Amount)).Aggregate(0F, (a, b) => a + b);
            var TotalFoodGoal2 = IntakeList.Where(i => i.FoodId != null).Select(i => i.CalculateValueToFloat(getFoodValue(i.Food, numbers[1]), i.Food.Amount)).Aggregate(0F, (a, b) => a + b);
            var TotalFoodGoal3 = IntakeList.Where(i => i.FoodId != null).Select(i => i.CalculateValueToFloat(getFoodValue(i.Food, numbers[2]), i.Food.Amount)).Aggregate(0F, (a, b) => a + b);

            var TotalCustomFoodGoal1 = IntakeList.Where(i => i.CustomFoodId != null).Select(i => i.CalculateValueToFloat(getCustomFoodValue(i.CustomFood, numbers[0]), i.CustomFood.Amount)).Aggregate(0F, (a, b) => a + b);
            var TotalCustomFoodGoal2 = IntakeList.Where(i => i.CustomFoodId != null).Select(i => i.CalculateValueToFloat(getCustomFoodValue(i.CustomFood, numbers[1]), i.CustomFood.Amount)).Aggregate(0F, (a, b) => a + b);
            var TotalCustomFoodGoal3 = IntakeList.Where(i => i.CustomFoodId != null).Select(i => i.CalculateValueToFloat(getCustomFoodValue(i.CustomFood, numbers[2]), i.CustomFood.Amount)).Aggregate(0F, (a, b) => a + b);


            TotalGoal1 = TotalFoodGoal1 + TotalCustomFoodGoal1;
            TotalGoal2 = TotalFoodGoal2 + TotalCustomFoodGoal2;
            TotalGoal3 = TotalFoodGoal3 + TotalCustomFoodGoal3;

            //Set date if provided, otherwise redirect to current date
            try
            {
                NewDate = DateTime.ParseExact(date, "yyyy-MM-dd", null);

                if (NewDate > DateTime.Now.Date)
                {
                    //trying to access day in the future
                    return(Redirect("./Dashboard?date=" + DateTime.Now.ToString("yyyy-MM-dd")));
                }
            }
            catch
            {
                //unexpected input for date
                return(Redirect("./Dashboard?date=" + DateTime.Now.ToString("yyyy-MM-dd")));
            }

            //Get Intake to be edited
            Intake = await _context.Intakes.Include(i => i.Food).Include(i => i.CustomFood).FirstOrDefaultAsync(m => m.Id == id);

            //Set flag to indicate new or existing intake based on response.
            NewIntake = (id == 0) ? true : false;

            //Declare user variable to be used for intake
            //Get active User from session
            //If not set, set session value of activeUserId to current user
            if (String.IsNullOrEmpty(activeUserId))
            {
                applicationUser = await _userManager.GetUserAsync(User);

                HttpContext.Session.SetString("activeUserId", applicationUser.Id);
                HttpContext.Session.SetString("fullName", applicationUser.FullName);
            }
            else
            {
                //Else get user to do intake for.
                applicationUser = await _userManager.FindByIdAsync(activeUserId);
            }
            //Get user fullname to display on page
            userFullName = applicationUser.FullName;


            return(Page());
        }