public ActionResult Edit(BorrowerItemViewModel viewModel)
        {
            try
            {
                var borrowersItem = _mappingEngine.Map<BorrowersItem>(viewModel);
                _borrowerItemRepository.Save(borrowersItem);

                return RedirectToAction("Index");
            }
            catch
            {
                //Load selectlists
                return View();
            }
        }
        public ActionResult Delete(BorrowerItemViewModel viewModel)
        {
            try
            {
                var borrowersItem = _mappingEngine.Map<BorrowersItem>(viewModel);
                _borrowerItemRepository.Delete(borrowersItem);


                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
 private void SetSelectListToDefaults(BorrowerItemViewModel viewModel)
 {
     viewModel.ItemSelectListItems = GetAllItems();
     viewModel.BorrowersSelectListItems = GetAllBorrowers();
 }
        public ActionResult Create(BorrowerItemViewModel viewModel)
        {
                if (ModelState.IsValid)
                {
                var borrowersItem = _mappingEngine.Map<BorrowersItem>(viewModel);

                var itemId = _itemRepository.Get(viewModel.ItemId);
                borrowersItem.Item = itemId;
                var borrowerId = _borrowerRepository.Get(viewModel.BorrowerId);
                borrowersItem.Borrower = borrowerId;
                borrowersItem.DateBorrowed = viewModel.DateBorrowed;
                borrowersItem.DateReturned = "";
                    
                    _borrowerItemRepository.Save(borrowersItem);
                    return RedirectToAction("Index");
                }
                 SetSelectListToDefaults(viewModel);
                return View();
            
        }
        // GET: BorrowerItem/Create
        public ActionResult Create()
        {
            var viewModel = new BorrowerItemViewModel
            {
                ItemSelectListItems = GetAllItemsNotLent(),
                BorrowersSelectListItems=GetAllBorrowers(),
                DateBorrowed = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd"))
           };

            return View(viewModel);
        }
        public void DeletePOST_GivenBorrowerItemViewModel_ShouldMapToBorrowerAndDelete()

        {
            var borrowerItems = new BorrowerItemBuilder().WithRandomProps().Build();
            
            var repository = Substitute.For<IBorrowerItemRepository>();
            var itemRepository = Substitute.For<IItemRepository>();
            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            var mapper = Substitute.For<IMappingEngine>();
            
            var borrowerItemViewModel = new BorrowerItemViewModel
            {
                Id = RandomValueGen.GetRandomInt(),
                ItemId = RandomValueGen.GetRandomInt(),
                BorrowerId = RandomValueGen.GetRandomInt()
            };

            mapper.Map<BorrowersItem>(borrowerItemViewModel).Returns(borrowerItems);
            repository.Delete(borrowerItems);
            var borrowerItemController = CreateBuilder()
                .WithBorrowerItemRepository(repository)
                .WithBorrowerRepository(borrowerRepository)
                .WithItemRepository(itemRepository)
                .WithMappingEngine(mapper)
                .Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = borrowerItemController.Delete(borrowerItemViewModel);
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            repository.Received().Delete(borrowerItems);
          

        }
        public void EditPOST_GivenValidBorrowerId_ShouldCallSaveandRedirect()

        {
            var borrowerItems = new BorrowerItemBuilder().WithRandomProps().Build();
            
            var repository = Substitute.For<IBorrowerItemRepository>();
            var itemRepository = Substitute.For<IItemRepository>();
            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            var mapper = Substitute.For<IMappingEngine>();
            
            var borrowerItemViewModel = new BorrowerItemViewModel
            {
                Id = RandomValueGen.GetRandomInt(),
                ItemId = RandomValueGen.GetRandomInt(),
                BorrowerId = RandomValueGen.GetRandomInt()
            };

            mapper.Map<BorrowersItem>(borrowerItemViewModel).Returns(borrowerItems);
            repository.Save(borrowerItems);
            var borrowerItemController = CreateBuilder()
                .WithBorrowerItemRepository(repository)
                .WithBorrowerRepository(borrowerRepository)
                .WithItemRepository(itemRepository)
                .WithMappingEngine(mapper)
                .Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = borrowerItemController.Edit(borrowerItemViewModel) as RedirectToRouteResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            repository.Received().Save(borrowerItems);
            var actionName = result.RouteValues["action"];
            Assert.AreEqual("Index", actionName);

        }
        public void EditGET_GivenValidBorrowerItemId_ShouldReturnBorrowerViewModel()
        {
            //---------------Set up test pack-------------------
            var borrowerItems= new BorrowerItemBuilder().WithRandomProps().Build();
            var id = borrowerItems.Id;

            var repository = Substitute.For<IBorrowerItemRepository>();
            var itemRepository = Substitute.For<IItemRepository>();
            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            var mapper = Substitute.For<IMappingEngine>();

            repository.Get(id).Returns(borrowerItems);

            var item = new Item { Id = RandomValueGen.GetRandomInt(), Description = RandomValueGen.GetRandomString() };
            var items = new List<Item> { item };
            var borrower = new BorrowerBuilder().WithRandomProps().Build();
            var borrowers = new List<Borrower> { borrower };


            itemRepository.GetAll().Returns(items);
            borrowerRepository.GetAll().Returns(borrowers);
          

            
            var borrowerItemViewModel = new BorrowerItemViewModel { Id = RandomValueGen.GetRandomInt() };
            
            mapper.Map<BorrowerItemViewModel>(borrowerItems).Returns(borrowerItemViewModel);

            var borrowerItemController = CreateBuilder()
                .WithBorrowerItemRepository(repository)
                .WithBorrowerRepository(borrowerRepository)
                .WithItemRepository(itemRepository)
                .WithMappingEngine(mapper)
                .Build();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = borrowerItemController.Edit(id) as ViewResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            var model = result.Model as BorrowerItemViewModel;
            Assert.IsNotNull(model);
            Assert.AreEqual(1, model.BorrowersSelectListItems.Count);
            Assert.AreEqual(1, model.ItemSelectListItems.Count);
        }
        public void Create_POST_ShouldCallSaveAndRedirectToIndex()
        {
            //---------------Set up test pack-------------------
            var borrowerItemRepository = Substitute.For<IBorrowerItemRepository>();
            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            var itemRepository = Substitute.For<IItemRepository>();
            var mapper = Substitute.For<IMappingEngine>();

            var borrowersItem = new BorrowerItemBuilder().WithRandomProps().Build();
            var borrower = new BorrowerBuilder().WithRandomProps().Build();
            var item = new ItemBuilder().WithRandomProps().Build();

            var borrowerItemViewModel = new BorrowerItemViewModel();

            mapper.Map<BorrowersItem>(borrowerItemViewModel).Returns(borrowersItem);

            itemRepository.Get(borrowerItemViewModel.ItemId).Returns(item);

            borrowersItem.ItemId = item.Id;
            borrowersItem.BorrowerId = borrower.Id;
            borrowerRepository.Get(borrowerItemViewModel.BorrowerId).Returns(borrower);

            borrowerItemViewModel.ItemId = item.Id;
            borrowerItemViewModel.BorrowerId = borrower.Id;
            borrowerItemViewModel.DateBorrowed=DateTime.Now;
            borrowerItemViewModel.DateReturned=DateTime.Now.ToString();

           
            
            var borrowerItemController = CreateBuilder()
                   .WithBorrowerItemRepository(borrowerItemRepository)
                   .WithBorrowerRepository(borrowerRepository)
                   .WithItemRepository(itemRepository)
                   .WithMappingEngine(mapper)
                   .Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = borrowerItemController.Create(borrowerItemViewModel) as RedirectToRouteResult;
            //---------------Test Result -----------------------
            borrowerItemRepository.Received().Save(borrowersItem);
            Assert.IsNotNull(result);
            var actionName = result.RouteValues["action"];
            Assert.AreEqual("Index", actionName);
        }