Exemplo n.º 1
0
        public async Task <OperationResult> DeleteImageAsync(DeleteProductImageInput input)
        {
            var validateResult = await _productImageValidator.ValidateDeleteProductImage(input);

            if (validateResult.IsSuccess)
            {
                var productDtoResult = await _productService.GetAsync(input.ProductId);

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

                var images = await _repository.GetProductImages(productDtoResult.Result.ConvertToEntity());

                images = images.Where(x => x.Id != input.ProductImageId);

                await _repository.UpdateProductImages(images);

                return(OperationResult.Success());
            }
            else
            {
                return(OperationResult.Fail(validateResult));
            }
        }
        public async Task <ValidationResult> ValidateDeleteProductImage(DeleteProductImageInput input)
        {
            ValidationResult validationResult = new();

            if (string.IsNullOrWhiteSpace(input.ProductId))
            {
                validationResult.Messages.Add(new(nameof(DeleteProductImageInput.ProductId), "Debe ingresar el Id del Producto."));
            }
            else
            {
                Product product = await _productRepository.GetAsync(input.ProductId);

                if (product is null)
                {
                    validationResult.Messages.Add(new(nameof(DeleteProductImageInput.ProductId), "El producto no existe."));
                }
                else
                {
                    var images = await _productImageRepository.GetProductImages(product);

                    var image = images.SingleOrDefault(x => x.Id == input.ProductImageId);

                    if (image is null)
                    {
                        validationResult.Messages.Add(new(nameof(DeleteProductImageInput.ProductImageId), "La imagen no existe."));
                    }
                    else if (product.Thumbnail == image.Image)
                    {
                        validationResult.Messages.Add(new(nameof(DeleteProductImageInput.ProductImageId), "No se puede eliminar la imagen principal del producto."));
                    }
                }
            }

            return(validationResult);
        }