From e71bfc13fbeb5b913f010db36097a093833721f3 Mon Sep 17 00:00:00 2001 From: Austen Stone Date: Thu, 9 May 2024 15:08:48 -0400 Subject: [PATCH] chore: Add genre field to Book model and update database queries --- .../hackathon/advancedsecurityjava/Application.java | 13 +++++++------ .../Controllers/IndexController.java | 6 +++++- .../hackathon/advancedsecurityjava/Models/Book.java | 7 +++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/hackathon/advancedsecurityjava/Application.java b/src/main/java/com/github/hackathon/advancedsecurityjava/Application.java index 5ddd245..644eb58 100644 --- a/src/main/java/com/github/hackathon/advancedsecurityjava/Application.java +++ b/src/main/java/com/github/hackathon/advancedsecurityjava/Application.java @@ -36,11 +36,11 @@ public static void main(String[] args) { List books = new ArrayList(); - 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); @@ -67,13 +67,14 @@ public static void createDatabase() { public static void createDatabaseEntries(List 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(); diff --git a/src/main/java/com/github/hackathon/advancedsecurityjava/Controllers/IndexController.java b/src/main/java/com/github/hackathon/advancedsecurityjava/Controllers/IndexController.java index 2a8439e..83a0cd9 100644 --- a/src/main/java/com/github/hackathon/advancedsecurityjava/Controllers/IndexController.java +++ b/src/main/java/com/github/hackathon/advancedsecurityjava/Controllers/IndexController.java @@ -25,7 +25,8 @@ public class IndexController { @ResponseBody public List 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 books = new ArrayList(); Statement statement = null; @@ -43,6 +44,9 @@ public List 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; diff --git a/src/main/java/com/github/hackathon/advancedsecurityjava/Models/Book.java b/src/main/java/com/github/hackathon/advancedsecurityjava/Models/Book.java index ca0a388..a42b914 100644 --- a/src/main/java/com/github/hackathon/advancedsecurityjava/Models/Book.java +++ b/src/main/java/com/github/hackathon/advancedsecurityjava/Models/Book.java @@ -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; } }