public ActionResult Create([Bind(Include = "ItemID,Name,CategoryID,Price,Blurb,Picture")] Item item, HttpPostedFileBase imageFile) { if (ModelState.IsValid) { Category category = db.Categories.Single(c => c.CategoryID == item.CategoryID); db.Items.Add(item); db.SaveChanges(); if (imageFile != null) { string imageName = System.IO.Path.GetFileName(imageFile.FileName); var imagePath = new DisplayImage(DisplayImage.ImageCategory.Clothing); // Property not constructed by ASP.NET item.Category = db.Categories.Single(c => c.CategoryID == item.CategoryID); string path = System.IO.Path.Combine( Server.MapPath("~/" + imagePath .GetPath(item, false)), imageName); imageFile.SaveAs(path); } return RedirectToAction("Index"); } ViewBag.CategoryID = SelectListHelper.GetCategoryList(item.Category, false); return View(item); }
public string GetPartialPath(Category category, DisplayImage.ImageCategory imgCategory) { if (imgCategory == ImageCategory.Clothing) { string basePath = AppImageRoot + GetCategoryDir(); string genderDir = "Neutral/"; if (category.HasGender) { bool? isMale = category.IsMale; if (!isMale.HasValue) throw new ArgumentNullException("Invalid Gender"); else { if ((bool)category.IsMale) genderDir = "Male"; else genderDir = "Female"; } } string path = basePath; path += genderDir + "/"; path += category.AgeGroup.Name + "/"; path += category.CategoryName.Name + "/"; return path; } else throw new NotImplementedException("Image Category Not Supported"); }
public ActionResult Edit([Bind(Include = "ItemID,Name,CategoryID,Price,Blurb,Picture")] Item item, HttpPostedFileBase imageFile, String OriginalPicture) { item.Category = db.Categories.Single(c => c.CategoryID == item.CategoryID); if (ModelState.IsValid) { string imageName; string originalImagePath; if (imageFile != null) { imageName = System.IO.Path.GetFileName(imageFile.FileName); var displayImage = new DisplayImage(DisplayImage.ImageCategory.Clothing); originalImagePath = displayImage.GetPath(item, true); string originalImageSystemPath = Server.MapPath("~/" + originalImagePath); var bitmap = new Bitmap(imageFile.InputStream); if (displayImage.ResizeImage(bitmap)) { bool uniqueNameGenerated = false; string newImagePartialPath = ""; while (!uniqueNameGenerated) { item.Picture = displayImage.GenerateImageName(8, item.Picture); if (!displayImage.ImageExists(item.Picture, item.Category, DisplayImage.ImageCategory.Clothing, out newImagePartialPath)) uniqueNameGenerated = true; } string newImageSystemPath = System.IO.Path.Combine( Server.MapPath("~/" + newImagePartialPath), item.Picture); ImageConverter byteArrayConverter = new ImageConverter(); System.IO.File.WriteAllBytes(newImageSystemPath, (byte[])byteArrayConverter.ConvertTo(bitmap, typeof(byte[]))); System.IO.File.Delete(originalImageSystemPath); } else { ViewBag.BadImageMessage = "The image must be Square (Height = Width)."; return View(item); } } db.Entry(item).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(item); }
public bool ImageExists(string fileName, Category category, DisplayImage.ImageCategory imgCategory, out String partialPath) { bool returnVal = false; string baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory; partialPath = GetPartialPath(category, imgCategory); string pathSys = partialPath.Replace("/", "\\"); string fileSys = System.IO.Path.Combine(baseDirectory, pathSys, fileName); if (System.IO.File.Exists(fileSys)) returnVal = true; return returnVal; }
public ActionResult Index() { var shoppingCart = ShoppingCart.GetCart(this.HttpContext); string cartId = shoppingCart.GetCartId(this.HttpContext); var viewModel = new ShoppingCartViewModel(); var carts = webStoreDb.Carts.Where(c => c.CartId == cartId); List<CartDetail> details = new List<CartDetail>(); decimal cartTotal = 0; //bool insufficientInventory = false; var alertMessages = new List<string>(); var displayImage = new DisplayImage(DisplayImage.ImageCategory.Clothing); foreach (Cart cart in carts) { Item item = mainDb.Items.Single(i => i.ItemID == cart.ItemId); Inventory inventory = mainDb.Inventories.Single(i => i.InventoryID == cart.InventoryId); InventoryDetailGenerator inventoryDetailGenerator = new InventoryDetailGenerator(); List<InventoryDetail> inventoryDetails = inventoryDetailGenerator.GetInventoryDetails(inventory); decimal cartDetailTotal = cart.Price * cart.Count; bool insufficientInventoryItem = false; if (inventory.QuantityInStock < cart.Count) { insufficientInventoryItem = true; //insufficientInventory = true; alertMessages.Add("Choose new quantity for " + item.Name + "."); } details.Add(new CartDetail { RecordId = cart.RecordId, InventoryId = cart.InventoryId, ItemId = cart.ItemId, SubTotal = cart.Price, Quantity = cart.Count, QuantityInStock = inventory.QuantityInStock, Name = item.Name, Size = inventoryDetailGenerator.GetInventoryDetailString(inventoryDetails, false), Picture = displayImage.GetPath(item, true), Total = cartDetailTotal, InventoryFlag = insufficientInventoryItem } ); cartTotal += cartDetailTotal; } viewModel.Details = details; viewModel.CartId = cartId; viewModel.Total = cartTotal; viewModel.NumItems = shoppingCart.GetCount(); ViewBag.AlertMessages = alertMessages; return View(viewModel); }