public ActionResult Create()
 {
     var username = GetUserName();
     var lendingViewModel = new LendingViewModel();
     SetBaseFieldsOn(lendingViewModel, username);
     return View(lendingViewModel);
 }
 public ActionResult Create(LendingViewModel lendingViewModel)
 {
     if (!ModelState.IsValid) return View(lendingViewModel);
     var lendingEntry = _mappingEngine.Map<LendingViewModel, Lending>(lendingViewModel);
     SetItemOn(lendingViewModel, lendingEntry);
     SetPersonOn(lendingViewModel, lendingEntry);
     _lendingRepository.Save(lendingEntry);
     return RedirectToAction("Index");
 }
        public void Edit_POST_GivenModelStateIsValid_ShouldCallGetByIdFromLendingsRepo()
        {
            //---------------Set up test pack-------------------
            var lendingsViewModel = new LendingViewModel();
            var mappingEngine = _container.Resolve<IMappingEngine>();
            var lendingRepository = Substitute.For<ILendingRepository>();

            var controller = CreateLendingController(lendingRepository, mappingEngine);

            //---------------Assert Precondition----------------
            Assert.IsTrue(controller.ModelState.IsValid);
            //---------------Execute Test ----------------------
            var result = controller.Edit(lendingsViewModel);
            //---------------Test Result -----------------------
            lendingRepository.Received().GetById(lendingsViewModel.Id);
        }
        public void Create_POST_GivenModelStateIsValid_ShouldRedirectToIndexPage()
        {
            //---------------Set up test pack-------------------
            var viewModel = new LendingViewModel();
            var mappingEngine = _container.Resolve<IMappingEngine>();
            var lendingRepository = Substitute.For<ILendingRepository>();
            var controller = CreateLendingController(lendingRepository, mappingEngine);

            //---------------Assert Precondition----------------
            Assert.IsTrue(controller.ModelState.IsValid);
            //---------------Execute Test ----------------------
            var result = controller.Create(viewModel) as RedirectToRouteResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["Action"]);
        }
        public void Create_POST_GivenModelStateIsValid_ShouldCallSaveOnPersonRepo()
        {
            //---------------Set up test pack-------------------
            var viewModel = new LendingViewModel();
            var mappingEngine = _container.Resolve<IMappingEngine>();
            var lendingRepository = Substitute.For<ILendingRepository>();
            var controller = CreateLendingController(lendingRepository, mappingEngine);

            //---------------Assert Precondition----------------
            Assert.IsTrue(controller.ModelState.IsValid);
            //---------------Execute Test ----------------------
            var result = controller.Create(viewModel);
            //---------------Test Result -----------------------
            lendingRepository.Received(1).Save(Arg.Any<Lending>());
        }
 public void Edit_POST_GivenModelStateIsInvalid_ShouldReturnViewWithViewLendingViewModel()
 {
     //---------------Set up test pack-------------------
     var viewModel = new LendingViewModel();
     var controller = CreateLendingController().Build();
    controller.ModelState.AddModelError("key", "error message");
     //---------------Assert Precondition----------------
     Assert.IsFalse(controller.ModelState.IsValid);
     //---------------Execute Test ----------------------
     var result = controller.Edit(viewModel) as ViewResult;
     //---------------Test Result -----------------------
     Assert.IsNotNull(result);
     var model = result.Model;
     Assert.IsNotNull(model);
     Assert.IsInstanceOf<LendingViewModel>(model);
 }
 public void Edit_POST_GivenModelStateIsValid_ShouldRedirectToLendingsIndexPage()
 {
     //---------------Set up test pack-------------------
     var viewModel = new LendingViewModel();
     var controller = CreateLendingController().Build();
     //---------------Assert Precondition----------------
     Assert.IsTrue(controller.ModelState.IsValid);
     //---------------Execute Test ----------------------
     var result = controller.Edit(viewModel) as RedirectToRouteResult;
     //---------------Test Result -----------------------
     Assert.IsNotNull(result);
     Assert.AreEqual("Index", result.RouteValues["Action"]);
 }
 public void Edit_POST_GivenModelStateIsValid_ShouldCallUpdateFromLendingsRepo()
 {
     //---------------Set up test pack-------------------
     var viewModel = new LendingViewModel();
     var lendingRepository = Substitute.For<ILendingRepository>();
     var mappingEngine = _container.Resolve<IMappingEngine>();
     var itemsController = CreateLendingController()
                         .WithLendingRepository(lendingRepository)
                         .WithMappingEngine(mappingEngine)
                         .Build();
     //---------------Assert Precondition----------------
     Assert.IsTrue(itemsController.ModelState.IsValid);
     //---------------Execute Test ----------------------
     var result = itemsController.Edit(viewModel);
     //---------------Test Result -----------------------
     lendingRepository.Received().Update(Arg.Any<Lending>(), Arg.Any<Lending>());
 }
 private void UpdateBaseFieldsOn(LendingViewModel lendingViewModel)
 {
     lendingViewModel.DateLastModified = DateTimeProvider.Now;
     lendingViewModel.LastModifiedUsername = GetUserName();
 }
 private void SetPersonOn(LendingViewModel viewModel, Lending lending)
 {
     if (viewModel.PersonId != Guid.Empty)
     {
         var person = _personRepository.GetById(viewModel.PersonId);
         lending.PersonName = person.FirstName;
     }
 }
 private void SetItemOn(LendingViewModel viewModel, Lending lending)
 {
     if (viewModel.ItemId == Guid.Empty) return;
     var item = _itemsRepository.GetById(viewModel.ItemId);
     lending.ItemName = item.ItemName;
 }
        private SelectList GetItemsSelectList(LendingViewModel viewModel)
        {
            var list = _itemsRepository.GetAllItems();
            list = list ?? new List<Item>();
            var listItems = list.Select(p => new SelectListItem
            {
                Text = p.ItemName,
                Value = p.ItemId.ToString()

            });
            return new SelectList(listItems, "Value", "Text", viewModel.ItemId);
        }
 private SelectList GetPersonSelectList(LendingViewModel viewModel)
 {
     var people = _personRepository.GetAllPeople();
     people = people ?? new List<Person>();
     var listItems = people.Select(p => new SelectListItem
     {
         Text = p.FirstName,
         Value = p.PersonId.ToString()
     });
     return new SelectList(listItems, "Value", "Text", viewModel.PersonId);
 }
        public void Edit_POST_GivenModelStateIsValid_ShouldCallMappingEngine()
        {
            //---------------Set up test pack-------------------
            var viewModel = new LendingViewModel();
            var mappingEngine = Substitute.For<IMappingEngine>();

            var controller = CreateLendingController(null, mappingEngine);
            //---------------Assert Precondition----------------
            Assert.IsTrue(controller.ModelState.IsValid);
            //---------------Execute Test ----------------------
            var result = controller.Edit(viewModel);
            //---------------Test Result -----------------------
            mappingEngine.Received().Map<LendingViewModel, Lending>(viewModel);
        }
        public ActionResult Edit(LendingViewModel lendingViewModel)
        {
            if (ModelState.IsValid)
            {
                var existingItem = _lendingRepository.GetById(lendingViewModel.Id);
                UpdateBaseFieldsOn(lendingViewModel);
                var newItem = _mappingEngine.Map<LendingViewModel, Lending>(lendingViewModel);
                SetItemOn(lendingViewModel, newItem);
                SetPersonOn(lendingViewModel, newItem);
                _lendingRepository.Update(existingItem, newItem);

                return RedirectToAction("Index");
            }
            return View(lendingViewModel);
        }
        public void Edit_POST_GivenItemIdIsEmpty_ShouldNotCallGetByIdFromItemsRepo()
        {
            //---------------Set up test pack-------------------
            var viewModel = new LendingViewModel();
            var itemsRepository = Substitute.For<IItemsRepository>();
            var mappingEngine = _container.Resolve<IMappingEngine>();
            var controller = CreateLendingController()
                .WithItemsRepository(itemsRepository)
                .WithMappingEngine(mappingEngine)
                .Build();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var results = controller.Edit(viewModel);
            //---------------Test Result -----------------------
            itemsRepository.DidNotReceive().GetById(viewModel.ItemId);
        }
 private void SetBaseFieldsOn(LendingViewModel lendingViewModel, string username)
 {
     lendingViewModel.PeopleSelectList = GetPersonSelectList(lendingViewModel);
     lendingViewModel.ItemsSelectList = GetItemsSelectList(lendingViewModel);
     lendingViewModel.CreatedUsername = username;
     lendingViewModel.DateCreated = DateTimeProvider.Now;
     lendingViewModel.LastModifiedUsername = username;
     lendingViewModel.DateLastModified = DateTimeProvider.Now;
 }