Ejemplo n.º 1
0
        public async Task <int> AddMealToDietAsync(MealWithDiet meal)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                bool exists = ctx.Meal.Any(m => m.Id == meal.Id);
                int  mealId;
                if (exists)
                {
                    mealId = meal.Id;
                }
                else
                {
                    await ctx.Meal.AddAsync(new Meal
                    {
                        Title       = meal.Title,
                        Description = meal.Description,
                        Calories    = meal.Calories
                    });

                    await ctx.SaveChangesAsync();

                    mealId = ctx.Meal.ToList().Last().Id;
                }

                await ctx.DietMeals.AddAsync(new DietMeal
                {
                    MealId = mealId,
                    DietId = meal.DietId
                });

                await ctx.SaveChangesAsync();

                return(mealId);
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> EditMealInDiet(MealWithDiet meal)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                int  mealFrequency = GetNumberOfDietsForMeal(meal.Id);
                Meal mealDb        = await ctx.Meal.FirstAsync(m => m.Id == meal.Id);

                if (mealFrequency > 1)
                {
                    await ctx.Meal.AddAsync(new Meal
                    {
                        Calories    = mealDb.Calories,
                        Description = mealDb.Description,
                        Title       = mealDb.Title
                    });

                    await ctx.SaveChangesAsync();

                    int mealId = ctx.Meal.ToList().Last().Id;
                    Console.WriteLine("New meal id is " + mealId);
                    var dietMeals = ctx.DietMeals.Where(dm => dm.MealId == meal.Id &&
                                                        dm.Diet.Id != meal.DietId).ToList();
                    foreach (var dietMeal in dietMeals)
                    {
                        ctx.DietMeals.Remove(dietMeal);
                        await ctx.DietMeals.AddAsync(new DietMeal
                        {
                            DietId = dietMeal.DietId,
                            MealId = mealId
                        });
                    }
                }

                if (meal.Calories >= 0)
                {
                    mealDb.Calories = meal.Calories;
                }
                if (!string.IsNullOrEmpty(meal.Description))
                {
                    mealDb.Description = meal.Description;
                }
                if (!string.IsNullOrEmpty(meal.Title))
                {
                    mealDb.Title = meal.Title;
                }

                try
                {
                    ctx.Meal.Update(mealDb);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    return(false);
                }

                return(true);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Edits a given meal in a given diet
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> EditMealInDietAsync(ActualRequest actualRequest)
        {
            Request      request    = actualRequest.Request;
            MealWithDiet meal       = JsonSerializer.Deserialize <MealWithDiet>(request.Argument.ToString());
            bool         editResult = await dietRepo.EditMealInDiet(meal);

            Request responseRequest = new Request
            {
                ActionType = ActionType.DIET_EDIT_MEAL.ToString(),
                Argument   = JsonSerializer.Serialize(editResult)
            };

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a given meal to a given diet
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> AddMealToDietAsync(ActualRequest actualRequest)
        {
            Request      request = actualRequest.Request;
            MealWithDiet meal    = JsonSerializer.Deserialize <MealWithDiet>(request.Argument.ToString());
            int          mealId  = await dietRepo.AddMealToDietAsync(meal);

            Request responseRequest = new Request
            {
                ActionType = ActionType.DIET_ADD_MEAL.ToString(),
                Argument   = JsonSerializer.Serialize(mealId)
            };

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }