public static List<Author> GetAll()
        {
            List<Author> allAuthors = new List<Author>{};

              SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM authors;", conn);
              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            int authorId = rdr.GetInt32(0);
            string authorName = rdr.GetString(1);
            Author newAuthor = new Author(authorName, authorId);
            allAuthors.Add(newAuthor);
              }

              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }

              return allAuthors;
        }
        public static Author Find(int authorId)
        {
            SqlConnection conn = DB.Connection();
              conn.Open();
              SqlDataReader rdr = null;

              SqlCommand cmd = new SqlCommand("SELECT * FROM authors WHERE id = @AuthorId;", conn);

              SqlParameter authorIdParameter = new SqlParameter();
              authorIdParameter.ParameterName = "@AuthorId";
              authorIdParameter.Value = authorId;
              cmd.Parameters.Add(authorIdParameter);

              int foundAuthorId = 0;
              string foundAuthorName = null;

              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            foundAuthorId = rdr.GetInt32(0);
            foundAuthorName = rdr.GetString(1);
              }
              Author foundAuthor = new Author(foundAuthorName, foundAuthorId);

              if(rdr!=null) rdr.Close();
              if(conn!=null) conn.Close();

              return foundAuthor;
        }
 public void Author_FindsAuthorInDatabase()
 {
     //Arrange
       Author expectedResult = new Author("Todd");
       expectedResult.Save();
       //Act
       Author result = Author.Find(expectedResult.GetId());
       //Assert
       Assert.Equal(expectedResult, result);
 }
        public void Author_Delete_DeletesAuthorById()
        {
            Author firstAuthor = new Author("Chad");
              firstAuthor.Save();
              Author secondAuthor = new Author("Todd");
              secondAuthor.Save();

              firstAuthor.Delete();

              List<Author> expectedResult = new List<Author>{secondAuthor};
              List<Author> actualResult = Author.GetAll();
              Assert.Equal(expectedResult, actualResult);
        }
 public void Book_AddAuthors()
 {
     //Arrange
       Book newBook = new Book("Cats", testDate, 2);
       newBook.Save();
       Author newAuthor = new Author("Chad");
       newAuthor.Save();
       //Act
       newBook.AddAuthor(newAuthor.GetId());
       List<Author> result = newBook.GetAuthors();
       List<Author> expectedResult = new List<Author>{newAuthor};
       //Assert
       Assert.Equal(expectedResult, result);
 }
 public void Author_GetAllBooksByAuthor()
 {
     //Arrange
       Author firstAuthor = new Author("Chad");
       firstAuthor.Save();
       Book firstBook = new Book("Cats", testDate, 2);
       firstBook.Save();
       Book secondBook = new Book("Crime & Punishment", testDate, 3);
       secondBook.Save();
       //Act
       firstBook.AddAuthor(firstAuthor.GetId());
       List<Book> result = firstAuthor.GetBooks();
       List<Book> expectedResult = new List<Book>{firstBook};
       //Assert
       Assert.Equal(expectedResult, result);
 }
 public void Book_DeleteAuthor()
 {
     //Arrange
       Book newBook = new Book("Cats", testDate, 2);
       newBook.Save();
       Author firstAuthor = new Author("Chad");
       firstAuthor.Save();
       Author secondAuthor = new Author("Todd");
       secondAuthor.Save();
       newBook.AddAuthor(firstAuthor.GetId());
       newBook.AddAuthor(secondAuthor.GetId());
       //Act
       newBook.DeleteAuthor(firstAuthor.GetId());
       List<Author> result = newBook.GetAuthors();
       List<Author> expectedResult = new List<Author>{secondAuthor};
       //Assert
       Assert.Equal(expectedResult, result);
 }
        public void Book_FindBookByAuthorFullMatch()
        {
            Book newBook = new Book("Cathedrals", testDate, 2);
              newBook.Save();
              Book secondBook = new Book("Other Book", testDate, 1);
              secondBook.Save();

              Author newAuthor1 = new Author("Chad");
              newAuthor1.Save();
              Author newAuthor2 = new Author("Chadwick");
              newAuthor2.Save();

              newBook.AddAuthor(newAuthor1.GetId());
              secondBook.AddAuthor(newAuthor2.GetId());

              List<Book> result = Book.SearchForBookByAuthor("Chad", false);
              List<Book> expectedResult = new List<Book>{newBook};
              Assert.Equal(expectedResult, result);
        }
        public void Book_FindBookByAuthorNoDuplicates()
        {
            Book newBook = new Book("Cathedrals", testDate, 2);
              newBook.Save();
              Book secondBook = new Book("Other Book", testDate, 1);
              secondBook.Save();

              Author newAuthor1 = new Author("Meredith Hartley");
              newAuthor1.Save();
              Author newAuthor2 = new Author("Chadwick Hartley");
              newAuthor2.Save();

              newBook.AddAuthor(newAuthor1.GetId());
              newBook.AddAuthor(newAuthor2.GetId());

              List<Book> result = Book.SearchForBookByAuthor("Hartley", true);
              List<Book> expectedResult = new List<Book>{newBook};
              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 Author_Save_SavesAuthorToDatabase()
        {
            Author newAuthor = new Author("Chad");

              newAuthor.Save();
              List<Author> expectedResult = new List<Author>{newAuthor};
              List<Author> actualResult = Author.GetAll();

              Assert.Equal(expectedResult, actualResult);
        }
Exemple #12
0
        public List<Author> GetAuthors()
        {
            SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT authors.* FROM books JOIN books_authors ON (books_authors.book_id = books.id) JOIN authors ON (books_authors.author_id = authors.id) WHERE book_id = @BookId;", conn);

              SqlParameter bookIdParameter = new SqlParameter();
              bookIdParameter.ParameterName = "@BookId";
              bookIdParameter.Value = this.GetId();
              cmd.Parameters.Add(bookIdParameter);

              rdr=cmd.ExecuteReader();

              List<Author> foundAuthors = new List<Author>{};

              while(rdr.Read())
              {
            int foundId = rdr.GetInt32(0);
            string foundName = rdr.GetString(1);
            Author foundAuthor = new Author(foundName, foundId);
            foundAuthors.Add(foundAuthor);
              }

              if(rdr!=null) rdr.Close();
              if(conn!=null) conn.Close();

              return foundAuthors;
        }