Example #1
0
        public async Task <ResultDto> UpdateSharedMeal(SharedMealDto sharedMealDto)
        {
            var sharedMeal = await _context.SharedMeals.Where(sm => sm.UUID == sharedMealDto.UUID).FirstOrDefaultAsync();

            if (sharedMeal == null)
            {
                _logger.LogDebug("SharedMeal not found with UUID: ", sharedMealDto.UUID);
                return(new ResultDto(false, "Meal not found"));
            }

            sharedMeal.Info      = sharedMealDto.Info;
            sharedMeal.Name      = sharedMealDto.Name;
            sharedMeal.Recipe    = sharedMealDto.Recipe;
            sharedMeal.Tags      = sharedMealDto.Tags;
            sharedMeal.UpdatedAt = DateTime.Now;
            sharedMeal.Shared    = sharedMeal.Shared;

            List <Food> foods = _foodService.MapFoodDtoListToFoodList(sharedMealDto.Foods);

            _context.SharedMealFoods.RemoveRange(_context.SharedMealFoods.Where(smf => smf.SharedMealId == sharedMeal.Id));
            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);
            });

            _context.Entry(sharedMeal).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Meal updated succesfully"));
        }
Example #2
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"));
        }