public async Task <IActionResult> CreateAd(ProductAdViewModel productAdViewModel) { IList <ProductAd> possibleAdImages = null; possibleAdImages = await _productService.ListPossibleAdImages(productAdViewModel.SelectedProductId); var img = productAdViewModel.ProductAdImage; if (productAdViewModel.ProductAdImage != null) { var adImagePath = await _appEnvironment.UploadImageAsync(productAdViewModel.ProductAdImage, "main carousel", uploadMaxByteSize); if (adImagePath != null) { ProductAd productAd = new ProductAd { AdImageUrl = adImagePath, Product = await _productService.FindProductByIdAsync(productAdViewModel.SelectedProductId) }; if (possibleAdImages != null && possibleAdImages.Count > 0) { await _appEnvironment.DeleteImageAsync(possibleAdImages[0].AdImageUrl, "main carousel"); await _productService.DeleteProductAd(possibleAdImages[0]); } await _productService.CreateProductAd(productAd); } } return(await EditMainCarousel()); }
public async Task <IActionResult> DeleteAttributes(ManageAttributesViewModel model) { ICollection <Models.Attribute> attributesToRemove = new List <Models.Attribute>(); foreach (var id in model.IdsOfSelectedAttributesToRemove) { Models.Attribute attribute = _attributeService.FindAttributeById(id); if (attribute.IconUrl != null) { await _appEnvironment.DeleteImageAsync(attribute.IconUrl, "attribute-icons"); } attributesToRemove.Add(attribute); } await _attributeService.RemoveAttributeRange(attributesToRemove); return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(int id, ProductCategoryViewModel model) { if (id != model.Product.Id) { return(NotFound()); } if (ModelState.IsValid) { try { IList <ProductCategory> relatedProductCategories = null; IList <ProductImage> possiblePrimaryImages = null; IList <ProductImage> possibleOtherImages = null; relatedProductCategories = await _navigationService.GetProductCategories(model.Product.Id); if (model.PrimaryImage != null) { possiblePrimaryImages = await _productService.GetPrimaryImages(model.Product); } if (model.IdsOfSelectedImages != null) { possibleOtherImages = await _productService.GetSecondaryImages(model.Product); } //New primary image if (model.PrimaryImage != null) { var primaryImagePath = await _appEnvironment.UploadImageAsync(model.PrimaryImage, "products", uploadMaxByteSize); if (primaryImagePath != null) { ProductImage primaryImage = new ProductImage { IsPrimary = true, ImageUrl = primaryImagePath, Product = model.Product }; if (possiblePrimaryImages != null && possiblePrimaryImages.Count > 0) { await _appEnvironment.DeleteImageAsync(possiblePrimaryImages[0].ImageUrl, "products"); await _productService.DeleteProductImage(possiblePrimaryImages[0]); } await _productService.CreateProductImage(primaryImage); } else { model = await FillUpProductEditData(model, model.Product); ModelState.AddModelError(string.Empty, "Primary image file is of the wrong format or too large."); return(View(model)); } } //New additional images if (model.OtherImages != null) { foreach (IFormFile image in model.OtherImages) { var otherImagePath = await _appEnvironment.UploadImageAsync(image, "products", uploadMaxByteSize); if (otherImagePath != null) { ProductImage otherImage = new ProductImage { IsPrimary = false, ImageUrl = otherImagePath, Product = model.Product }; await _productService.CreateProductImage(otherImage); } else { model = await FillUpProductEditData(model, model.Product); ModelState.AddModelError(string.Empty, "One of the additional image files is of the wrong format or too large."); return(View(model)); } } } //Remove old images if (possibleOtherImages != null && possibleOtherImages.Count > 0 && model.IdsOfSelectedImages != null) { foreach (int imageId in model.IdsOfSelectedImages) { List <ProductImage> imageToClean = possibleOtherImages.Where(image => image.Id == imageId).ToList(); if (imageToClean.Count > 0) { await _appEnvironment.DeleteImageAsync(imageToClean[0].ImageUrl, "products"); await _productService.DeleteProductImage(imageToClean[0]); } } } //Opt locking separated too. Leave it or keep it? await _productService.UpdateRowVersionEntry(model.Product); if (model.IdsOfSelectedCategories != null) { foreach (int categoryId in model.IdsOfSelectedCategories) { ProductCategory productCategory = new ProductCategory { ProductId = model.Product.Id, CategoryId = categoryId }; await _navigationService.UpdateProductCategory(productCategory); } } foreach (var pc in relatedProductCategories) { await _navigationService.DeleteProductCategory(pc.Id); } ; } catch (DbUpdateConcurrencyException ex) { if ((await _productService.GetAllProducts()).Any(p => p.Id == model.Product.Id) == false) { return(NotFound()); } else { var exceptionEntry = ex.Entries.Single(); var clientValues = (Product)exceptionEntry.Entity; var databaseEntry = await exceptionEntry.GetDatabaseValuesAsync(); var databaseValues = (Product)databaseEntry.ToObject(); if (databaseValues.Name != clientValues.Name) { ModelState.AddModelError("Product.Name", $"Current value: {databaseValues.Name}"); } if (databaseValues.Description != clientValues.Description) { ModelState.AddModelError("Product.Description", $"Current value: {databaseValues.Description}"); } if (databaseValues.Price != clientValues.Price) { ModelState.AddModelError("Product.Price", $"Current value: {databaseValues.Price}"); } IList <Category> allCategories = await _navigationService.GetAllCategories(); IList <ProductCategory> allProductCategories = await _navigationService.GetAllProductCategories(); var dbProductCategoryNames = await Task.Run(() => allProductCategories.Where(x => x.Product == model.Product).Select(x => x.Category.Name).ToArray()); var clientProductCategoryNames = await Task.Run(() => allCategories.Where(x => model.IdsOfSelectedCategories.Contains(x.Id)).Select(x => x.Name).ToArray()); if (!dbProductCategoryNames.ToHashSet().SetEquals(clientProductCategoryNames.ToHashSet())) { string categoryStrings = String.Join(", ", dbProductCategoryNames); ModelState.AddModelError("IdsOfSelectedCategories", $"Current value: {categoryStrings}"); } ModelState.AddModelError(string.Empty, "The product's values were updated while you were editing them. Review the changes (if any) " + "and if you still wish to submit them, click the 'Save' button again."); await FillUpProductEditData(model, model.Product); model.Product.RowVersion = databaseValues.RowVersion; ModelState.Remove("Product.RowVersion"); return(RedirectToAction("Index", "Home")); } } return(RedirectToAction("Index", "Home")); } return(RedirectToAction("Index", "Home")); }