Beispiel #1
0
 public IEnumerable <IngredientAutocompleteItem> GetAllIngredients(ChooseMealReceiver data)
 {
     return(_appState.AllIngredients
            .Where(i => i.Name.StartsWith(data.Message, StringComparison.OrdinalIgnoreCase))
            .Select(i => new IngredientAutocompleteItem()
     {
         label = i.Name,
         value = i.IngredientId
     }));
 }
Beispiel #2
0
        public IActionResult ChooseMeal(ChooseMealReceiver data)
        {
            //parse input string
            var result = data.Message.Split("_");
            //open db!
            var db   = Database.GetDatabase(_env.ContentRootPath);
            var user = db.Users.FirstOrDefault();

            if (result.Length == 1)
            {
                //guid parse
                Guid mealGuid = Guid.Parse(result[0]);
                // fetch details from db
                // create new meal with new timestamp stc...
                // save to db!
                var hMeal = db.Users.FirstOrDefault()
                            .History
                            .OfType <DietHistory>()
                            .Single(m => m.Meal.MealId == mealGuid);
                var newMeal = new Meal()
                {
                    Name = hMeal.Meal.Name,
                    IngredientsAndWeights = new List <(Guid IngredientId, decimal weightInGrams)>
                                                (hMeal.Meal.IngredientsAndWeights),
                    MealId = Guid.NewGuid()
                };
                var newDH = new DietHistory()
                {
                    Meal          = newMeal,
                    EventDateTime = DateTime.Now,
                    UserId        = user.Id
                };
                db.Users[0].History.Add(newDH);
                Database.SaveDatabase(db, _env.ContentRootPath);
            }
            else
            {
                string         name            = result[0];
                List <Guid>    ingredientGuids = new List <Guid>();
                List <decimal> weights         = new List <decimal>();
                for (int i = 1; i < result.Length; i++)
                {
                    if ((i & 1) == 1)
                    {
                        ingredientGuids.Add(Guid.Parse(result[i]));
                    }
                    else
                    {
                        weights.Add(decimal.Parse(result[i]));
                    }
                }
                List <(Guid i, decimal w)> ps = new List <(Guid i, decimal w)>();
                for (int j = 0; j < ingredientGuids.Count; j++)
                {
                    ps.Add((ingredientGuids[j], weights[j]));
                }
                //use the ingredient weights and guids to construct a new meal
                // save to db
                var newMeal = new Meal()
                {
                    Name = name,
                    IngredientsAndWeights = new List <(Guid IngredientId, decimal weightInGrams)>(ps),
                    MealId = Guid.NewGuid()
                };
                var newDH = new DietHistory()
                {
                    Meal          = newMeal,
                    EventDateTime = DateTime.Now,
                    UserId        = user.Id
                };
                db.Users[0].History.Add(newDH);
                Database.SaveDatabase(db, _env.ContentRootPath);
            }

            return(Json(new { }));
        }