public async Task AddNewProductAsync(NewProductInDto dto) { dto.MainPicURL = dto.MainPicURL.Contains("res.cloudinary.com") ? dto.MainPicURL : cloudineryService.RelocateImgToCloudinary(dto.Name + "mainPic", dto.MainPicURL, null); var newProduct = mapper.Map <Product>(dto); foreach (var characteristic in dto.Characteristics.Distinct()) { newProduct.Characteristics.Add(new ProductCharacteristic { Title = characteristic.Title, TextValue = characteristic.TextValue, NumericValue = characteristic.NumericValue }); } int counter = 0; foreach (var pic in dto.ProductPictures.Distinct()) { newProduct.ProductPictures.Add(new ProductPicture { PictureURL = pic.PictureURL.Contains("res.cloudinary.com") ? pic.PictureURL : cloudineryService.RelocateImgToCloudinary(dto.Name + "altPic" + counter++, pic.PictureURL, info: Guid.NewGuid().ToString()), PictureDescription = pic.PictureDescription, }); } await productsRepository.AddAssync(newProduct); await productsRepository.SaveChangesAsync(); }
public async Task <IActionResult> AddProduct(NewProductInDto dto) { if (!string.IsNullOrEmpty(dto.ChangeCount)) { if (dto.ChangeCount == "pic++") { dto.ProductPictures.Add(new NewProductPictureDto()); } if (dto.ChangeCount == "char++") { dto.Characteristics.Add(new NewProductCharacteristicDto()); } if (dto.ChangeCount == "pic--") { dto.ProductPictures.RemoveAt(dto.ProductPictures.Count - 1); } if (dto.ChangeCount == "char--") { dto.Characteristics.RemoveAt(dto.Characteristics.Count - 1); } ViewData["Categories"] = categoryService.GetAllMinified(); ViewData["Manufacturers"] = manufacturerService.GetAllMinified(); return(View(dto)); } if (ModelState.IsValid) { memoryCache.Remove(GlobalConstants.CasheCategoriesInButtonName); memoryCache.Remove(GlobalConstants.CasheManufactorersInButtonName); await productsService.AddNewProductAsync(dto); return(RedirectToAction(nameof(Manage))); } ViewData["Categories"] = categoryService.GetAllMinified(); ViewData["Manufacturers"] = manufacturerService.GetAllMinified(); return(this.View(dto)); }