public IActionResult AddProduct(ProductAddVM product) { if (!ModelState.IsValid) { return(View(product)); } string thmbnailPath = env.WebRootPath + @"\lib\images\products\thumbnail"; string saveImagePath = env.WebRootPath + @"\lib\images\products"; string imageName1 = string.Empty.GetNewName(); string imageName2 = string.Empty.GetNewName(); string imageName3 = string.Empty.GetNewName(); string imageName4 = string.Empty.GetNewName(); ProductImage productImage = new ProductImage { Thumbnail = imageName1, Image_1 = imageName1, Image_2 = imageName2, Image_3 = imageName3, Image_4 = imageName4 }; var save_Thubnailimage_Result = fileService.SaveImageFile(product.Image1, 229, 229, thmbnailPath, imageName1); var save_image1_Result = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName1); var save_image2_Result = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName2); var save_image3_Result = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName3); var save_image4_Result = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName3); var addResult = PService.AddProduct(new Store.Models.DataBase.Entities.Product { BrandId = product.BrandId, EName = product.EName, ExistCount = product.ExistCount, Features = product.Features, Images_Json = productImage.ToJsonImage(), InsertedDate = DateTime.Now, IsActive = product.IsActive, IsDeleted = false, IsSpecial = product.IsSpecial, MainDescription = product.MainDescription, Name = product.Name, SalesPrice = product.SalesPrice, ShortDescription = product.ShortDescription, Tags = product.Tags, SubCategoryId = product.SubCategoryId, UnitType = product.UnitType, WrittenPrice = product.WrittenPrice.HasValue? product.WrittenPrice.Value:0 }); if (addResult) { return(View("Index")); } return(View(product)); }
public IActionResult AddProduct(ProductAddVM product) { if (!product.Image1.IsImageValid()) { ModelState.AddModelError("Image1", "فایل تصویر نامعتبر است."); } if (!product.Image2.IsImageValid() && product.Image2 != null) { ModelState.AddModelError("Image2", "فایل تصویر نامعتبر است."); } if (!product.Image3.IsImageValid() && product.Image3 != null) { ModelState.AddModelError("Image3", "فایل تصویر نامعتبر است."); } if (!product.Image4.IsImageValid() && product.Image4 != null) { ModelState.AddModelError("Image4", "فایل تصویر نامعتبر است."); } if (!ModelState.IsValid) { return(View(product)); } string thmbnailPath = env.WebRootPath + @"\lib\images\products\thumbnail"; string saveImagePath = env.WebRootPath + @"\lib\images\products"; string imageName1 = string.Empty.GetNewName() + ".jpg"; string imageName2 = product.Image2.CheckIsNull()?string.Empty: string.Empty.GetNewName() + ".jpg"; string imageName3 = product.Image3.CheckIsNull()?string.Empty: string.Empty.GetNewName() + ".jpg"; string imageName4 = product.Image4.CheckIsNull()?string.Empty: string.Empty.GetNewName() + ".jpg"; ProductImage productImage = new ProductImage { Thumbnail = imageName1, Image_1 = imageName1, Image_2 = imageName2, Image_3 = imageName3, Image_4 = imageName4 }; if (product.Image1 != null && product.Image1.IsImageValid()) { var save_Thubnailimage_Result = fileService.SaveImageFile(product.Image1, 229, 229, thmbnailPath, imageName1); var save_image1_Result = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName1); } if (product.Image2 != null && product.Image2.IsImageValid()) { var save_image2_Result = fileService.SaveImageFile(product.Image2, 700, 700, saveImagePath, imageName2); } if (product.Image3 != null && product.Image3.IsImageValid()) { var save_image3_Result = fileService.SaveImageFile(product.Image3, 700, 700, saveImagePath, imageName3); } if (product.Image4 != null && product.Image4.IsImageValid()) { var save_image4_Result = fileService.SaveImageFile(product.Image4, 700, 700, saveImagePath, imageName4); } var addResult = PService.AddProduct(new Store.Models.DataBase.Entities.Product { BrandId = product.BrandId, EName = product.EName, ExistCount = product.ExistCount, Features = product.Features, Images_Json = productImage.ToJsonImage(), InsertedDate = DateTime.Now, IsActive = product.IsActive, IsDeleted = false, IsSpecial = product.IsSpecial, MainDescription = product.MainDescription, Name = product.Name, SalesPrice = product.SalesPrice, ShortDescription = product.ShortDescription, Tags = product.Tags, SubCategoryId = product.SubCategoryId, UnitType = product.UnitType, WrittenPrice = product.WrittenPrice.HasValue ? product.WrittenPrice.Value : 0 }); if (addResult) { return(RedirectToAction("index", "ProductManager")); } return(View(product)); }
public IActionResult EditeProduct(ProductEditedAddVM product) { #region CheckValiData if (!product.Image1.IsImageValid()) { ModelState.AddModelError("Image1", "فایل تصویر نامعتبر است."); } if (!product.Image2.IsImageValid() && product.Image2 != null) { ModelState.AddModelError("Image2", "فایل تصویر نامعتبر است."); } if (!product.Image3.IsImageValid() && product.Image3 != null) { ModelState.AddModelError("Image3", "فایل تصویر نامعتبر است."); } if (!product.Image4.IsImageValid() && product.Image4 != null) { ModelState.AddModelError("Image4", "فایل تصویر نامعتبر است."); } if (!ModelState.IsValid) { return(View(product)); } #endregion var p = PService.GetProductById(product.ProductId); string thmbnailPath = env.WebRootPath + @"\lib\images\products\thumbnail"; string saveImagePath = env.WebRootPath + @"\lib\images\products"; string imageName1 = string.Empty.GetNewName() + ".jpg"; string imageName2 = string.Empty.GetNewName() + ".jpg"; string imageName3 = string.Empty.GetNewName() + ".jpg"; string imageName4 = string.Empty.GetNewName() + ".jpg"; if (p == null) { return(View(product)); } var pimage = p.Images_Json.ToProductImage(); if (product.Image1 != null && product.Image1.IsImageValid()) { fileService.DeleteFile(pimage.Thumbnail, thmbnailPath); fileService.DeleteFile(pimage.Image_1, saveImagePath); var save_Thubnailimage_Result = fileService.SaveImageFile(product.Image1, 229, 229, thmbnailPath, imageName1); var save_image1_Result = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName1); } if (product.Image2 != null && product.Image2.IsImageValid()) { fileService.DeleteFile(pimage.Image_2, saveImagePath); var save_image2_Result = fileService.SaveImageFile(product.Image2, 700, 700, saveImagePath, imageName2); } if (product.Image3 != null && product.Image3.IsImageValid()) { fileService.DeleteFile(pimage.Image_3, saveImagePath); var save_image3_Result = fileService.SaveImageFile(product.Image3, 700, 700, saveImagePath, imageName3); } if (product.Image4 != null && product.Image4.IsImageValid()) { fileService.DeleteFile(pimage.Image_4, saveImagePath); var save_image4_Result = fileService.SaveImageFile(product.Image4, 700, 700, saveImagePath, imageName4); } ProductImage productImage = new ProductImage { Thumbnail = product.Image1.CheckIsNull() ? pimage.Image_1 : imageName1, Image_1 = product.Image1.CheckIsNull() ? pimage.Image_1 : imageName1, Image_2 = product.Image2.CheckIsNull() ? pimage.Image_2 : imageName2, Image_3 = product.Image3.CheckIsNull() ? pimage.Image_3 : imageName3, Image_4 = product.Image4.CheckIsNull() ? pimage.Image_4 : imageName4 }; #region Update Product p.BrandId = product.BrandId; p.EName = product.EName; p.ExistCount = product.ExistCount; p.Features = product.Features; p.Images_Json = productImage.ToJsonImage(); p.IsActive = product.IsActive; p.IsSpecial = product.IsSpecial; p.MainDescription = product.MainDescription; p.Name = product.Name; p.ProductId = product.ProductId; p.SalesPrice = product.SalesPrice; p.ShortDescription = product.ShortDescription; p.SubCategoryId = product.SubCategoryId; p.Tags = product.Tags; p.UnitType = product.UnitType; p.WrittenPrice = product.WrittenPrice.HasValue ? product.WrittenPrice.Value : 0; #endregion var addResult = PService.UpdateProduct(p, p.ProductId); if (addResult) { return(RedirectToAction("index", "ProductManager")); } return(View(product)); }