Browse Source

Implement support for database edits, re-factor code

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
d121c07ce6
7 changed files with 118 additions and 26 deletions
  1. +5
    -4
      bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java
  2. +7
    -2
      bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java
  3. +3
    -1
      bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java
  4. +60
    -18
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java
  5. +1
    -1
      bookstore/src/main/resources/templates/bookadd.html
  6. +39
    -0
      bookstore/src/main/resources/templates/bookedit.html
  7. +3
    -0
      bookstore/src/main/resources/templates/booklist.html

+ 5
- 4
bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java View File

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


+ 7
- 2
bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java View File

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


+ 3
- 1
bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java View File

@ -6,8 +6,10 @@ import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface BookRepository extends CrudRepository<Book, Long> {
// Handles both INSERT and UPDATE queries
List<Book> findById(String title);
}

+ 60
- 18
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -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


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

@ -8,7 +8,7 @@
<h1>Add new book</h1>
<div>
<form th:object="${book}" th:action="@{booksave}" action="#" method="post">
<form th:object="${book}" th:action="@{bookadd}" action="#" method="post">
<div>
<label for="fname">Author</label>


+ 39
- 0
bookstore/src/main/resources/templates/bookedit.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>Update existing book</title>
</head>
<body>
<h1>Update existing book</h1>
<div>
<form th:object="${book}" th:action="@{/bookedit/{id}(id=${book.id})}" 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="Update book"></input>
</form>
</div>
</body>
</html>

+ 3
- 0
bookstore/src/main/resources/templates/booklist.html View File

@ -22,6 +22,9 @@
<td>
<a th:href="@{/bookdelete/{id}(id=${book.id})}">Delete</a>
</td>
<td>
<a th:href="@{/bookedit/{id}(id=${book.id})}">Edit</a>
</td>
</tr>
</table>
<div>


Loading…
Cancel
Save