public ActionResult Create(ItemViewModel itemViewModel)
 {
     if (ModelState.IsValid)
     {
         SetBaseFieldsOn(itemViewModel);
         var item = _mappingEngine.Map<ItemViewModel, Item>(itemViewModel);
         _itemsRepository.Save(item);
         return RedirectToAction("Index", "Items");
     }
     return View(itemViewModel);
 }
        public ActionResult Edit(ItemViewModel itemViewModel)
        {
            if (ModelState.IsValid)
            {
                UpdateBaseFieldOn(itemViewModel);
                var existingItem = _itemsRepository.GetById(itemViewModel.Id);
                var newItem = _mappingEngine.Map<ItemViewModel, Item>(itemViewModel);
                _itemsRepository.Update(existingItem, newItem);

                return RedirectToAction("Index");
            }
            return View(itemViewModel);
        }
 private void UpdateBaseFieldOn(ItemViewModel itemViewModel)
 {
     itemViewModel.DateLastModified = DateTimeProvider.Now;
     itemViewModel.LastModifiedUsername = GetUserName();
 }
 //test this
 private void SetBaseFieldsOn(ItemViewModel itemViewModel)
 {
     itemViewModel.CreatedUsername = GetUserName();
     itemViewModel.LastModifiedUsername = GetUserName();
     itemViewModel.DateCreated = DateTimeProvider.Now;
     itemViewModel.DateLastModified = DateTimeProvider.Now;
 }
        public void Create_POST_GivenModelStateIsInvalid_ShouldReturnViewWithItemsViewModel()
        {
            //---------------Set up test pack-------------------
            var itemsViewModel = new ItemViewModel();
            var itemsController = CreateItemsControllerBuilder().Build();

            itemsController.ModelState.AddModelError("key", "some error");
            //---------------Assert Precondition----------------
            Assert.IsFalse(itemsController.ModelState.IsValid);
            //---------------Execute Test ----------------------
            var result = itemsController.Create(itemsViewModel) as ViewResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            var model = result.Model;
            Assert.AreEqual(itemsViewModel, model);
        }
        public void Create_POST_GivenModelStateIsValid_ShouldRedirectToItemsIndexPage()
        {
            //---------------Set up test pack-------------------
            var itemsViewModel = new ItemViewModel();

            var itemsController = CreateItemsControllerBuilder().Build();
            //---------------Assert Precondition----------------
            Assert.IsTrue(itemsController.ModelState.IsValid);
            //---------------Execute Test ----------------------
            var result = itemsController.Create(itemsViewModel) as RedirectToRouteResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["Action"]);
            Assert.AreEqual("Items", result.RouteValues["Controller"]);
        }
        public void Create_POST_GivenModelStateIsValid_ShouldCallMappingEngine()
        {
            //---------------Set up test pack-------------------
            var mappingEngine = Substitute.For<IMappingEngine>();
            var itemsViewModel = new ItemViewModel();

            var itemsController = CreateItemsControllerBuilder()
                .WithMappingEngine(mappingEngine)
                .Build();
            //---------------Assert Precondition----------------
            Assert.IsTrue(itemsController.ModelState.IsValid);
            //---------------Execute Test ----------------------
            var result = itemsController.Create(itemsViewModel) as ViewResult;
            //---------------Test Result -----------------------
            mappingEngine.Received(1).Map<ItemViewModel, Item>(itemsViewModel);
        }