diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java b/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java index 4fa7bb8..ec992f0 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java @@ -29,6 +29,7 @@ public class BookstoreApplication extends SpringBootServletInitializer { 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()); 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 cbc5781..48d0585 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -4,6 +4,7 @@ package com.fjordtek.bookstore.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +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; @@ -25,6 +26,9 @@ 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"; private HttpServerLogger httpServerLogger = new HttpServerLogger(); private HttpExceptionHandler httpExceptionHandler = new HttpExceptionHandler(); @@ -34,9 +38,9 @@ public class BookController { @RequestMapping( value = { bookListPageURL }, - method = RequestMethod.GET + method = { RequestMethod.GET, RequestMethod.POST } ) - public String DefaultWebFormGet(Model dataModel, HttpServletRequest requestData) { + public String defaultWebFormGet(Model dataModel, HttpServletRequest requestData) { httpServerLogger.logMessageNormal( requestData, @@ -49,6 +53,58 @@ public class BookController { } + @RequestMapping( + value = bookAddPageURL, + method = { RequestMethod.GET, RequestMethod.PUT } + ) + public String webFormAddBook(Model dataModel, HttpServletRequest requestData) { + + httpServerLogger.logMessageNormal( + requestData, + "HTTPOK" + ); + + dataModel.addAttribute("book", new Book()); + + return bookAddPageURL; + } + + @RequestMapping( + value = bookSavePageURL, + method = RequestMethod.POST + ) + public String webFormSaveBook(Book book, HttpServletRequest requestData) { + + httpServerLogger.logMessageNormal( + requestData, + "HTTPOK" + ); + + bookRepository.save(book); + + return "redirect:" + bookListPageURL; + } + + @RequestMapping( + value = bookDeletePageURL + "/{id}", + method = RequestMethod.GET + ) + public String webFormDeleteBook( + @PathVariable("id") long bookId, + Model dataModel, HttpServletRequest requestData + ) { + + httpServerLogger.logMessageNormal( + requestData, + "HTTPOK" + ); + + bookRepository.deleteById(bookId); + + return "redirect:../" + bookListPageURL; + } + + // Redirect @RequestMapping( value = { "/", landingPageURL }, @@ -64,7 +120,7 @@ public class BookController { value = "*", method = { RequestMethod.GET, RequestMethod.POST } ) - public String ErrorWebForm(HttpServletRequest requestData) { + public String errorWebForm(HttpServletRequest requestData) { return httpExceptionHandler.notFoundErrorHandler(requestData); } diff --git a/bookstore/src/main/resources/templates/bookadd.html b/bookstore/src/main/resources/templates/bookadd.html new file mode 100644 index 0000000..38dae61 --- /dev/null +++ b/bookstore/src/main/resources/templates/bookadd.html @@ -0,0 +1,39 @@ + + + + + Add new book + + +

Add new book

+
+ +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + +
+ +
+ + diff --git a/bookstore/src/main/resources/templates/booklist.html b/bookstore/src/main/resources/templates/booklist.html index 78f131b..e5bf702 100644 --- a/bookstore/src/main/resources/templates/booklist.html +++ b/bookstore/src/main/resources/templates/booklist.html @@ -19,8 +19,13 @@ + + Delete + - +
+ Add Book +
diff --git a/bookstore/src/main/resources/templates/index.html.old b/bookstore/src/main/resources/templates/index.html.old deleted file mode 100644 index 96d69d5..0000000 --- a/bookstore/src/main/resources/templates/index.html.old +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Home page - - - N/A // To-be-added - -