public void AddToWishListTest() { // Arrange var expected = new WishListItemDto { Id = "1", BookId = "1", Book = new Book() { Id = "1", Title = "Stephen", Description = "King" }, UserId = "1", User = new UserProfile() { Id = "1", FirstName = "Oleksii", LastName = "Rudenko" }, Note = "Best book ever" }; var repository = new Mock <IRepository <WishListItem> >(); repository.Setup(r => r.Get(expected.Id)) .Returns(new WishListItem { Id = "1", BookId = "1", Book = new Book() { Id = "1", Title = "Stephen", Description = "King" }, UserId = "1", User = new UserProfile() { Id = "1", FirstName = "Oleksii", LastName = "Rudenko" }, Note = "Best book ever" }); var mapper = new Mock <IMapper>(); mapper.Setup(m => m.Map <WishListItemDto, WishListItem>(It.IsAny <WishListItemDto>())).Returns(new WishListItem()); var svc = new WishListService(repository.Object, mapper.Object); // Act svc.AddToWishList(expected); // Assert mapper.Verify(m => m.Map <WishListItemDto, WishListItem>(It.IsAny <WishListItemDto>()), Times.Once()); repository.Verify(r => r.Create(It.IsAny <WishListItem>()), Times.Once()); repository.Verify(r => r.Save(), Times.Once()); }
public void AddToWishList(WishListItemDto wishListItemDto) { var exists = _repository.Get(i => i.BookId == wishListItemDto.BookId && i.UserId == wishListItemDto.UserId).Any(); if (exists) { return; } var item = _mapper.Map <WishListItemDto, WishListItem>(wishListItemDto); item.TimeStamp = DateTime.Now; _repository.Create(item); _repository.Save(); }
public IActionResult RemoveFromWishList(string id) { try { var bookDto = _bookService.Get(id); var wlDto = new WishListItemDto { BookId = bookDto.Id, UserId = User.GetUserId() }; _service.DeleteFromWishList(wlDto); return(Redirect($"/Books/Details/{id}")); } catch (Exception e) { return(View("Error", new ErrorViewModel { RequestId = Request.HttpContext.TraceIdentifier, Exception = e })); } }
public IActionResult AddToWishListConfirmed(string id, string note) { try { var bookDto = _bookService.Get(id); var wlDto = new WishListItemDto { BookId = bookDto.Id, UserId = User.GetUserId(), Note = note }; _service.AddToWishList(wlDto); return(Redirect("/WishList")); } catch (Exception e) { return(View("Error", new ErrorViewModel { RequestId = Request.HttpContext.TraceIdentifier, Exception = e })); } }