public ActionResult AddColor(Color color, HttpPostedFileBase upload) { if (upload == null) { ModelState.AddModelError("NoImage", "Upload color's image"); } if (ModelState.IsValid) { Guid number = Guid.NewGuid(); // assign upload to color and save on server FilePath colorImage = new FilePath() { FileType = FileType.colorImage, FileName = Path.GetFileName(number + "-" + upload.FileName) }; color.FilePath = colorImage; color.FilePathId = colorImage.FilePathId; upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/Colors"), colorImage.FileName)); // save changes repository.Add(color); colorImage.Colors.Add(color); repository.Save(); return RedirectToAction("Index"); } // return if invalid return View(color); }
public ActionResult AddSupplier(Supplier supplier, HttpPostedFileBase upload) { ViewBag.DeliveryMethodID = new SelectList(Retriever.GetDeliveryMethods(), "DeliveryMethodId", "Name", null); if (upload == null) { ModelState.AddModelError("NoImage", "Upload supplier's image"); } if (ModelState.IsValid) { Guid number = Guid.NewGuid(); // assign upload to color and save on server FilePath image = new FilePath() { FileType = FileType.supplierImage, FileName = Path.GetFileName(number + "-" + upload.FileName), }; supplier.FilePath = image; supplier.FilePathId = image.FilePathId; upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/Suppliers"), image.FileName)); // save changes repository.Add(supplier); image.Suppliers.Add(supplier); repository.Save(); return RedirectToAction("Index"); } // return if invalid return View(supplier); }
public ActionResult EditSupplier(EditSupplierViewModel model, HttpPostedFileBase upload) { Supplier supplier = repository.Get(model.Id); FilePath actualImage = repoFilePath.GetAll.Where(c => c.FilePathId == supplier.FilePathId).FirstOrDefault(); // get a path to image on server string actualImagePath = Request.MapPath("~/Content/Images/Suppliers/" + actualImage.FileName); if (ModelState.IsValid) { // check if upload exists // if exists delete old from server, // assign new to color and save on server if (upload != null && upload.ContentLength > 0) { System.IO.File.Delete(actualImagePath); Guid number = Guid.NewGuid(); FilePath photo = new FilePath() { FileType = FileType.supplierImage, FileName = Path.GetFileName(number + "-" + upload.FileName), }; supplier.FilePath = photo; supplier.FilePathId = photo.FilePathId; upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/Suppliers"), photo.FileName)); } // assign properties from model supplier.DeliveryMethodId = model.DeliveryMethodId; supplier.Name = model.Name; supplier.Price = model.Price; supplier.TransportTime = model.TransportTime; // save changes repository.Update(supplier); return RedirectToAction("Index"); } // return if invalid return View(model); }
public ActionResult EditColor(EditColorViewModel model, HttpPostedFileBase upload) { Color color = repository.Get(model.Id); FilePath actualImage = repoFilePath.GetAll.Where(c => c.FilePathId == color.FilePathId).FirstOrDefault(); // assign name from model color.ColorName = model.Name; // get a path to image on server string actualImagePath = Request.MapPath("~/Content/Images/Colors/" + actualImage.FileName); if (ModelState.IsValid) { // check if upload exists // if exists delete old from server, // assign new to color and save on server if (upload != null && upload.ContentLength > 0) { System.IO.File.Delete(actualImagePath); Guid number = Guid.NewGuid(); FilePath colorImage = new FilePath { FileType = FileType.colorImage, FileName = Path.GetFileName(number + "-" + upload.FileName) }; color.FilePath = colorImage; color.FilePathId = colorImage.FilePathId; upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/Colors"), colorImage.FileName)); } repository.Update(color); return RedirectToAction("Index"); } // return if invalid return View(model); }
public async Task<ActionResult> Edit(EditProductViewModel model, IEnumerable<HttpPostedFileBase> uploads, string[] selectedSizes) { Product prod = db.Products.Include(i => i.Sizes).Include(p => p.FilePaths).Where(p => p.ProductId == model.Id).Single(); List<FilePath> actualImages = db.FilePaths.Where(p => p.ProductId == model.Id).ToList(); if (ModelState.IsValid) { // check files upload // if exists // delete old images and add new to product images list, // save on server if (uploads != null) { foreach (FilePath file in actualImages) { string filePath = Request.MapPath("~/Content/Images/" + file.FileName); System.IO.File.Delete(filePath); } db.FilePaths.RemoveRange(actualImages); //prod.FilePaths = new List<FilePath>(); foreach (HttpPostedFileBase upload in uploads) { if (upload != null && upload.ContentLength > 0) { FilePath photo = new FilePath { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Photo }; prod.FilePaths.Add(photo); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images"), photo.FileName)); } } } //update sizes UpdateProductSizes(selectedSizes, prod); // calculate product price if (prod.Discount > 0) { prod.Price = model.NormalPrice - model.NormalPrice * ((decimal)model.Discount / 100); } else { prod.Price = model.NormalPrice; } //attach model properties as product properties prod.ProductId = model.Id; prod.ProductName = model.ProductName; prod.Desc = model.Desc; prod.Material = model.Material; prod.NormalPrice = model.NormalPrice; prod.Discount = model.Discount; prod.CategoryId = model.CategoryId; prod.ColorId = model.ColorId; prod.EditDate = DateTime.Now; //save changes db.Entry(prod).State = EntityState.Modified; await db.SaveChangesAsync(); // set confirmation message to Index() View TempData["message"] = string.Format("{0} has been edited", prod.ProductName); return RedirectToAction("Index"); } // return form if invalid return View(model); }
public async Task<ActionResult> Create(Product newproduct, IEnumerable<HttpPostedFileBase> uploads, string[] selectedSizes) { // remake if something went wrong ViewBag.CategoryId = new SelectList(Retriever.GetCategories(), "CategoryId", "CategoryName", null); ViewBag.ColorId = new SelectList(Retriever.GetColors(), "ColorId", "ColorName", null); if (ModelState.IsValid) { // assign sizes to product newproduct.Sizes = new List<Size>(); foreach (string size in selectedSizes) { Size sizeToAdd = db.Sizes.Find(int.Parse(size)); newproduct.Sizes.Add(sizeToAdd); } // set product images list newproduct.FilePaths = new List<FilePath>(); foreach (HttpPostedFileBase upload in uploads) { // check each file upload // if any exists add this to product images list, // save on server if (upload != null && upload.ContentLength > 0) { FilePath photo = new FilePath() { FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName), FileType = FileType.Photo }; newproduct.FilePaths.Add(photo); upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images"), photo.FileName)); } } // calculate product price if (newproduct.Discount > 0) { newproduct.Price = newproduct.NormalPrice - newproduct.NormalPrice * ((decimal)newproduct.Discount / 100); } else { newproduct.Price = newproduct.NormalPrice; } // set create date and save changes newproduct.CreateDate = DateTime.Now; db.Products.Add(newproduct); await db.SaveChangesAsync(); // set confirmation message to Index() View TempData["message"] = string.Format("{0} has been created.", newproduct.ProductName); return RedirectToAction("Index"); } // return if invalid return View(newproduct); }