From d121c07ce61b7bba81ac3aaa89035d07ce93ff26 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Sun, 13 Sep 2020 00:15:13 +0300 Subject: [PATCH] Implement support for database edits, re-factor code Signed-off-by: Pekka Helenius --- .../bookstore/BookstoreApplication.java | 9 ++- .../com/fjordtek/bookstore/model/Book.java | 9 ++- .../bookstore/model/BookRepository.java | 4 +- .../bookstore/web/BookController.java | 78 ++++++++++++++----- .../src/main/resources/templates/bookadd.html | 2 +- .../main/resources/templates/bookedit.html | 39 ++++++++++ .../main/resources/templates/booklist.html | 3 + 7 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 bookstore/src/main/resources/templates/bookedit.html diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java b/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java index ec992f0..a6fecf6 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java @@ -25,14 +25,15 @@ public class BookstoreApplication extends SpringBootServletInitializer { public CommandLineRunner bookDatabaseRunner(BookRepository repository) { return (args) -> { - commonLogger.info("Add new books to a database"); + commonLogger.info("Add new sample books to database"); repository.save(new Book("Book 1 title", "Book 1 author", 2020, "aaa-b2b-c3c-444", 40.00)); repository.save(new Book("Book 2 title", "Book 2 author", 2005, "111-2b2-3c3-ddd", 20.17)); - commonLogger.info("New books in the database"); - for (Book student : repository.findAll()) { - commonLogger.info(student.toString()); + commonLogger.info("------------------------------"); + commonLogger.info("Sample books in the database"); + for (Book book : repository.findAll()) { + commonLogger.info(book.toString()); } }; diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java index dce45ab..006ac84 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java @@ -66,7 +66,10 @@ public class Book { // TODO consider accessibility restrictions? - // Id generated automatically + // NOTE: in default scenario, this is automatically generated + public void setId(long id) { + this.id = id; + } public void setTitle(String title) { this.title = title; @@ -93,7 +96,9 @@ public class Book { // TODO consider accessibility restrictions? - // Id generated automatically + public long getId() { + return id; + } public String getTitle() { return title; diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java index c59f024..421ba2d 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java @@ -6,8 +6,10 @@ import java.util.List; import org.springframework.data.repository.CrudRepository; + public interface BookRepository extends CrudRepository { + // Handles both INSERT and UPDATE queries List findById(String title); - + } \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index 48d0585..e82da3d 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -4,31 +4,26 @@ package com.fjordtek.bookstore.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.web.bind.annotation.RequestParam; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; -//import java.util.ArrayList; - import javax.servlet.http.HttpServletRequest; -//import javax.validation.Valid; import com.fjordtek.bookstore.model.*; @Controller public class BookController { - - protected static final String landingPageURL = "index"; protected static final String bookListPageURL = "booklist"; protected static final String bookAddPageURL = "bookadd"; protected static final String bookDeletePageURL = "bookdelete"; - protected static final String bookSavePageURL = "booksave"; + protected static final String bookEditPageURL = "bookedit"; private HttpServerLogger httpServerLogger = new HttpServerLogger(); private HttpExceptionHandler httpExceptionHandler = new HttpExceptionHandler(); @@ -37,14 +32,14 @@ public class BookController { private BookRepository bookRepository; @RequestMapping( - value = { bookListPageURL }, + value = bookListPageURL, method = { RequestMethod.GET, RequestMethod.POST } ) public String defaultWebFormGet(Model dataModel, HttpServletRequest requestData) { httpServerLogger.logMessageNormal( requestData, - "HTTPOK" + bookListPageURL + ": " + "HTTPOK" ); dataModel.addAttribute("books", bookRepository.findAll()); @@ -53,6 +48,9 @@ public class BookController { } + ////////////////////////////// + // ADD BOOK + @RequestMapping( value = bookAddPageURL, method = { RequestMethod.GET, RequestMethod.PUT } @@ -61,23 +59,23 @@ public class BookController { httpServerLogger.logMessageNormal( requestData, - "HTTPOK" + bookAddPageURL + ": " + "HTTPOK" ); dataModel.addAttribute("book", new Book()); return bookAddPageURL; } - + @RequestMapping( - value = bookSavePageURL, + value = bookAddPageURL, method = RequestMethod.POST ) - public String webFormSaveBook(Book book, HttpServletRequest requestData) { + public String webFormSaveNewBook(Book book, HttpServletRequest requestData) { httpServerLogger.logMessageNormal( requestData, - "HTTPOK" + bookEditPageURL + ": " + "HTTPOK" ); bookRepository.save(book); @@ -85,27 +83,71 @@ public class BookController { return "redirect:" + bookListPageURL; } + ////////////////////////////// + // DELETE BOOK + @RequestMapping( value = bookDeletePageURL + "/{id}", method = RequestMethod.GET ) public String webFormDeleteBook( @PathVariable("id") long bookId, - Model dataModel, HttpServletRequest requestData - ) { + HttpServletRequest requestData + ) { httpServerLogger.logMessageNormal( requestData, - "HTTPOK" + bookDeletePageURL + ": " + "HTTPOK" ); bookRepository.deleteById(bookId); return "redirect:../" + bookListPageURL; } + + ////////////////////////////// + // UPDATE BOOK + + @RequestMapping( + value = bookEditPageURL + "/{id}", + method = RequestMethod.GET + ) + public String webFormEditBook( + @PathVariable("id") long bookId, + Model dataModel, HttpServletRequest requestData + ) { + + httpServerLogger.logMessageNormal( + requestData, + bookEditPageURL + ": " + "HTTPOK" + ); + + Book book = bookRepository.findById(bookId).get(); + dataModel.addAttribute("book", book); + + return bookEditPageURL; + } + + @RequestMapping( + value = bookEditPageURL + "/{id}", + method = RequestMethod.POST + ) + public String webFormUpdateBook(@ModelAttribute("book") Book book, HttpServletRequest requestData) { + + httpServerLogger.logMessageNormal( + requestData, + bookEditPageURL + ": " + "HTTPOK" + ); + + bookRepository.save(book); + + return "redirect:../" + bookListPageURL; + } + + ////////////////////////////// + // REDIRECTS - // Redirect @RequestMapping( value = { "/", landingPageURL }, method = RequestMethod.GET diff --git a/bookstore/src/main/resources/templates/bookadd.html b/bookstore/src/main/resources/templates/bookadd.html index 38dae61..e1c4d14 100644 --- a/bookstore/src/main/resources/templates/bookadd.html +++ b/bookstore/src/main/resources/templates/bookadd.html @@ -8,7 +8,7 @@

Add new book

-
+
diff --git a/bookstore/src/main/resources/templates/bookedit.html b/bookstore/src/main/resources/templates/bookedit.html new file mode 100644 index 0000000..aa210e7 --- /dev/null +++ b/bookstore/src/main/resources/templates/bookedit.html @@ -0,0 +1,39 @@ + + + + + Update existing book + + +

Update existing book

+
+ + + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + + + +
+ + diff --git a/bookstore/src/main/resources/templates/booklist.html b/bookstore/src/main/resources/templates/booklist.html index e5bf702..e09a4d6 100644 --- a/bookstore/src/main/resources/templates/booklist.html +++ b/bookstore/src/main/resources/templates/booklist.html @@ -22,6 +22,9 @@ Delete + + Edit +