private async Task LoadImages(IEnumerable <string> pictureUrls, SellerProduct item, List <byte[]> imageHashes) { const int ImageLoadCount = 6; using (var client = new HttpClient()) { var getTasks = pictureUrls.Take(ImageLoadCount).Select(x => client.GetByteArrayAsync(x)); var imagesData = await Task.WhenAll(getTasks); var productImages = imagesData.Select(x => new ProductImage() { ImageData = x, Item = item }); using (MD5 md5 = MD5.Create()) { foreach (var img in productImages) { var hash = md5.ComputeHash(img.ImageData); if (!imageHashes.Any(x => x.SequenceEqual(hash))) { _imageRepo.Add(img); imageHashes.Add(hash); } } } } if (pictureUrls.Count() > imageHashes.Count) { await LoadImages(pictureUrls.Skip(ImageLoadCount), item, imageHashes); } }
public Product AddProduct(ProductAddViewModel productAdd) { //MessageBox.Show(Environment.CurrentDirectory); Product product = new Product { Name = productAdd.Name, CategoryId = productAdd.CategoryId, Price = productAdd.Price, Quantity = productAdd.Quantity, DateCreate = DateTime.Now }; _productRepository.Add(product); _productRepository.SaveChanges(); if (productAdd.Images.Count > 0) { List <string> imgTemp = new List <string>(); foreach (var p in productAdd.Images) { string fSaveName = Guid.NewGuid().ToString() + ".jpg"; //Path.GetFileName(p.Source); string fImages = Environment.CurrentDirectory + ConfigurationManager.AppSettings["ImageStore"].ToString(); string fSaveImageBig = "o_" + fSaveName; string fSaveImageSmall = "s_" + fSaveName; // Will not overwrite if the destination file already exists. try { File.Copy(p.SourceOriginal, Path.Combine(fImages, fSaveImageBig)); imgTemp.Add(fSaveImageBig); using (FileStream stream = new FileStream(Path.Combine(fImages, fSaveImageSmall), FileMode.Create)) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(p.ImageFrame); encoder.Save(stream); imgTemp.Add(fSaveImageSmall); } ProductImage image = new ProductImage // сохраняем в таблицу ProductImages { Name = fSaveName, // сохраняем имя без приставки ProductId = product.Id }; _productImageRepository.Add(image); // Catch exception if the file was already copied. } catch (IOException copyError) { MessageBox.Show(copyError.Message); foreach (var item in imgTemp) { File.Delete(Path.Combine(fImages, item)); } return(null); } } _productImageRepository.SaveChanges(); } return(product); }
public void SaveImages(int productId, List <ProductImageViewModel> productImageVms) { var productImageModels = Mapper.Map <List <ProductImageViewModel>, List <ProductImage> >(productImageVms); foreach (var img in productImageModels) { _productImageRepository.Add(img); } }
public void AddImages(int productId, string[] images) { _productImageRepository.RemoveMultiple(_productImageRepository.FindAll(x => x.ProductId == productId).ToList()); foreach (var image in images) { _productImageRepository.Add(new ProductImage() { Path = image, ProductId = productId, Caption = string.Empty }); } }
public void Add(ImageUploadDTO imageUploadDto) { var imageId = _imageRepository.Add(new Image { ImageBase64 = imageUploadDto.Image }); _productImageRepository.Add(new ProductImage { ProductId = imageUploadDto.ProductId, ImageId = imageId }); }
public async Task AddImages(int productId, string[] images) { await _productImageRepository.RemoveMultiple((await _productImageRepository.FindAll(x => x.ProductId == productId)).AsNoTracking().AsParallel().AsOrdered().ToList()); foreach (var image in images) { await _productImageRepository.Add(new ProductImage() { Path = image, ProductId = productId, Caption = string.Empty }); } }
public void Add(ProductImage productImage) { _productImageRepository.Add(productImage); }
public ActionResult Create(ProductViewModel model) { if (ModelState.IsValid) { try { var ProductId = Guid.NewGuid().ToString(); List <string> productImageList = ProcessUploadedFile(model); _productRepository.Add(new Product() { Id = ProductId, ProductCode = model.ProductCode, ProductName = model.ProductName, Slug = model.Slug, Description = model.Description, ProductTypeId = model.ProductTypeId, MaterialId = model.MaterialId, CategoryId = model.CategoryId, PriceType = int.Parse(model.PriceType), Price = model.Price }); _productRepository.Save(requestContext); if (productImageList != null && productImageList.Count > 0) { foreach (var image in productImageList) { _productImageRepository.Add(new ProductImage() { Id = Guid.NewGuid().ToString(), ProductId = ProductId, Url = image, CreateAt = DateTime.UtcNow }); } _productImageRepository.Save(requestContext); } if (model.TagList != null && model.TagList.Count > 0) { foreach (var item in model.TagList) { _productTagRepository.Add(new ProductTag() { Id = Guid.NewGuid().ToString(), ProductId = ProductId, TagId = item }); } } _productTagRepository.Save(requestContext); } catch (Exception) { SetComboData(); return(View()); } return(RedirectToAction("Index")); } SetComboData(); return(View()); }
public void Add(ProductImage entity) { repo.Add(entity); }
public async Task <IActionResult> Create(ProductViewModel model) { if (ModelState.IsValid) { try { var productId = Guid.NewGuid().ToString(); List <string> productImageList = ProcessUploadedFile(model); await _productRepository.AddAsync(new Product { Id = productId, ProductCode = model.ProductCode, ProductName = model.ProductName, Slug = model.Slug, Description = model.Description, ProductTypeId = model.ProductTypeId, MaterialId = model.MaterialId, CategoryId = model.CategoryId, PriceType = int.Parse(model.PriceType), Price = model.Price, IsFeatured = model.IsFeatured, IsNew = model.IsNew, Actived = model.Actived, Discount = model.Discount, ExtraDiscount = model.ExtraDiscount, SeoAlias = TextHelper.ToUnsignString(model.ProductName), PriceAfter = Math.Round((double)((1 - model.Discount / 100 - model.ExtraDiscount / 100) * model.Price.GetValueOrDefault()), 1, MidpointRounding.AwayFromZero) }); await _productRepository.SaveAsync(RequestContext); _cache.Remove("CACHE_MASTER_PRODUCT"); if (productImageList != null && productImageList.Count > 0) { foreach (var image in productImageList) { _productImageRepository.Add(new ProductImage() { Id = Guid.NewGuid().ToString(), ProductId = productId, Url = image, CreateAt = DateTime.UtcNow }); } _productImageRepository.Save(RequestContext); } if (model.TagList != null && model.TagList.Count > 0) { foreach (var item in model.TagList) { _productTagRepository.Add(new ProductTag() { Id = Guid.NewGuid().ToString(), ProductId = productId, TagId = item }); } } _productTagRepository.Save(RequestContext); } catch (Exception) { SetComboData(); ViewBag.ProductCode = HttpContext.Session.GetString("ProductCode"); return(View()); } HttpContext.Session.Remove("ProductCode"); return(RedirectToAction("Index")); } SetComboData(); ViewBag.ProductCode = HttpContext.Session.GetString("ProductCode"); return(View()); }
public ProductImage Add(ProductImage productImage) { return(_productImageRepository.Add(productImage)); }