Example #1
0
        public async Task <DishDto> AddDishAsync(AddDishDto addDish)
        {
            if (await _unitOfWork.Dishes.GetByNameAsync(addDish.Name) != null)
            {
                ExceptionHandler.DublicateObject(nameof(Dish), nameof(Dish.Name));
            }

            var dishEntity = _unitOfWork.Dishes.Add(_mapper.Map <AddDishDto, Dish>(addDish));

            var priority = 1;

            dishEntity.Images = (await _imageService.UploadRangeAsync(addDish.Images))
                                .Select(url => new Image {
                Priority = priority++, DishId = dishEntity.Id, Url = url
            })
                                .ToList();

            var dishIngredients = addDish.Ingredients
                                  .Zip(addDish.IngredientAmounts, (i, a) => new { IngredientId = i, Amount = a })
                                  .Select(
                x =>
                new DishIngredient
            {
                DishId       = dishEntity.Id,
                IngredientId = x.IngredientId,
                Amount       = x.Amount
            });

            _unitOfWork.DishIngredients.AddRange(dishIngredients);

            var dishCategories = addDish.Categories.Select(
                x => new DishCategory
            {
                CategoryId = x, DishId = dishEntity.Id
            });

            _unitOfWork.DishCategories.AddRange(dishCategories);

            await _unitOfWork.CommitAsync();

            return(_mapper.Map <Dish, DishDto>(dishEntity));
        }
 public async Task <IActionResult> AddDish([FromForm] AddDishDto addDish)
 {
     return(Ok(await _dishService.AddDishAsync(addDish)));
 }
Example #3
0
 public async Task Add([FromForm] AddDishDto dish)
 {
     await _dishService.Add(dish);
 }
Example #4
0
 public async Task Add(AddDishDto dishDto)
 {
     var dish = _mapper.Map <Dish>(dishDto);
     await _repository.Add(dish);
 }