コード例 #1
0
ファイル: Genre.cs プロジェクト: CharlesEwel/library-catalog
        public static List<Genre> GetAll()
        {
            List<Genre> allGenres = new List<Genre>{};

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

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

              while(rdr.Read())
              {
            int genreId = rdr.GetInt32(0);
            string genreName = rdr.GetString(1);
            Genre newGenre = new Genre(genreName, genreId);
            allGenres.Add(newGenre);
              }

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

              return allGenres;
        }
コード例 #2
0
ファイル: Genre.cs プロジェクト: CharlesEwel/library-catalog
        public static Genre Find(int genreId)
        {
            SqlConnection conn = DB.Connection();
              conn.Open();
              SqlDataReader rdr = null;

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

              SqlParameter genreIdParameter = new SqlParameter();
              genreIdParameter.ParameterName = "@GenreId";
              genreIdParameter.Value = genreId;
              cmd.Parameters.Add(genreIdParameter);

              int foundGenreId = 0;
              string foundGenreName = null;

              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            foundGenreId = rdr.GetInt32(0);
            foundGenreName = rdr.GetString(1);
              }
              Genre foundGenre = new Genre(foundGenreName, foundGenreId);

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

              return foundGenre;
        }
コード例 #3
0
 public void Genre_FindsGenreInDatabase()
 {
     //Arrange
       Genre expectedResult = new Genre("Literature");
       expectedResult.Save();
       //Act
       Genre result = Genre.Find(expectedResult.GetId());
       //Assert
       Assert.Equal(expectedResult, result);
 }
コード例 #4
0
        public void Genre_Delete_DeletesGenreById()
        {
            Genre firstGenre = new Genre("Sci-Fi");
              firstGenre.Save();
              Genre secondGenre = new Genre("Literature");
              secondGenre.Save();

              firstGenre.Delete();

              List<Genre> expectedResult = new List<Genre>{secondGenre};
              List<Genre> actualResult = Genre.GetAll();
              Assert.Equal(expectedResult, actualResult);
        }
コード例 #5
0
        public void Genre_GetBooks_GetsAllBookInGenre()
        {
            Genre firstGenre = new Genre("Sci-Fi");
              firstGenre.Save();
              Book firstBook = new Book("Cats", testDate, 2);
              firstBook.Save();
              Book secondBook = new Book("Crime & Punishment", testDate, firstGenre.GetId());
              secondBook.Save();

              List<Book> expectedResult = new List<Book> {secondBook};
              List<Book> actualResult = firstGenre.GetBooks();

              Assert.Equal(expectedResult, actualResult);
        }
コード例 #6
0
        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];
              };
        }
コード例 #7
0
        public void Genre_Save_SavesGenreToDatabase()
        {
            Genre newGenre = new Genre("Sci-Fi");

              newGenre.Save();
              List<Genre> expectedResult = new List<Genre>{newGenre};
              List<Genre> actualResult = Genre.GetAll();

              Assert.Equal(expectedResult, actualResult);
        }