public static List<Patron> GetAll() { List<Patron> allPatrons = new List<Patron>{}; SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM patrons;", conn); rdr = cmd.ExecuteReader(); while(rdr.Read()) { int patronId = rdr.GetInt32(0); string patronName = rdr.GetString(1); Patron newPatron = new Patron(patronName, patronId); allPatrons.Add(newPatron); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return allPatrons; }
public static Patron Find(int patronId) { SqlConnection conn = DB.Connection(); conn.Open(); SqlDataReader rdr = null; SqlCommand cmd = new SqlCommand("SELECT * FROM patrons WHERE id = @PatronId;", conn); SqlParameter patronIdParameter = new SqlParameter(); patronIdParameter.ParameterName = "@PatronId"; patronIdParameter.Value = patronId; cmd.Parameters.Add(patronIdParameter); int foundPatronId = 0; string foundPatronName = null; rdr = cmd.ExecuteReader(); while(rdr.Read()) { foundPatronId = rdr.GetInt32(0); foundPatronName = rdr.GetString(1); } Patron foundPatron = new Patron(foundPatronName, foundPatronId); if(rdr!=null) rdr.Close(); if(conn!=null) conn.Close(); return foundPatron; }
public void Patron_Checkout_ChecksoutABook() { Patron firstPatron = new Patron("Mayor McCheese"); firstPatron.Save(); Book newBook = new Book("Cats", testDate, 2); newBook.Save(); newBook.StockBook(); firstPatron.CheckoutBook(newBook.GetCopies()[0].GetId(), testDate2); List<Copy> result = firstPatron.GetCheckOutRecord(false); List<Copy> expectedResult = newBook.GetCopies(); Assert.Equal(expectedResult, result); }
public void Copy_GetAllCheckouts_ReturnAllCheckoutInfo() { Patron firstPatron = new Patron("Mayor McCheese"); firstPatron.Save(); Book firstBook = new Book("Cats", testDate, 2); firstBook.Save(); firstBook.StockBook(); Book secondBook = new Book("Dogs", testDate, 2); secondBook.Save(); secondBook.StockBook(); firstPatron.CheckoutBook(firstBook.GetCopies()[0].GetId(), testDate2); firstPatron.CheckoutBook(secondBook.GetCopies()[0].GetId(), testDate); Dictionary<string, object> result = Copy.GetAllCheckouts(); List<Patron> expectedPatrons = new List<Patron>{firstPatron, firstPatron}; List<Copy> expectedCopies = new List<Copy>{firstBook.GetCopies()[0], secondBook.GetCopies()[0]}; List<DateTime?> expectedDueDates = new List<DateTime?>{testDate2, testDate}; Assert.Equal(result["patrons"], expectedPatrons); Assert.Equal(result["copies"], expectedCopies); Assert.Equal(result["dueDates"], expectedDueDates); }
public void Book_GetCheckedOutCopies() { Book newBook = new Book("Cathedrals", testDate, 2); newBook.Save(); Patron firstPatron = new Patron("Mayor McCheese"); firstPatron.Save(); newBook.StockBook(); newBook.StockBook(); newBook.StockBook(); firstPatron.CheckoutBook(newBook.GetCopies()[0].GetId(), testDate2); firstPatron.CheckoutBook(newBook.GetCopies()[2].GetId(), testDate2); List<Copy> expectedResult = new List<Copy>{newBook.GetCopies()[0], newBook.GetCopies()[2]}; List<Copy> result = newBook.GetCheckedOutCopies(); Assert.Equal(expectedResult, result); }
public HomeModule() { Get["/"] = _ => View["index.cshtml"]; Get["/books"] = _ => { List<Book> allBooks = Book.GetAll(); return View["books.cshtml", allBooks]; }; Post["/results"] = _ => { string searchTerm = Request.Form["search-term"]; bool searchType = Request.Form["search-type"]; bool searchByTitle = Request.Form["title-or-author"]; List<Book> searchResult = new List<Book>{}; if(searchByTitle) { searchResult = Book.SearchForBookByTitle(searchTerm, searchType); } else { searchResult = Book.SearchForBookByAuthor(searchTerm, searchType); } Dictionary<string, object> model = new Dictionary<string, object>{}; model.Add("results", searchResult); model.Add("priorSearchTerm", searchTerm); model.Add("priorSearchType", searchType); model.Add("priorSearchBy", searchByTitle); return View["results.cshtml", model]; }; Get["/books/{id}"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>{}; List<Author> allAuthors = Author.GetAll(); Book selectedBook = Book.Find(parameters.id); List<Genre> allGenres = Genre.GetAll(); List<Patron> allPatrons = Patron.GetAll(); model.Add("patrons", allPatrons); model.Add("genres", allGenres); model.Add("book", selectedBook); model.Add("authors", allAuthors); return View["book.cshtml", model]; }; Get["/books/{id}/stock"]= parameters => { Dictionary<string, object> model = new Dictionary<string, object>{}; List<Author> allAuthors = Author.GetAll(); Book selectedBook = Book.Find(parameters.id); selectedBook.StockBook(); List<Genre> allGenres = Genre.GetAll(); List<Patron> allPatrons = Patron.GetAll(); model.Add("patrons", allPatrons); model.Add("genres", allGenres); model.Add("book", selectedBook); model.Add("authors", allAuthors); return View["book.cshtml", model]; }; Post["/books/{id}"] = parameters => { Book selectedBook = Book.Find(parameters.id); int selectedAuthor = Request.Form["author-name"]; Dictionary<string, object> model = new Dictionary<string, object>{}; selectedBook.AddAuthor(selectedAuthor); List<Author> allAuthors = Author.GetAll(); List<Genre> allGenres = Genre.GetAll(); List<Patron> allPatrons = Patron.GetAll(); model.Add("patrons", allPatrons); model.Add("genres", allGenres); model.Add("book", selectedBook); model.Add("authors", allAuthors); return View["book.cshtml", model]; }; Delete["/books/{id}"] = parameters => { Book selectedBook = Book.Find(parameters.id); int authorToDelete = Request.Form["author-name"]; selectedBook.DeleteAuthor(authorToDelete); Dictionary<string, object> model = new Dictionary<string, object>{}; List<Author> allAuthors = Author.GetAll(); List<Genre> allGenres = Genre.GetAll(); List<Patron> allPatrons = Patron.GetAll(); model.Add("patrons", allPatrons); model.Add("genres", allGenres); model.Add("book", selectedBook); model.Add("authors", allAuthors); return View["book.cshtml", model]; }; Patch["/books/{id}"] = parameters => { Book selectedBook = Book.Find(parameters.id); selectedBook.Update(Request.Form["book-title"], Request.Form["publication-date"], Request.Form["new-genre"]); Dictionary<string, object> model = new Dictionary<string, object>{}; List<Author> allAuthors = Author.GetAll(); List<Genre> allGenres = Genre.GetAll(); List<Patron> allPatrons = Patron.GetAll(); model.Add("patrons", allPatrons); model.Add("genres", allGenres); model.Add("book", selectedBook); model.Add("authors", allAuthors); return View["book.cshtml", model]; }; Get["/books/add"] = _ => { Dictionary<string, object> model = new Dictionary<string, object>{}; List<Genre> allGenres = Genre.GetAll(); List<Author> allAuthors = Author.GetAll(); model.Add("genres", allGenres); model.Add("authors", allAuthors); return View["book_new.cshtml", model]; }; Post["/books/add"] = _ => { Book newBook = new Book(Request.Form["book-title"], Request.Form["publication-date"], Request.Form["genre"]); newBook.Save(); int authorId = Request.Form["author"]; newBook.AddAuthor(authorId); List<Book> allBooks = Book.GetAll(); return View["books.cshtml", allBooks]; }; Get["/authors"] = _ => { List<Author> allAuthors = Author.GetAll(); return View["authors.cshtml", allAuthors]; }; Get["/authors/{id}"] = parameters => { Author selectedAuthor = Author.Find(parameters.id); return View["author.cshtml", selectedAuthor]; }; Get["/authors/add"] = _ =>View["author_new.cshtml"]; Post["/authors/add"] = _ => { Author newAuthor = new Author(Request.Form["author-name"]); newAuthor.Save(); List<Author> allAuthors = Author.GetAll(); return View["authors.cshtml", allAuthors]; }; Get["/genres"] = _ => { List<Genre> allGenres = Genre.GetAll(); return View["genres.cshtml", allGenres]; }; Get["/genres/{id}"] = parameters => { Genre selectedGenre = Genre.Find(parameters.id); return View["genre.cshtml", selectedGenre]; }; Get["/genres/add"] = _ =>View["genre_new.cshtml"]; Post["/genres/add"] = _ => { Genre newGenre = new Genre(Request.Form["genre-name"]); newGenre.Save(); List<Genre> allGenres = Genre.GetAll(); return View["genres.cshtml", allGenres]; }; Get["/patrons"] = _ => { List<Patron> allPatrons = Patron.GetAll(); return View["patrons.cshtml", allPatrons]; }; Get["/patrons/{id}"] = parameters => { Patron selectedPatron = Patron.Find(parameters.id); return View["patron.cshtml", selectedPatron]; }; Get["/patrons/new"] = _ => View["patron_new.cshtml"]; Post["/patrons"] = _ => { Patron newPatron = new Patron(Request.Form["name"]); newPatron.Save(); List<Patron> allPatrons = Patron.GetAll(); return View["patrons.cshtml", allPatrons]; }; Post["/patrons/checkout"] = _ => { Patron selectedPatron = Patron.Find(Request.Form["patron"]); DateTime today = DateTime.Today; selectedPatron.CheckoutBook(Request.Form["copy"], today.AddDays(Request.Form["length-of-borrow"])); return View["patron.cshtml", selectedPatron]; }; Post["/patrons/{id}/return"] = parameters => { Patron selectedPatron = Patron.Find(parameters.id); int bookId = Request.Form["book"]; selectedPatron.ReturnBook(bookId); return View["patron.cshtml", selectedPatron]; }; Get["/librarian"] = _ => { //Key: "patrons, copies, dueDates" Dictionary<string, object> allCheckouts = Copy.GetAllCheckouts(); return View["librarian.cshtml", allCheckouts]; }; }
public void Patron_Delete_DeletesPatronById() { Patron firstPatron = new Patron("Mayor McCheese"); firstPatron.Save(); Patron secondPatron = new Patron("Franklin Pierce"); secondPatron.Save(); firstPatron.Delete(); List<Patron> expectedResult = new List<Patron>{secondPatron}; List<Patron> actualResult = Patron.GetAll(); Assert.Equal(expectedResult, actualResult); }
public void Patron_Save_SavesPatronToDatabase() { Patron newPatron = new Patron("Mayor McCheese"); newPatron.Save(); List<Patron> expectedResult = new List<Patron>{newPatron}; List<Patron> actualResult = Patron.GetAll(); Assert.Equal(expectedResult, actualResult); }
public void Patron_ReturnBook_ReturnsCheckedoutBook() { Patron firstPatron = new Patron("Mayor McCheese"); firstPatron.Save(); Book newBook = new Book("Cats", testDate, 2); newBook.Save(); newBook.StockBook(); firstPatron.CheckoutBook(newBook.GetCopies()[0].GetId(), testDate2); firstPatron.ReturnBook(newBook.GetCopies()[0].GetId()); int result = firstPatron.GetCheckOutRecord(false).Count; Assert.Equal(0, result); }
public void Patron_GetDueDate_GetsDueDateOfCheckedOutBook() { Patron firstPatron = new Patron("Mayor McCheese"); firstPatron.Save(); Book newBook = new Book("Cats", testDate, 2); newBook.Save(); newBook.StockBook(); firstPatron.CheckoutBook(newBook.GetCopies()[0].GetId(), testDate2); DateTime? result = firstPatron.GetReturnDate(newBook.GetCopies()[0].GetId()); DateTime? expectedResult = testDate2; Assert.Equal(expectedResult, result); }
public void Patron_GetCheckoutHistory_ReturnsCheckOutHistory() { Patron firstPatron = new Patron("Mayor McCheese"); firstPatron.Save(); Book firstBook = new Book("Cats", testDate, 2); firstBook.Save(); firstBook.StockBook(); Book secondBook = new Book("Dogs", testDate, 2); secondBook.Save(); secondBook.StockBook(); firstPatron.CheckoutBook(firstBook.GetCopies()[0].GetId(), testDate2); firstPatron.CheckoutBook(secondBook.GetCopies()[0].GetId(), testDate2); firstPatron.ReturnBook(firstBook.GetCopies()[0].GetId()); Copy result = firstPatron.GetCheckOutRecord(true)[0]; Copy expectedResult = firstBook.GetCopies()[0]; Assert.Equal(expectedResult, result); }
public void Patron_FindsPatronInDatabase() { //Arrange Patron expectedResult = new Patron("Mayor McCheese"); expectedResult.Save(); //Act Patron result = Patron.Find(expectedResult.GetId()); //Assert Assert.Equal(expectedResult, result); }