예제 #1
0
        public async Task <string> InsertImage(ProductImageDto dto)
        {
            string message     = "";
            var    con         = _baseInterface.GetConnection();
            var    transaction = con.BeginTransaction();

            try
            {
                int result  = 0;
                var product = await _productRepository.GetProductByIdAsync(dto.ProductId);

                if (product == null || dto.Image is null)
                {
                    return("Product is not available or image cannot be empty");
                }

                var productImages = await GetProductImageByProductId(dto.ProductId);

                if (productImages.Count() > 0)
                {
                    if (dto.IsPrimary == true)
                    {
                        _productRepository.ProductPrimaryImage(con, transaction, dto.ProductId);
                    }
                }
                else
                {
                    dto.IsPrimary = true;
                }

                var entity = dto.ToEntity();
                entity.Photo = _imageService.ConvertToByte(dto.Image);
                result       = _productRepository.InsertProductImage(entity, transaction, con);
                message      = _messageClass.ShowSuccessMessage(result);
                transaction.Commit();
            }
            catch (SqlException ex)
            {
                message = _messageClass.ShowErrorMessage(string.Format("{0} ~ {1}", ex.Number.ToString(), ex.Message));
                transaction.Rollback();
            }
            return(message);
        }