Esempio n. 1
0
        private static void MatchItemsInDatabase(MfpDay temp)
        {
            foreach (FoodItemViewModel model in temp.All)
            {
                List <FoodItem> matches = db.FoodItems.Where(x => x.Name == model.FoodItem.Name && (string.IsNullOrEmpty(x.Brand) == string.IsNullOrEmpty(model.FoodItem.Brand) || x.Brand == model.FoodItem.Brand)).ToList();

                if (matches.Count == 1)
                {
                    var potentialMatch = matches.First();

                    if (!model.FoodItem.CompareMacroPercent(potentialMatch, 6, 6, 6))
                    {
                        throw new ArgumentException();
                        // Matched item macros do not match
                    }
                    model.FoodItemMatch = matches.First();

                    // not convinced

                    model.FoodItem.Category_Id     = model.FoodItemMatch.Category_Id;
                    model.FoodItem.FoodItemType_Id = model.FoodItemMatch.FoodItemType_Id;

                    if (model.FoodItemMatch.OriginalQuantity < model.FoodItem.Quantity)
                    {
                        model.FoodItemMatch.SetAsOriginal(model.FoodItem);
                        db.SaveChanges();
                    }
                }
                if (matches.Count > 1)
                {
                    throw new InvalidOperationException();
                    // duplicate food item
                }
            }
        }
Esempio n. 2
0
        public async System.Threading.Tasks.Task <ActionResult> Index(string selectedDate)
        {
            DateTime selectedDateIsGood = InterogateDate(selectedDate);
            MfpDay   mfpDay             = new MfpDay()
            {
                SelectedDate = selectedDateIsGood
            };

            await GetDate(selectedDateIsGood, mfpDay);

            //ThreadPool.QueueUserWorkItem(Worker, selectedDateIsGood);

            ViewBag.SelectedDate = selectedDateIsGood;
            TempData["MfpDay"]   = mfpDay;
            TempData.Keep();

            var  notMatchingList = mfpDay.All.Where(x => x.FoodItemMatch.Name == null).ToList();
            bool allMatching     = notMatchingList.Count() == 0;

            if (allMatching)
            {
                return(View("DayDisplay", mfpDay));
            }
            else
            {
            }

            return(View(mfpDay));
        }
Esempio n. 3
0
        private static async Task <MfpDay> GetDay(MfpDay mfpDay)
        {
            string searchLink = "https://www.myfitnesspal.com/food/diary/thephoenix25?date=";  // date=2020-02-14
            string date       = mfpDay.SelectedDate.ToString("yyyy-MM-dd");
            string website    = searchLink + date;

            return(await SiteParser.Parse(website, mfpDay));
        }
Esempio n. 4
0
        public ActionResult Update(int id)
        {
            MfpDay day = TempData["MfpDay"] as MfpDay;
            List <FoodItemViewModel> modelList = day.All;
            var model = modelList.FirstOrDefault(x => x.Id == id);

            TempData["Model"] = model;
            TempData.Keep();
            return(View("Update", model));
        }
Esempio n. 5
0
        public static async void Worker(object state)
        {
            DateTime      d  = (DateTime)state;
            List <MfpDay> fj = new List <MfpDay>();

            for (int i = 0; i < 7; i++)
            {
                MfpDay day = new MfpDay();
                await GetDate(d, day);

                d = d.AddDays(-1);
                fj.Add(day);
            }
            StoreDay(fj);
        }
Esempio n. 6
0
        public async static Task <MfpDay> GetDate(DateTime date, MfpDay mfpDay)
        {
            mfpDay.SelectedDate = date;
            await GetDay(mfpDay);

            mfpDay.SelectedDate = date;
            MatchItemsInDatabase(mfpDay);
            for (int i = 0; i < mfpDay.All.Count(); i++)
            {
                mfpDay.All[i].Id = i;
            }

            mfpDay.All = mfpDay.All.OrderBy(x => x.FoodItemMatch.Name != null).ToList();

            //Console.Beep();
            return(mfpDay);
        }
Esempio n. 7
0
        public static async Task <MfpDay> Parse(string website, MfpDay day)
        {
            HttpClient   http     = new HttpClient();
            HtmlDocument resultat = new HtmlDocument();
            String       source;

            try
            {
                var response = await http.GetByteArrayAsync(website);

                source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
                source = WebUtility.HtmlDecode(source);
                resultat.LoadHtml(source);
            }
            catch
            {
                string          date = day.SelectedDate.ToString("yyyy-MM-dd");
                MyFitnessPalDay mfpd = db.MyFitnessPalDays.FirstOrDefault(x => x.DateOfPage.ToString("yyyy-MM-dd") == date);
                if (mfpd == null)
                {
                    throw new HttpException();
                }
                source = mfpd.Html;
            }

            day.Htlm = source;


            List <HtmlNode> toftitle = resultat.DocumentNode.Descendants().Where
                                           (x => (x.Name == "td" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("first alt"))).ToList();

            Meal meal = new Meal();


            List <HtmlNode> listOfRows = resultat.DocumentNode.Descendants().Where(x => (x.Name == "tr")).ToList();

            foreach (HtmlNode node in listOfRows)
            {
                string line         = node.InnerText;
                bool   isHeaderLine = false;
                if (line.Trim() == string.Empty)
                {
                    break;
                }
                if (line.Contains("AM - 12"))
                {
                    meal         = Meal.Meal1;
                    isHeaderLine = true;
                }
                if (line.Contains("12 - 13"))
                {
                    meal         = Meal.Meal2;
                    isHeaderLine = true;
                }
                if (line.Contains("13 - 14"))
                {
                    meal         = Meal.Meal3;
                    isHeaderLine = true;
                }
                if (line.Contains("14 - 17"))
                {
                    meal         = Meal.Meal4;
                    isHeaderLine = true;
                }
                if (line.Contains("17 - 20"))
                {
                    meal         = Meal.Meal5;
                    isHeaderLine = true;
                }
                if (line.Contains("20 - PM"))
                {
                    meal         = Meal.Meal6;
                    isHeaderLine = true;
                }

                if (!isHeaderLine)
                {
                    FoodItemViewModel fivm = ParseLine(line);
                    fivm.MealId = (int)meal;
                    day.All.Add(fivm);
                    switch (meal)
                    {
                    case Meal.Meal1:
                        day.meal1.Add(fivm);
                        break;

                    case Meal.Meal2:
                        day.meal2.Add(fivm);
                        break;

                    case Meal.Meal3:
                        day.meal3.Add(fivm);
                        break;

                    case Meal.Meal4:
                        day.meal4.Add(fivm);
                        break;

                    case Meal.Meal5:
                        day.meal5.Add(fivm);
                        break;

                    case Meal.Meal6:
                        day.meal6.Add(fivm);
                        break;

                    default:
                        break;
                    }
                }
            }
            return(day);
        }