public static ProductViewModel CreateFromProduct(Product product) { return new ProductViewModel() { Id = product.Id, Title = product.Title, Category = product.Category.Name, Price = product.Price, Description = product.Description, ImageUrl = product.ImageUrl, Condition = product.Condition, StartingPrice =product.StartingPrice, DateAdded = product.DateAdded }; }
public void EditMethod_ShouldReturnProperProduct() { Product product = new Product() { Id = Guid.NewGuid(), Title = "test", Description = "1234567891011121212512255621dfsdfsd", Price = 10, StartingPrice = 5, DateAdded = DateTime.Now, Category = new Category { Id = Guid.NewGuid(), Name = "dsfdsfsfdsfsfs" } }; Guid guid1 = new Guid("50d3ebaa-eea3-453f-8e8b-b835605b3e85"); var productsRepoMock = new Mock<IRepository<Product>>(); var categoriesRepoMock = new Mock<IRepository<Category>>(); productsRepoMock.Setup(x => x.GetById(guid1)).Returns(product); List<Category> list = new List<Category>() { new Category(){Name = "category1", Id = Guid.NewGuid(), }, new Category(){Name = "category2", Id = Guid.NewGuid() } }; categoriesRepoMock.Setup(x => x.All()).Returns(list.AsQueryable()); var uofMock = new Mock<IUnitOfWorkData>(); uofMock.Setup(x => x.Products).Returns(productsRepoMock.Object); uofMock.Setup(x => x.Categories).Returns(categoriesRepoMock.Object); var controller = new ProductsController(uofMock.Object); var viewResult = controller.Edit(guid1) as ViewResult; Assert.IsNotNull(viewResult, "Index action returns null."); var model = viewResult.Model as Product; Assert.IsNotNull(model, "The model is null."); Assert.AreEqual(10, model.Price); Assert.AreEqual(5, model.StartingPrice); Assert.AreEqual("test", model.Title); Assert.AreSame(product, model); }
public void IndexMethodShouldReturn1Product() { Product product = new Product() { Id = Guid.NewGuid(), Title = "test", Description = "1234567891011121212512255621dfsdfsd", Price = 10, StartingPrice = 5, DateAdded = DateTime.Now, ImageUrl = "test", Category = new Category { Id = Guid.NewGuid(), Name = "dsfdsfsfdsfsfs" } }; Auction auction = new Auction() { Id = Guid.NewGuid(), DateStarted = DateTime.Now, Duration = 12, Type = AuctionType.Auction, Product = product, DeliveryDuration = 123, CurrentPrice = 123, }; var list = new List<Auction>(); list.Add(auction); var bugsRepoMock = new Mock<IRepository<Auction>>(); bugsRepoMock.Setup(x => x.All()).Returns(list.AsQueryable()); var uofMock = new Mock<IUnitOfWorkData>(); uofMock.Setup(x => x.Auctions).Returns(bugsRepoMock.Object); var controller = new HomeController(uofMock.Object); var viewResult = controller.Index() as ViewResult; Assert.IsNotNull(viewResult, "Index action returns null."); var model = viewResult.Model as IList<ProductViewModel>; Assert.IsNotNull(model, "The model is null."); Assert.AreEqual(1, model.Count()); Assert.AreEqual(product.Description, model[0].Description); }
public ActionResult Create(AddProductViewModel product) { if (product.StartingPrice >= product.Price) { ModelState.AddModelError("StartingPrice", new ArgumentOutOfRangeException("Starting price should be less than product price.")); } if (ModelState.IsValid) { var cat = db.Categories.GetById(product.Category.Id); string ownerId = User.Identity.GetUserId(); ApplicationUser owner = this.db.Users.All().FirstOrDefault(user => user.Id == ownerId); if (product.ImageUrl == null) { product.ImageUrl = "default-product-image.jpg"; } var newProduct = new Product { Id = Guid.NewGuid(), ImageUrl = product.ImageUrl, Category = cat, DateAdded = DateTime.Now, CategoryId = product.Category.Id, // (yoan) should it be : cat.id Condition = product.Condition, Description = product.Description, Price = Convert.ToDecimal(product.Price), StartingPrice = Convert.ToDecimal(product.StartingPrice), Title = product.Title, State = ProductState.ForSelling, Owner = owner, OwnerId = ownerId }; db.Products.Add(newProduct); db.SaveChanges(); var newAuction = new Auction { Id = Guid.NewGuid(), DateStarted = DateTime.Now, Duration = product.AuctionTime, Product = newProduct, Type = product.AuctionType, DeliveryDuration = product.DeliveryTime, }; if (product.AuctionType == AuctionType.Normal) { newAuction.CurrentPrice = product.Price; } else { newAuction.CurrentPrice = product.StartingPrice; } this.db.Auctions.Add(newAuction); this.db.SaveChanges(); return RedirectToAction("Details", "Products", new { id = newProduct.Id }); } ViewBag.Categories = db.Categories.All().ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); return View(product); }
public ActionResult Edit(Product product) { if (ModelState.IsValid) { product.CategoryId = product.Category.Id; db.Products.Update(product); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ProductId = product.Id; return View(product); }