public async Task<IActionResult> AddCompoundDish(CreateCompoundDishRequest dish)
 {
     var loggedUser = User.GetUserIdFromToken();
     var result = await _compoundDishService.CreateCompoundDishAsync(loggedUser, dish);
     var mapped = _mapper.Map<AdminCompoundDishResponse>(result, opt =>
     {
         opt.Items["lang"] = "es";
     });
     return Created("", new ApiOkResponse(mapped));
 }
        public async Task <CompoundDish> CreateCompoundDishAsync(int ownerId, CreateCompoundDishRequest dish)
        {
            var existName = await _uow.CompoundDishRepository.FindByAsync(c => c.UserId == ownerId && c.Name == dish.Name && !c.IsDeleted);

            if (existName.Count > 0)
            {
                throw new InvalidDataException(ExceptionConstants.INVALID_DATA, "Dish name");
            }

            var cDish = await AuxCreateCompoundDishAsync(ownerId, dish);

            await _uow.CommitAsync();

            return(cDish);
        }
        private async Task <CompoundDish> AuxCreateCompoundDishAsync(int ownerId, CreateCompoundDishRequest dish, CompoundDish existingDish = null)
        {
            var cDish = new CompoundDish();

            if (existingDish != null)
            {
                cDish.Name             = existingDish.Name;
                cDish.UserId           = existingDish.UserId;
                cDish.CreatedBy        = existingDish.CreatedBy;
                cDish.IsAdminConverted = false;
                cDish.IsAdminReviewed  = false;

                cDish.IsDeleted  = false;
                cDish.ModifiedAt = DateTime.UtcNow;
                cDish.CreatedAt  = DateTime.UtcNow;

                foreach (var item in existingDish.DishCompoundDishes)
                {
                    cDish.DishCompoundDishes.Add(new DishCompoundDish
                    {
                        DishId  = item.DishId,
                        DishQty = item.DishQty
                    });
                }

                if (existingDish.Image != null)
                {
                    string guid = Guid.NewGuid().ToString();
                    await _fileService.CopyFileAsync(existingDish.Image, guid);

                    cDish.Image         = guid;
                    cDish.ImageMimeType = existingDish.ImageMimeType;
                }
            }
            else
            {
                cDish.Name       = dish.Name;
                cDish.UserId     = ownerId;
                cDish.ModifiedAt = DateTime.UtcNow;
                cDish.CreatedAt  = DateTime.UtcNow;

                if (dish.Image != null)
                {
                    string guid = Guid.NewGuid().ToString();
                    await _fileService.UploadFileAsync(dish.Image, guid);

                    cDish.Image         = guid;
                    cDish.ImageMimeType = dish.Image.ContentType;
                }

                if (dish.Dishes != null)
                {
                    foreach (var item in dish.Dishes)
                    {
                        var dComDish = new DishCompoundDish
                        {
                            CompoundDish = cDish,
                            DishId       = item.DishId,
                            DishQty      = item.Qty
                        };
                        await _uow.DishCompoundDishRepository.AddAsync(dComDish);
                    }
                }
            }

            await _uow.CompoundDishRepository.AddAsync(cDish);

            return(cDish);
        }