Exemple #1
0
        public async Task <ResultDto> AddNewSharedMeal(SharedMealDto sharedMealDto, string userId)
        {
            var user = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync();

            if (user == null)
            {
                return(new ResultDto(false, "Unauthorized"));
            }

            List <Food> foods      = _foodService.MapFoodDtoListToFoodList(sharedMealDto.Foods);
            var         sharedMeal = _mapper.Map <SharedMeal>(sharedMealDto);

            sharedMeal.UUID      = Guid.NewGuid().ToString();
            sharedMeal.User      = user;
            sharedMeal.CreatedAt = DateTime.Now;
            sharedMeal.UpdatedAt = DateTime.Now;
            await _context.AddAsync(sharedMeal);

            foods.ForEach(f => {
                var smf = new SharedMealFood
                {
                    FoodId     = f.Id,
                    SharedMeal = sharedMeal,
                    FoodAmount = sharedMealDto.Foods.FindLast(food => food.UUID == f.UUID).Amount
                };
                _context.Add(smf);
            });

            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Meal shared succesfully"));
        }