Esempio n. 1
0
        public async Task <IActionResult> UpdateDish([FromBody] UpdateEatRequest eat)
        {
            var loggedUser = User.GetUserIdFromToken();
            var language   = await _userService.GetUserLanguageFromUserIdAsync(loggedUser);

            var result = await _eatService.UpdateEatAsync(loggedUser, eat);

            var mapped = _mapper.Map <EatResponse>(result, opt =>
            {
                opt.Items["lang"] = language;
            });

            return(Created("", new ApiOkResponse(mapped)));
        }
Esempio n. 2
0
        public async Task <Eat> UpdateEatAsync(int loggedUser, UpdateEatRequest eat)
        {
            var e = await _uow.EatRepository.GetAll().Where(e => e.Id == eat.Id)
                    .Include(e => e.User)
                    .Include(e => e.EatDishes)
                    .ThenInclude(ed => ed.Dish)
                    .ThenInclude(d => d.DishTags)
                    .ThenInclude(dt => dt.Tag)
                    .Include(e => e.EatCompoundDishes)
                    .ThenInclude(ed => ed.CompoundDish)
                    .ThenInclude(d => d.DishCompoundDishes)
                    .ThenInclude(dt => dt.Dish)
                    .ThenInclude(dt => dt.DishTags)
                    .ThenInclude(dt => dt.Tag)
                    .FirstOrDefaultAsync();

            if (e == null)
            {
                throw new NotFoundException(ExceptionConstants.NOT_FOUND, "Eat");
            }

            e.EatType    = (EatTypeEnum)eat.EatType;
            e.ModifiedAt = DateTime.UtcNow;

            foreach (var ed in e.EatDishes)
            {
                _uow.EatDishRepository.Delete(ed);
            }

            var eatDishes = new List <EatDish>();

            foreach (var ed in eat.Dishes)
            {
                var eatD = new EatDish();
                eatD.DishId = ed.DishId;
                eatD.Qty    = ed.Qty;

                eatDishes.Add(eatD);
            }
            e.EatDishes = eatDishes;

            _uow.EatRepository.Update(e);
            await _uow.CommitAsync();

            return(e);
        }