public void CanDeleteItem() { Item itemToAdd = new Item(); var repo = new PirateItemRepository(); itemToAdd.ItemName = "Test Item"; itemToAdd.RealValue = 15000; itemToAdd.DisplayValue = 20000; itemToAdd.Description = "This is a test item"; itemToAdd.Favorite = true; itemToAdd.Featured = true; itemToAdd.CategoryName = "Space Goats"; itemToAdd.CurrencyName = "Space Bucks"; itemToAdd.ItemPictureURL = "placeholder.jpg"; repo.Insert(itemToAdd); Assert.AreEqual(9, itemToAdd.ItemId); var loaded = repo.GetItemById(9); Assert.IsNotNull(loaded); Assert.AreEqual(9, loaded.ItemId); repo.Delete(9); loaded = repo.GetItemById(9); Assert.IsNull(loaded); }
public ActionResult Add(ItemAddViewModel model) { if (ModelState.IsValid) { var repo = new PirateItemRepository(); try { var savepath = Server.MapPath("~/Images"); string fileName = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName); string extension = Path.GetExtension(model.ImageUpload.FileName); var filePath = Path.Combine(savepath, fileName + extension); int counter = 1; while (System.IO.File.Exists(filePath)) { filePath = Path.Combine(savepath, fileName + counter.ToString() + extension); counter++; } model.ImageUpload.SaveAs(filePath); model.Item.ItemPictureURL = Path.GetFileName(filePath); repo.Insert(model.Item); return(RedirectToAction("Inventory", "Admin")); } catch (Exception ex) { throw ex; } } else { var currenciesRepo = new CurrencyRepository(); model.Currencies = new SelectList(currenciesRepo.GetAll(), "CurrencyName", "CurrencyName"); var categoriesRepo = new CategoryRepository(); model.Categories = new SelectList(categoriesRepo.GetAll(), "CategoryName", "CategoryName"); return(View(model)); } }