Exemple #1
0
        public async Task <OperationResult <ProductImageDto> > UploadImageAsync(string productId, byte[] image)
        {
            var productDtoResult = await _productService.GetAsync(productId);

            if (!productDtoResult.IsSuccess)
            {
                return(OperationResult <ProductImageDto> .Fail(productDtoResult.Validations));
            }

            Uri uriImage = await _imageService.UploadImage(image);

            ProductDto product = productDtoResult.Result;

            if (product?.Thumbnail is null)
            {
                productDtoResult = await _productService.UpdateAsync(
                    new(product.Id, product.Name, product.Cost, product.Price, product.PercentageOff, product.Category, product.SubCategory, uriImage)
                    );
            }

            IEnumerable <ProductImage> productImages = await _repository.GetProductImages(productDtoResult.Result.ConvertToEntity());

            List <ProductImage> newProductImages = new();

            if (productImages is not null)
            {
                newProductImages.AddRange(productImages);
            }

            ProductImage productImage = new()
            {
                Id        = Guid.NewGuid(),
                ProductId = productId,
                Image     = uriImage
            };

            newProductImages.Add(productImage);

            if (productImages.Any())
            {
                await _repository.UpdateProductImages(newProductImages);
            }
            else
            {
                await _repository.CreateProductImages(newProductImages);
            }

            return(OperationResult <ProductImageDto> .Success(productImage.ConvertToDto()));
        }
    }