예제 #1
0
        public bool Update(HUTModels.Food model)
        {
            try
            {
                Food food = new Food()
                {
                    CaloriesPer100Grams = model.CaloriesPer100Grams, Description = model.Description, FoodId = model.FoodId
                };

                // checking separately so we don't accidentally delete the URL while doing some testing
                if (model.PhotoURL != null)
                {
                    food.PhotoURL = model.PhotoURL;
                }

                repo.Update(food);
                repo.Save();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #2
0
        public HUTModels.Food RetrieveAndUpdatePhotoURL(HUTModels.Food model)
        {
            HUTModels.Food food = GetFoodFromService(model.Description).Result;

            // if a photo url is found, update our database with the value
            if (!string.IsNullOrEmpty(food.PhotoURL))
            {
                model.PhotoURL = food.PhotoURL;
                Update(model);
            }

            return(model);
        }
예제 #3
0
        public IHttpActionResult Put(HUTModels.Food model)
        {
            FoodBLL bll = new FoodBLL();

            if (bll.Update(model))
            {
                return(Ok());
            }
            else
            {
                return(Content(HttpStatusCode.InternalServerError, "Problem updating food record."));
            }
        }
예제 #4
0
        public IHttpActionResult RetrieveAndUpdatePhotoURL(HUTModels.Food model)
        {
            FoodBLL bll  = new FoodBLL();
            var     food = bll.RetrieveAndUpdatePhotoURL(model);

            if (food == null)
            {
                return(Content(HttpStatusCode.NotFound, "Food or Photo URL not found in external resource."));
            }
            else
            {
                return(Ok(food));
            }
        }
예제 #5
0
        public List <HUTModels.Food> GetUnlistedFood(string foodName)
        {
            // need webservice call to get new food
            HUTModels.Food food = GetFoodFromService(foodName).Result;

            // need to insert record into database
            if (Insert(food))
            {
                // need to return new food with id
                List <HUTModels.Food> foods = GetFood(foodName);

                return(foods);
            }

            return(null);
        }
예제 #6
0
        public bool Insert(HUTModels.Food model)
        {
            try
            {
                Food food = new Food()
                {
                    CaloriesPer100Grams = model.CaloriesPer100Grams, Description = model.Description, PhotoURL = model.PhotoURL
                };

                repo.Create(food);
                repo.Save();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #7
0
        private async Task <HUTModels.Food> GetFoodFromService(string foodName)
        {
            Uri uri = new Uri("https://trackapi.nutritionix.com/v2/natural/nutrients");

            string json = await HTTPHelper.PostValues(uri, SetQueryModel(foodName), SetCustomHeaders()).ConfigureAwait(false);

            JObject jsonObject = JObject.Parse(json);

            JArray foods    = (JArray)jsonObject["foods"];
            int    calories = Convert.ToInt32(foods[0]["nf_calories"]);
            string highres  = (string)foods[0]["photo"]["highres"];

            HUTModels.Food food = new HUTModels.Food();
            food.CaloriesPer100Grams = Convert.ToInt32(calories);
            food.Description         = foodName;
            food.PhotoURL            = highres;

            return(food);
        }