public ActionResult Details(int id)
 {
     var itemDto = _itemRepository.GetItemById(id);
     var mapper = new ItemMapper();
     var item = mapper.MapItem(itemDto);
     return View(item);
 }
 public ActionResult Index()
 {
     var dtoList = _itemRepository.GetItemList();
     var mapper = new ItemMapper();
     var list = mapper.MapItemList(dtoList);
     return View(list);
 }
 public ActionResult Create(ItemViewModel item)
 {
     try
     {
         var itemRepo = _itemRepository;
         var mapper = new ItemMapper();
         var dto = mapper.MapItemDto(item);
         itemRepo.Add(dto);
         return RedirectToAction("Index");
     }
     catch(Exception e)
     {
         return View(e);
     }
 }
 public void MapItem_GivenItemDTOByName_ShouldReturnAnItem()
 {
     //---------------Set up test pack-------------------
     var itemDto = new ItemDto()
     {
         Id = 2,
         Description = "Description two",
         ItemName = "ItemDTO name2"
     };
     var mapper = new ItemMapper();
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     ItemViewModel item = mapper.MapItem(itemDto);
     //---------------Test Result -----------------------
     Assert.AreEqual(itemDto.ItemName, item.Name);
 }
 public void MapItem_GivenItemDTO_ShouldReturnAnItem()
 {
     //---------------Set up test pack-------------------
     var itemDTO = new ItemDto()
     {
         Id = 1,
         Description = "First item DTO",
         ItemName = "ItemDTO name1"
     };
     var mapper = new ItemMapper();
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     ItemViewModel item = mapper.MapItem(itemDTO);
     //---------------Test Result -----------------------
     Assert.AreEqual(itemDTO.Id, item.Id);
 }
        public ActionResult Edit(ItemViewModel item)
        {
            try
            {
                var map = new ItemMapper();
                var itemDto = map.MapItemDto(item);
                _itemRepository.Update(itemDto);

                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                var x = e;
                return View();
            }

        }
        public void Index_GivenRepositoryHasItem_ReturnsViewWithListOfItems()
        {
            var repo = Substitute.For<IItemRepository>();
            var dtoList = new List<ItemDto>();
            repo.GetItemList().Returns(dtoList);
            //---------------Set up test pack-------------------
            ItemController controller = new ItemController(repo);
            //---------------Execute Test ----------------------
            ViewResult result = controller.Index() as ViewResult;
            //---------------Test Result -----------------------

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Model as IEnumerable);

            var map = new ItemMapper();
            var itemList = map.MapItemList(dtoList);

            CollectionAssert.AreEqual((IEnumerable)result.Model, itemList);

        }
        public void Details_GivenItemId_ShouldReturnViewWithItemDetails()
        {
            //---------------Set up test pack-------------------
            var repo = Substitute.For<IItemRepository>();
            var itemDto = new ItemDto {Id = 2, ItemName = "FakeName"};
            
            repo.GetItemById(itemDto.Id).Returns(itemDto);
           
            var controller = new ItemController(repo);
            //---------------Execute Test ----------------------
            const int expectedId = 2;
            var viewResult = controller.Details(expectedId) as ViewResult;
            //---------------Test Result -----------------------
            var resultItem = (ItemViewModel) viewResult?.ViewData.Model;

            Assert.IsNotNull(resultItem);

            var map = new ItemMapper();
            var expectedItem = map.MapItem(itemDto);

            Assert.AreSame(expectedItem.Name, resultItem.Name);

        }
 public void MapItemList_GivenItemDTOAsList_ShouldReturnAnItemList()
 {
     //---------------Set up test pack-------------------
     var itemDto = new ItemDto()
     {
         Id = 2,
         Description = "Description two",
         ItemName = "ItemDTO name2"
     };
     var itemDto1 = new ItemDto()
     {
         Id = 3,
         Description = "Description three",
         ItemName = "ItemDTO name3"
     };
     var itemDTOList = new List<ItemDto>()
     {
         itemDto,
         itemDto1
     };
     var mapper = new ItemMapper();
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     List<ItemViewModel> itemList = mapper.MapItemList(itemDTOList);
     //---------------Test Result -----------------------
     
     Assert.AreEqual(itemDTOList.Count, itemList.Count);
 }
        public ActionResult Delete(int id)
        {
            var itemDto = _itemRepository.GetItemById(id);
            if (itemDto == null)
            {
                return View();
            }
            var mapper = new ItemMapper();
            var item = mapper.MapItem(itemDto);

            return View(item);
        }