コード例 #1
0
        public async Task <int> AddDietAsync(DietSocketsModelWithOwner diet)
        {
            using (ShapeAppDbContext ctx = new ShapeAppDbContext())
            {
                try
                {
                    User owner = await ctx.Users.FirstAsync(u => u.Id == diet.Owner.UserId);

                    Diet dietDb = new Diet
                    {
                        Description = diet.Description,
                        IsPublic    = diet.Global,
                        Owner       = owner,
                        Title       = diet.Title
                    };
                    await ctx.Diet.AddAsync(dietDb);

                    await ctx.SaveChangesAsync();

                    int createdDietId = ctx.Diet.ToList().Last().Id;

                    if (diet.Meals != null && diet.Meals.Any())
                    {
                        foreach (var meal in diet.Meals)
                        {
                            int createdMealId;
                            if (meal.Id > 0)
                            {
                                createdMealId = meal.Id;
                            }
                            else
                            {
                                await ctx.Meal.AddAsync(meal);

                                await ctx.SaveChangesAsync();

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

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

                            await ctx.SaveChangesAsync();
                        }
                    }

                    return(createdDietId);
                }
                catch (Exception e)
                {
                    return(-1);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Retrieves a diet with its owner, by id
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> GetDietByIdAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;
            int     dietId  = Convert.ToInt32(request.Argument.ToString());
            DietSocketsModelWithOwner diet = await dietRepo.GetDietByIdAsync(dietId);

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

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
コード例 #3
0
        /// <summary>
        /// Persists a given diet to the database
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> AddDietAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;
            DietSocketsModelWithOwner diet = JsonSerializer.Deserialize <DietSocketsModelWithOwner>(request.Argument.ToString());
            int dietId = await dietRepo.AddDietAsync(diet);

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

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