public async Task <IActionResult> Create([Bind("BookID,Title,ImgLink,Author,Owner,Borrower,Written,Availability,AverageRating")] Book book) { string link; if (book.ImgLink == "1") { link = "genericblack"; } else if (book.ImgLink == "1") { link = "genericblue"; } else if (book.ImgLink == "1") { link = "genericred"; } else { link = "genericgreen"; } book.ImgLink = link; if (ModelState.IsValid) { AppUser user = await userManager.GetUserAsync(HttpContext.User); book.Owner = user.UserName; bookRepo.AddBook(book); _context.Add(book); _context.Update(user); //await _context.SaveChangesAsync(); return(RedirectToAction("Index", user)); } return(View(book)); }
public void TestAddBook() { //Arrange repo = new FakeBookRepo(); SeedData(); Book book = new Book() { Title = "The Lightning Thief", Author = "Rick Riordan", Written = new DateTime(2005, 06, 28), Owner = users[1].UserName, Availability = true, AverageRating = 5, ImgLink = "genericblack" }; // Act repo.AddBook(book); // Assert Assert.Single(repo.Books); Assert.Equal("The Lightning Thief", repo.Books.ElementAt(repo.Books.Count() - 1).Title); Assert.Equal(users[1].UserName, repo.Books.ElementAt(repo.Books.Count() - 1).Owner); }
public async Task <IActionResult> AddBook(BookViewModel model) { if (!ModelState.IsValid) { return(View(model)); } await _bookRepo.AddBook(new Book { Name = model.Name, Author = model.Author }); return(RedirectToAction("Index")); }
public ActionResult Post(Book book) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } Book _book = _bookRepo.AddBook(book); if (_book == null) { return(Conflict()); } return(Ok(_book)); }
public void TestCheckForBookByTitle() { //Arrange repo = new FakeBookRepo(); SeedBookData(); foreach (Book b in books) { repo.AddBook(b); } //Act bool hp = repo.CheckForBookByTitle("Harry Potter and the Sorceror's Stone"); bool pj = repo.CheckForBookByTitle("The Heroes of Olympus"); //Assert Assert.True(hp); Assert.False(pj); }
public ActionResult Create([Bind(Include = "Isbn,Title,Author,Page,Publisher,PublicationYear,Available,CategoryId")] Book book) { if (ModelState.IsValid) { try { _repo.AddBook(book); _repo.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View(book)); } } ViewBag.CategoryId = new SelectList(_context.Category, "Id", "Name", book.CategoryId); return(View(book)); }
public void TestGetBookByTitle() { //Arrange repo = new FakeBookRepo(); SeedBookData(); foreach (Book b in books) { repo.AddBook(b); } //Act Book hp = repo.GetBookByTitle("Harry Potter and the Sorceror's Stone"); Book pj = repo.GetBookByTitle("The Lightning Thief"); //Assert Assert.Equal(books[0], pj); Assert.NotEqual(books[1], pj); Assert.Equal(books[1], hp); Assert.NotEqual(books[0], hp); }