public async Task EditProduct(EditProductInput input)
        {
            var id = input.Id;

            if (id == null)
            {
                var count = _productRepository.GetAllList(m => m.Name == input.Name).Count();
                if (count > 0)
                {
                    throw new UserFriendlyException(string.Format("产品名 {0} 已经存在", input.Name));
                }
                var product = input.MapTo <Product>();
                await _productRepository.InsertAsync(product);
            }
            else
            {
                var count = _productRepository.GetAllList(m => m.Name == input.Name && m.Id != id).Count();
                if (count > 0)
                {
                    throw new UserFriendlyException(string.Format("产品名 {0} 已经存在", input.Name));
                }
                var product = await _productRepository.GetAsync((int)id);

                //oldProduct.Name = input.Name;
                //oldProduct.Description = input.Description;
                product = input.MapTo <EditProductInput, Product>(product);
                await _productRepository.UpdateAsync(product);
            }
        }
        public async Task <IActionResult> Edit(int id, [FromForm] EditProductInput input)
        {
            var absolutePath = Path.Combine(_webHostEnvironment.WebRootPath, Product.IMAGE_PATH);
            await _productService.Edit(id, input, absolutePath);

            await _dbContext.SaveChangesAsync();

            return(Ok());
        }
示例#3
0
        public async Task Edit(int id, EditProductInput input, string imageFolderPath)
        {
            var product = await Get(id);

            if (product == null)
            {
                throw new Exception("product not found");
            }

            var sources   = input.Description.GetBase64Sources();
            var extension = "";

            byte[] imageBytes = null;
            string imageName  = "";

            foreach (var src in sources)
            {
                extension  = src.GetExtensionFromBase64ImageSource();
                imageBytes = Convert.FromBase64String(src.GetValueFromBase64ImageSource());
                imageName  = $"{Guid.NewGuid()}.{extension}";
                Upload.ByteArrayToFile($"{imageFolderPath}/{imageName}", imageBytes);
                input.Description = input.Description.Replace(src, $"{_configuration.GetSection("Domain").Value}/{Product.IMAGE_PATH}/{imageName}");
            }

            var imgs = new List <string>();

            if (input.ImageLinks != null)
            {
                imgs.AddRange(input.ImageLinks.Select(x => x.Replace($"{Product.IMAGE_PATH}/", "")));
            }
            if (input.ImageFiles != null)
            {
                imgs.AddRange(await Task.WhenAll <string>(input.ImageFiles.Select(img => Upload.UploadImageAsync(img, imageFolderPath))));
            }
            product.Images          = string.Join(";", imgs);
            product.Name            = input.Name;
            product.Code            = "product";
            product.Description     = input.Description;
            product.Price           = input.Price;
            product.Status          = input.Status;
            product.SeoUrl          = input.SeoUrl;
            product.MetaDescription = input.MetaDescription;
            product.MetaTitle       = input.MetaTitle;
        }