Browse Source

Implement add & delete functionality

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
6a5986c421
5 changed files with 105 additions and 14 deletions
  1. +1
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java
  2. +59
    -3
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java
  3. +39
    -0
      bookstore/src/main/resources/templates/bookadd.html
  4. +6
    -1
      bookstore/src/main/resources/templates/booklist.html
  5. +0
    -10
      bookstore/src/main/resources/templates/index.html.old

+ 1
- 0
bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java View File

@ -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());


+ 59
- 3
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -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);
}


+ 39
- 0
bookstore/src/main/resources/templates/bookadd.html View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add new book</title>
</head>
<body>
<h1>Add new book</h1>
<div>
<form th:object="${book}" th:action="@{booksave}" action="#" method="post">
<div>
<label for="fname">Author</label>
<input type="text" th:field="*{author}" />
</div>
<div>
<label for="fname">Title</label>
<input type="text" th:field="*{title}" />
</div>
<div>
<label for="fname">ISBN</label>
<input type="text" th:field="*{isbn}" />
</div>
<div>
<label for="fname">Year</label>
<input type="text" th:field="*{year}" />
</div>
<div>
<label for="fname">Price</label>
<input type="text" th:field="*{price}" />
</div>
<input type="submit" value="Save book"></input>
</form>
</div>
</body>
</html>

+ 6
- 1
bookstore/src/main/resources/templates/booklist.html View File

@ -19,8 +19,13 @@
<td th:text="${book.title}"></td>
<td th:text="${book.isbn}"></td>
<td th:text="${book.year}"></td>
<td>
<a th:href="@{/bookdelete/{id}(id=${book.id})}">Delete</a>
</td>
</tr>
</table>
<div>
<a href="/bookadd">Add Book</a>
</div>
</body>
</html>

+ 0
- 10
bookstore/src/main/resources/templates/index.html.old View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Home page</title>
</head>
<body>
N/A // To-be-added
</body>
</html>

Loading…
Cancel
Save