Пример #1
0
        private async void SaveData(string search, SmartLifeDbContext db, List <FoodUnit> result)
        {
            foreach (var item in result)
            {
                var foodNutritions = await GetFoodNutrition(item.Name);

                if (foodNutritions != null)
                {
                    item.FoodNutritionsItemID = foodNutritions.ID;
                }
            }
            var saveData = new SearchFood(search);

            saveData.FoodUnitList = result;
            db.SearchFood.AddOrUpdate(S => S.FoodName, saveData);
            try
            {
                await db.SaveChangesAsync();
            }
            catch (Exception)
            {
            }
        }
Пример #2
0
        public async Task <FoodNutritionsItem> GetFoodNutrition(string foodName)
        {
            try
            {
                var response = await Request(foodName, NutritionixReqType.getFoodsNutrients);

                if (response.TryGetValue("success", out object payload))
                {
                    var foodNutrition = GetFoodsNutrientsResultApi.FromJson(JsonConvert.SerializeObject(response));
                    var food          = ((foodNutrition.Success.First()).Foods.First());

                    (new Thread(() =>
                    {
                        var db = new SmartLifeDbContext();
                        try
                        {
                            db.FoodsNutritions.AddOrUpdate(food);
                            db.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            //Do nothing
                            if (System.Diagnostics.Debugger.IsAttached == false)
                            {
                                System.Diagnostics.Debugger.Launch();
                            }
                        }
                    })).Start();
                    return(food);
                }
            }
            catch (Exception)
            {
            }

            return(null);
        }
Пример #3
0
 public Login(IEventAggregator eventAggregator)
 {
     db = new SmartLifeDbContext();
     _eventAggregator = eventAggregator;
 }