Ejemplo n.º 1
0
        protected virtual async Task <List <ProductMainImage> > GetImagesAsync(IEnumerable <ProductWithVariant> products)
        {
            var imageRequests = products.Select(identifier =>
            {
                var imageRequest = new ProductImageRequest
                {
                    ProductId = identifier.Product.Id,
                    Variant   = identifier.Variant == null ? VariantKey.Empty : new VariantKey
                    {
                        Id = identifier.Variant.Id,
                        KeyVariantAttributeValues = identifier.Variant.PropertyBag
                    },
                    ProductDefinitionName = identifier.Product.DefinitionName
                };

                var imageUrl = DamProvider.GetMediaImageUrl(identifier.Product, identifier.Variant?.Id);
                if (imageUrl != null)
                {
                    imageRequest.PropertyBag = new Dictionary <string, object> {
                        ["ImageUrl"] = imageUrl
                    };
                }
                return(imageRequest);
            }).ToList();

            var imageRequestParam = new GetProductMainImagesParam
            {
                ImageSize            = ProductConfiguration.ProductSummaryImageSize,
                ProductImageRequests = imageRequests
            };

            var mainImages = await DamProvider.GetProductMainImagesAsync(imageRequestParam).ConfigureAwait(false);

            return(mainImages);
        }
Ejemplo n.º 2
0
        protected virtual GetProductMainImagesParam GetImagesParam(IEnumerable <ProductWithVariant> products)
        {
            var imageRequests = products.Select(identifier =>
            {
                var imageRequest = new ProductImageRequest
                {
                    ProductId = identifier.Product.Id,
                    Variant   = identifier.Variant == null ? VariantKey.Empty : new VariantKey
                    {
                        Id = identifier.Variant.Id,
                        KeyVariantAttributeValues = identifier.Variant.PropertyBag
                    },
                    ProductDefinitionName = identifier.Product.DefinitionName
                };

                identifier.Product.PropertyBag.TryGetValue("ImageUrl", out var imageUrlProperty);
                var imageUrl = imageUrlProperty?.ToString() ?? DamProvider.GetMediaImageUrl(identifier.Product, identifier.Variant?.Id);

                if (imageUrl != null)
                {
                    imageRequest.PropertyBag = new Dictionary <string, object> {
                        ["ImageUrl"] = imageUrl
                    };
                }
                return(imageRequest);
            }).ToList();

            return(new GetProductMainImagesParam
            {
                ImageSize = ProductConfiguration.ThumbnailImageSize,
                ProductImageRequests = imageRequests
            });
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateProductImage(int productId, [FromForm] ProductImageRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var result = await _productImageService.Update(productId, request);

            if (!result.IsSuccessed)
            {
                return(BadRequest(result.Message));
            }
            return(Ok());
        }
        public async Task <IActionResult> UploadFileAsync([FromBody] ProductImageRequest productImageRequest)
        {
            try
            {
                var fileName = await _azureStorageService.UploadFile(productImageRequest.ImageBase64);

                await _productBusiness.AddProductFileAsync(fileName, productImageRequest.ProductId);

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e.ToString()));
            }
        }
Ejemplo n.º 5
0
        public async Task <ApiResponse <string> > Update(int productId, ProductImageRequest request)
        {
            var productFromDb = await _db.Products.FindAsync(productId);

            if (productFromDb == null)
            {
                return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId)));
            }
            var productImageFromDb = await _db.ProductImages.Where(x => x.ProductId == productId).ToListAsync();

            if (productImageFromDb.Count == 0)
            {
                return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId)));
            }
            if (request.ProductImages == null)
            {
                return(new ApiErrorResponse <string>(ConstantStrings.imgIsEmptyOrNull));
            }
            var images = request.ProductImages.ToList();

            for (int i = 0; i < images.Count; i++)
            {
                foreach (var item in productImageFromDb)
                {
                    if (images[i].Name == item.Caption)
                    {
                        var deleteResult = await DeleteImage(item.PublicId);

                        if (deleteResult.Error != null)
                        {
                            return(new ApiErrorResponse <string>(deleteResult.Error.ToString()));
                        }
                        var uploadResult = await UploadImage(images[i]);

                        if (uploadResult.Error != null)
                        {
                            return(new ApiErrorResponse <string>(uploadResult.Error.ToString()));
                        }
                        item.ImageUrl = uploadResult.SecureUrl.ToString();
                        item.PublicId = uploadResult.PublicId;
                        break;
                    }
                }
            }
            await _db.SaveChangesAsync();

            return(new ApiSuccessResponse <string>(ConstantStrings.editSuccessfully));
        }
Ejemplo n.º 6
0
        public async Task <ApiResponse <string> > Create(int productId, ProductImageRequest request)
        {
            var productFromDb = await _db.Products.FindAsync(productId);

            if (productFromDb == null)
            {
                return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId)));
            }
            var productImageFromDb = await _db.ProductImages.Where(x => x.ProductId == productFromDb.Id)
                                     .OrderByDescending(x => x.SortOrder).ToListAsync();

            if (productFromDb == null)
            {
                return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId)));
            }
            if (request.ProductImages == null)
            {
                return(new ApiErrorResponse <string>(ConstantStrings.imgIsEmptyOrNull));
            }
            var images = request.ProductImages.ToList();

            for (int i = 0; i < images.Count; i++)
            {
                var uploadResult = await UploadImage(images[i]);

                if (uploadResult.Error != null)
                {
                    return(new ApiErrorResponse <string>(uploadResult.Error.ToString()));
                }
                var productImage = new ProductImage()
                {
                    ImageUrl  = uploadResult.SecureUrl.ToString(),
                    PublicId  = uploadResult.PublicId,
                    Caption   = SystemFunctions.ProductImageCaption(productFromDb.Name, productImageFromDb.FirstOrDefault().SortOrder + i + 1),
                    ProductId = productFromDb.Id,
                    SortOrder = productImageFromDb.FirstOrDefault().SortOrder + i + 1
                };
                _db.ProductImages.Add(productImage);
            }
            _db.ProductImages.Remove(productImageFromDb.Where(x => x.PublicId == ConstantStrings.blankProductImagePublicId).FirstOrDefault());
            await _db.SaveChangesAsync();

            return(new ApiSuccessResponse <string>(ConstantStrings.addSuccessfully));
        }