public ActionResult SaveBook(AddBookModel addBookModel) { if (ModelState.IsValid) { try { DataTable dt = libraryService.AddBook(addBookModel); if (dt.Rows[0][0].ToString() == "Y") { TempData["Status"] = "Y"; return(RedirectToAction("AddBook")); } else { TempData["Status"] = "N"; return(View("AddBook")); } } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; ModelState.AddModelError("", ex.Message); TempData["Fail"] = "Fail"; return(View("AddBook")); } } else { TempData["ModelsError"] = "Error"; return(RedirectToAction("AddBook")); } }//.End SaveBook
public ValueTask ExecuteAsync(IConsole console) { // To make the demo simpler, we will just generate random publish date and ISBN if they were not set if (Published == default) { Published = CreateRandomDate(); } if (Isbn == default) { Isbn = CreateRandomIsbn(); } if (_libraryService.GetBook(Title) != null) { throw new CommandException("Book already exists.", 1); } var book = new Book(Title, Author, Published, Isbn); _libraryService.AddBook(book); console.Output.WriteLine("Book added."); console.RenderBook(book); return(default);
public void should_fail_if_author_is_null_or_empty() { try { var book = new Book { Author = "test author", Title = "test title" }; Mock <IRedisCacheProvider> _redisProvider = new Mock <IRedisCacheProvider>(); _redisProvider.Setup(x => x.GetNextSequenceForBook()).Returns(2001); _redisProvider.Setup(x => x.SaveBook(It.IsAny <Book>())).Returns(book); _redisProvider.Setup(x => x.GetBookById(It.IsAny <long>())).Returns(book); var libService = new LibraryService(_redisProvider.Object); var bookToInsert = new Book { Id = 0, Author = null, Title = "title", IsAvailable = true }; var result = libService.AddBook(bookToInsert); } catch (Exception e) { Assert.AreEqual("Author cannot be empty", e.Message); } }
public void should_succeed_in_adding_a_book() { try { Mock <IRedisCacheProvider> _redisProvider = new Mock <IRedisCacheProvider>(); _redisProvider.Setup(x => x.GetNextSequenceForBook()).Returns(2001); _redisProvider.Setup(x => x.SaveBook(It.IsAny <Book>())).Returns(new Book()); _redisProvider.Setup(x => x.GetBookById(It.IsAny <long>())).Returns(new Book()); var libService = new LibraryService(_redisProvider.Object); var bookToInsert = new Book { Id = 0, Author = "Author", Title = "Title", IsAvailable = true }; var result = libService.AddBook(bookToInsert); Assert.AreEqual(result.Author, "Author"); } catch (Exception e) { } }
public IActionResult AddBook(BookViewModel book) { if (_service.GetBooksByUser(GetUserId().Result).Contains(book)) { ModelState.AddModelError(nameof(book.Title), "This book is already added!"); } else if (ModelState.IsValid) { if (book.State != BookState.FINISHED && book.YearReaded != default) { ModelState.AddModelError(nameof(book.YearReaded), "Can't fill 'Year Readed' when the book is not finished!"); } else if (book.State == BookState.FINISHED && book.YearPublished > book.YearReaded) { ModelState.AddModelError(nameof(book.YearReaded), "You can't read a book until is not published. Not here!"); } else { book.ID_User = GetUserId().Result; _service.AddBook(book); // Clears the form when a book is added ModelState.Clear(); TempData["success"] = "true"; TempData["title"] = book.Title; } } return(View("../Books/AddBookView", new BookViewModel())); }
protected void btnAddBook_Click(object sender, EventArgs e) { LibraryService service = ServiceFactory.CreateLibraryService(); AddBookRequest request = new AddBookRequest(); request.ISBN = ddlBookTitles.SelectedValue; service.AddBook(request); DisplayBooks(); }
/// <summary> /// Calls the book creation. /// </summary> /// <param name="library">The library service.</param> public static void CreateBook(LibraryService library) { Clear(); string title; string author; int copyrightYear; int numberOfPages; Book book; WriteLine(AskForTitle); title = CreateTitle(ReadLine()); WriteLine(); // user input answer WriteLine(AskForAuthor); author = CreateAuthor(ReadLine()); WriteLine(); // user inputs answer WriteLine(AskForCopyright); copyrightYear = CreateCopyrightYear(ReadLine()); WriteLine(); // user inputs answer WriteLine(AskForNumberOfPages); numberOfPages = CreateNumberOfPages(ReadLine()); WriteLine(); // try to create book try { book = new Book(title: title, author: author, copyrightYear: copyrightYear, numberOfPages: numberOfPages); // try to store book if (!library.AddBook(book)) { throw new InvalidOperationException(); } WriteLine($"Saved \"{book.GetTitle()}\" successfully:"); } catch (InvalidOperationException) { Write("Unable to save the book in our library.\n" + "This mean that this books is already in the library."); Home(library); } catch (Exception) { WriteLine("I'm sorry Dave, I'm afraid that I can't do that..."); WriteLine("Let's try again to create this book."); CreateBook(library); } Home(library); }
public ActionResult <IEnumerable <Book> > AddBook([FromBody] Book newBook) { try { return(Ok(_ls.AddBook(newBook))); } catch (System.Exception err) { return(BadRequest(err.Message)); } }
public void AddAndRemoveBooks_Test() { fls.Login("Gold"); Assert.IsFalse(flc.SearchForABook("Harry Potter and the Philosopher's Stone", "J. K. Rowling", true, BookState.AVAILABLE)); int temp = fl.CountAllBooks(); fls.AddBook("Harry Potter and the Philosopher's Stone", "J. K. Rowling", true); Assert.AreEqual(fl.GetEvents()[0].type, EventType.ADD_A_BOOK); Assert.AreEqual(fl.GetEvents()[0].actor.Username, "Gold"); Assert.AreEqual(fl.GetEvents()[0].bookAffected.Title, "Harry Potter and the Philosopher's Stone"); Assert.IsTrue(flc.SearchForABook("Harry Potter and the Philosopher's Stone", "J. K. Rowling", true, BookState.AVAILABLE)); Assert.AreEqual(fl.CountAllBooks(), temp + 1); fls.RemoveBook("On the Bright Side", "Hendrik Groen", false); Assert.AreEqual(fl.GetEvents()[1].type, EventType.REMOVE_A_BOOK); Assert.AreEqual(fl.GetEvents()[1].actor.Username, "Gold"); Assert.AreEqual(fl.GetEvents()[1].bookAffected.Title, "On the Bright Side"); Assert.IsFalse(flc.SearchForABook("On the Bright Side", "Hendrik Groen", false, BookState.AVAILABLE)); }
public ValueTask ExecuteAsync(IConsole console) { if (_libraryService.GetBook(Title) != null) { throw new CommandException("Book already exists.", 1); } var book = new Book(Title, Author, Published, Isbn); _libraryService.AddBook(book); console.Output.WriteLine("Book added."); console.RenderBook(book); return(default);
private static void ShowManageBookMenu(LibraryService libraryService) { bool manageBooks = true; while (manageBooks) { Console.WriteLine("Please choose the following options:"); Console.WriteLine(" 1.Show all books\n 2.Add new book\n 3.Delete existing book\n 4.Print all rented books\n 5.Edit number of copies"); int.TryParse(Console.ReadLine(), out int manageBookOpt); switch (manageBookOpt) { case 1: libraryService.ShowAllBooks(); break; case 2: libraryService.AddBook(); break; case 3: libraryService.RemoveBook(); break; case 4: libraryService.PrintRentedBooks(); break; case 5: libraryService.EditQuantity(); break; default: Console.WriteLine("Option not found!"); break; } Console.WriteLine("Do you want to continue managing books? Enter y if yes!Any other key to exit section!"); var manageBookChoice = Console.ReadLine(); manageBooks = manageBookChoice.ToLower() == "y"; } }
public void LibraryWithBookItemWillReturnTrue() { library.AddBook(book); // Act bool actual = library.Search(book); // Assert Assert.That(actual, Is.True); }