Пример #1
0
        //
        // Display Nutrient Values in a formattable way
        public List<NutrientValue> DisplayNutrientList(User user, NutrientAmount nutrientAmount)
        {
            var userNutrients = new HashSet<int>(user.Nutrients.Select(c => c.NutrientID));

            var viewModel = new List<NutrientValue>();

            Dictionary<int, decimal> groupInfo = new Dictionary<int, decimal>();

            Dictionary<int, decimal> nutrientAmounts = DictionaryUnSerializer(nutrientAmount.NutrientAmounts);

            foreach (var groupDatum in db.GroupData.Where(g => g.GroupID == user.GroupID))
            {
                groupInfo.Add(groupDatum.NutrientID, groupDatum.RDA);
            }

            decimal amount;
            decimal amountPercentage;

            foreach (var nutrient in db.Nutrients)
            {
                if (nutrientAmounts.ContainsKey(nutrient.NutrientID))
                {
                    var measurement = nutrient.Measurement;
                    if (nutrient.Type == "v") { measurement = "%"; }

                    amount = 0;
                    amountPercentage = 0;

                    if (nutrientAmounts.ContainsKey(nutrient.NutrientID))
                    {
                        if (nutrient.Type == "v")
                        {
                            amount = Math.Round(nutrientAmounts[nutrient.NutrientID] / groupInfo[nutrient.NutrientID] * 100);
                            amountPercentage = 0;
                        }
                        else
                        {
                            amount = nutrientAmounts[nutrient.NutrientID];
                            if (groupInfo[nutrient.NutrientID] != 0) { amountPercentage = Math.Round(nutrientAmounts[nutrient.NutrientID] / groupInfo[nutrient.NutrientID] * 100); }
                            else { amountPercentage = 0; }
                        }
                    }

                    viewModel.Add(new NutrientValue
                    {
                        NutrientID = nutrient.NutrientID,
                        Name = nutrient.Name,
                        Amount = amount,
                        Measurement = measurement,
                        AmountPercentage = amountPercentage
                    });
                }
            }

            return viewModel;
        }
Пример #2
0
        public ActionResult Create(User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    DateTime now = DateTime.Today;

                    // Set other default information for new user
                    user.PermissionsLevel = 2;
                    user.JoinDate = now;

                    // Calculate the User's Age
                    int age = now.Year - user.Birthday.Year;
                    if (user.Birthday > now.AddYears(-age)) age--;

                    // Groups for those under the age of 9 are gender nutral
                    // therefore we might have to adjust the matching parameters
                    char? sex = user.Sex;
                    if (age < 9) { sex = null; }

                    // Assign the user to the correct group
                    /*var q2 = db.Groups
                        .Include(i => i.GroupID)
                        .Where(i => i.BeginAge >= age && i.EndAge <= age && i.Sex == sex && i.SpecialOption == user.SpecialOption)
                        .Single();*/
                    user.GroupID = 7;

                    // Commits the new user
                    db.Users.Add(user);
                    db.SaveChanges();

                    // Initialize Nutrient Tracking for the user
                    InitializeUserTracking(user);
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {

            }

            return View(user);
        }
Пример #3
0
        private void PopulateTrackedNutrients(User user)
        {
            var userNutrients = new HashSet<int>(user.Nutrients.Select(c => c.NutrientID));

            var viewModel = new List<UserTracking>();

            foreach (var nutrient in db.Nutrients)
            {
                viewModel.Add(new UserTracking
                {
                    NutrientID = nutrient.NutrientID,
                    Name = nutrient.Name,
                    Tracked = userNutrients.Contains(nutrient.NutrientID)
                });
            }

            ViewBag.Nutrients = viewModel;
        }
Пример #4
0
        public void UpdateUserTracking(string[] selectedNutrients, User userToUpdate)
        {
            userToUpdate.Nutrients = new List<Nutrient>();

            // The user selected 0 nutrients. For the purposes of this demo -
            // That is dumb. Therefore we reset to defaults
            if (selectedNutrients == null)
            {
                InitializeUserTracking(userToUpdate);
                return;
            }

            var selectedNutrientsHS = new HashSet<string>(selectedNutrients);
            var userNutrients = new HashSet<int>(userToUpdate.Nutrients.Select(c => c.NutrientID));

            foreach (var nutrient in db.Nutrients)
            {
                if (selectedNutrientsHS.Contains(nutrient.NutrientID.ToString()))
                {
                    if (!userNutrients.Contains(nutrient.NutrientID))
                    {
                        userToUpdate.Nutrients.Add(nutrient);
                    }
                }
                else
                {
                    if (userNutrients.Contains(nutrient.NutrientID))
                    {
                        userToUpdate.Nutrients.Remove(nutrient);
                    }
                }
            }
        }
Пример #5
0
        // Set the User to the default
        public void InitializeUserTracking(User userToUpdate)
        {
            userToUpdate.Nutrients = new List<Nutrient>();

            foreach (var nutrient in db.Nutrients)
            {
                if (nutrient.DefaultTracked == true)
                {
                    userToUpdate.Nutrients.Add(nutrient);
                }
            }
        }
        //
        // Builds a list of all the user's predefined meals so they can quickly
        // select them and add them as a meal
        private SelectList PopulateMealsDropDownList(User user)
        {
            var mealsQuery = from d in db.Meals
                             orderby d.Name
                             select d.Name;

            return new SelectList(mealsQuery, "MealID", "Name");
        }
Пример #7
0
        public List<NutrientValue> EditNutrientList(User user, NutrientAmount nutrientAmount)
        {
            var userNutrients = new HashSet<int>(user.Nutrients.Select(c => c.NutrientID));

            var viewModel = new List<NutrientValue>();

            Dictionary<int, decimal> nutrientAmounts = DictionaryUnSerializer(nutrientAmount.NutrientAmounts);

            foreach (var nutrient in db.Nutrients)
            {
                if (userNutrients.Contains(nutrient.NutrientID))
                {
                    var measurement = nutrient.Measurement;
                    if (nutrient.Type == "v") { measurement = "%"; }

                    decimal amount = 0;
                    if (nutrientAmounts.ContainsKey(nutrient.NutrientID))
                    {
                        if (nutrient.Type == "v") { amount = nutrientAmounts[nutrient.NutrientID] / nutrient.PercentageMultiplier; }
                        else { amount = nutrientAmounts[nutrient.NutrientID]; }
                    }

                    viewModel.Add(new NutrientValue
                    {
                        NutrientID = nutrient.NutrientID,
                        Name = nutrient.Name,
                        Amount = amount,
                        Measurement = measurement
                    });
                }
            }

            return viewModel;
        }
Пример #8
0
        public List<NutrientValue> EditNutrientList(User user)
        {
            var userNutrients = new HashSet<int>(user.Nutrients.Select(c => c.NutrientID));

            var viewModel = new List<NutrientValue>();

            foreach (var nutrient in db.Nutrients)
            {
                if (userNutrients.Contains(nutrient.NutrientID))
                {
                    var measurement = nutrient.Measurement;
                    if (nutrient.Type == "v") { measurement = "%"; }

                    viewModel.Add(new NutrientValue
                    {
                        NutrientID = nutrient.NutrientID,
                        Name = nutrient.Name,
                        Measurement = measurement
                    });
                }
            }

            return viewModel;
        }