Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add genre field to Book model and update database queries #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public static void main(String[] args) {

List<Book> books = new ArrayList<Book>();

books.add(new Book("The Hobbit", "JRR Tolkien", true));
books.add(new Book("The Fellowship of the Ring", "JRR Tolkien", true));
books.add(new Book("The Eye of the World", "Robert Jordan"));
books.add(new Book("A Game of Thrones", "George R. R. Martin", true));
books.add(new Book("The Way of Kings", "Brandon Sanderson"));
books.add(new Book("The Hobbit", "JRR Tolkien", "Fantasy", true));
books.add(new Book("The Fellowship of the Ring", "JRR Tolkien", "Fantasy", true));
books.add(new Book("The Eye of the World", "Robert Jordan", "Fantasy"));
books.add(new Book("A Game of Thrones", "George R. R. Martin", "Fantasy", true));
books.add(new Book("The Way of Kings", "Brandon Sanderson", "Fantasy"));

// Create database entries
createDatabaseEntries(books);
Expand All @@ -67,13 +67,14 @@ public static void createDatabase() {
public static void createDatabaseEntries(List<Book> books) {

try (Connection connection = DriverManager.getConnection(connectionString)) {
String query = "INSERT INTO Books (name, author, read) VALUES(?, ?, ?)";
String query = "INSERT INTO Books (name, author, genre, read) VALUES(?, ?, ?, ?)";

for (Book book : books) {
try (PreparedStatement prepStmt = connection.prepareStatement(query);) {
prepStmt.setString(1, book.name);
prepStmt.setString(2, book.author);
prepStmt.setInt(3, book.read? 1 : 0);
prepStmt.setString(4, book.genre);

prepStmt.executeUpdate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class IndexController {
@ResponseBody
public List<Book> getBooks(@RequestParam(name = "name", required = false) String bookname,
@RequestParam(name = "author", required = false) String bookauthor,
@RequestParam(name = "read", required = false) Boolean bookread) {
@RequestParam(name = "read", required = false) Boolean bookread),
@RequestParam(name = "genre", required = false) String bookgenre) {
List<Book> books = new ArrayList<Book>();

Statement statement = null;
Expand All @@ -43,6 +44,9 @@ public List<Book> getBooks(@RequestParam(name = "name", required = false) String
} else if (bookauthor != null) {
// Filter by book author
query = "SELECT * FROM Books WHERE author LIKE '%" + bookauthor + "%'";
} else if (bookgenre != null) {
// Filter by book genre
query = "SELECT * FROM Books WHERE genre LIKE " + bookauthor;
} else if (bookread != null) {
// Filter by if the book has been read or not
Integer read = bookread ? 1 : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
public class Book {
public String name;
public String author;
public String genre;

public Boolean read = false;

public Book(String name, String author) {
public Book(String name, String author, String genre) {
this.name = name;
this.author = author;
this.genre = genre;
}

public Book(String name, String author, Boolean read) {
public Book(String name, String author, String genre, Boolean read) {
this.name = name;
this.author = author;
this.genre = genre;
this.read = read;
}
}