Exemplo n.º 1
0
        public async Task <Meal> AddMeal(AddMealBindingModel model, string username)
        {
            var user = this.context.Users.FirstOrDefault(u => u.UserName == username);

            if (user == null)
            {
                return(null);
            }

            Location location = null;

            if (!string.IsNullOrEmpty(model.Location))
            {
                location = this.HandleLocation(model.Location, user.Id);
            }

            var mealFoodItems = new List <MealFoodItem>();
            var mealRecipes   = new List <MealRecipe>();

            if (model.FoodItems != null)
            {
                foreach (var foodItemBindingModel in model.FoodItems)
                {
                    var foodItem = this.context.FoodItems.FirstOrDefault(fi => fi.Name == foodItemBindingModel.Name);
                    if (foodItem != null && foodItemBindingModel.AmountInGrams > 0)
                    {
                        var mealFoodItem = new MealFoodItem
                        {
                            FoodItem      = foodItem,
                            AmountInGrams = foodItemBindingModel.AmountInGrams
                        };
                        mealFoodItems.Add(mealFoodItem);
                    }
                }
            }

            if (model.Recipes != null)
            {
                foreach (var recipeBindingModel in model.Recipes)
                {
                    var recipe = this.context.Recipes.FirstOrDefault(r => r.Name == recipeBindingModel.Name);
                    if (recipe != null && recipeBindingModel.AmountInGrams > 0)
                    {
                        var mealRecipe = new MealRecipe
                        {
                            Recipe        = recipe,
                            AmountInGrams = recipeBindingModel.AmountInGrams
                        };
                        mealRecipes.Add(mealRecipe);
                    }
                }
            }

            var meal = new Meal
            {
                FoodyUser         = user,
                Location          = location,
                MealFoodItems     = mealFoodItems,
                MealRecipes       = mealRecipes,
                Note              = model.Note,
                TimeOfConsumption = model.TimeOfConsumption
            };



            this.context.Meals.Add(meal);

            this.context.SaveChanges();

            await this.SetMealCaloriesAsync(meal.Id);

            return(meal);
        }
Exemplo n.º 2
0
        public async Task <Meal> EditMeal(EditMealViewModel model)
        {
            var meal = this.context.Meals.Include(m => m.Location).Include(m => m.FoodyUser).Include(m => m.MealFoodItems).ThenInclude(mfi => mfi.FoodItem)
                       .Include(m => m.MealRecipes).ThenInclude(mr => mr.Recipe)
                       .FirstOrDefault(m => m.Id == model.Id);

            if (meal == null)
            {
                return(null);
            }

            var caloriesChange = false;

            if (model.FoodItems != null)
            {
                foreach (var foodItemViewModel in model.FoodItems)
                {
                    var foodItem = this.context.FoodItems.FirstOrDefault(fi => fi.Name == foodItemViewModel.Name);

                    if (foodItem == null)
                    {
                        continue;
                    }

                    var existingMealFoodItem = meal.MealFoodItems.FirstOrDefault(mfi => mfi.FoodItem.Name == foodItemViewModel.Name);

                    if (foodItemViewModel.AmountInGrams > 0)
                    {
                        if (existingMealFoodItem == null)
                        {
                            var newMealFoodItem = new MealFoodItem
                            {
                                Meal          = meal,
                                FoodItem      = foodItem,
                                AmountInGrams = foodItemViewModel.AmountInGrams
                            };
                            meal.MealFoodItems.Add(newMealFoodItem);
                            caloriesChange = true;
                        }
                        else
                        {
                            if (!existingMealFoodItem.AmountInGrams.Equals(foodItemViewModel.AmountInGrams))
                            {
                                existingMealFoodItem.AmountInGrams = foodItemViewModel.AmountInGrams;
                                caloriesChange = true;
                            }
                        }
                    }
                    else
                    {
                        if (existingMealFoodItem != null)
                        {
                            meal.MealFoodItems.Remove(existingMealFoodItem);
                            caloriesChange = true;
                        }
                    }
                }
            }

            if (model.Recipes != null)
            {
                foreach (var recipeViewModel in model.Recipes)
                {
                    var recipe = this.context.Recipes.FirstOrDefault(r => r.Name == recipeViewModel.Name);

                    if (recipe == null)
                    {
                        continue;
                    }

                    var existingMealRecipe = meal.MealRecipes.FirstOrDefault(mr => mr.Recipe.Name == recipeViewModel.Name);

                    if (recipeViewModel.AmountInGrams > 0)
                    {
                        if (existingMealRecipe == null)
                        {
                            var newMealRecipe = new MealRecipe
                            {
                                Meal          = meal,
                                Recipe        = recipe,
                                AmountInGrams = recipeViewModel.AmountInGrams
                            };
                            meal.MealRecipes.Add(newMealRecipe);
                            caloriesChange = true;
                        }
                        else
                        {
                            if (!existingMealRecipe.AmountInGrams.Equals(recipeViewModel.AmountInGrams))
                            {
                                existingMealRecipe.AmountInGrams = recipeViewModel.AmountInGrams;
                                caloriesChange = true;
                            }
                        }
                    }
                    else
                    {
                        if (existingMealRecipe != null)
                        {
                            meal.MealRecipes.Remove(existingMealRecipe);
                            caloriesChange = true;
                        }
                    }
                }
            }

            meal.Note = model.Note;

            if (meal.Location == null || meal.Location.Name != model.Location)
            {
                meal.Location = this.HandleLocation(model.Location, meal.FoodyUserId);
            }

            meal.TimeOfConsumption = model.TimeOfConsumption;

            context.SaveChanges();

            if (caloriesChange)
            {
                Thread.Sleep(500);
                await this.SetMealCaloriesAsync(meal.Id);
            }

            return(meal);
        }