Browse Source

Update book data Edit logic

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
074c25c0ed
1 changed files with 32 additions and 20 deletions
  1. +32
    -20
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

+ 32
- 20
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import com.fjordtek.bookstore.model.Book; import com.fjordtek.bookstore.model.Book;
import com.fjordtek.bookstore.model.BookRepository; import com.fjordtek.bookstore.model.BookRepository;
import com.fjordtek.bookstore.model.Category;
import com.fjordtek.bookstore.model.CategoryRepository; import com.fjordtek.bookstore.model.CategoryRepository;
@Controller @Controller
@ -31,6 +32,7 @@ public class BookController {
protected static final String bookAddPageURL = "bookadd"; protected static final String bookAddPageURL = "bookadd";
protected static final String bookDeletePageURL = "bookdelete"; protected static final String bookDeletePageURL = "bookdelete";
protected static final String bookEditPageURL = "bookedit"; protected static final String bookEditPageURL = "bookedit";
protected static final String bookSavePageURL = "booksave";
private HttpServerLogger httpServerLogger = new HttpServerLogger(); private HttpServerLogger httpServerLogger = new HttpServerLogger();
private HttpExceptionHandler httpExceptionHandler = new HttpExceptionHandler(); private HttpExceptionHandler httpExceptionHandler = new HttpExceptionHandler();
@ -49,13 +51,13 @@ public class BookController {
) )
public String defaultWebFormGet(HttpServletRequest requestData, Model dataModel) { public String defaultWebFormGet(HttpServletRequest requestData, Model dataModel) {
dataModel.addAttribute("books", bookRepository.findAll());
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookListPageURL + ": " + "HTTPOK" bookListPageURL + ": " + "HTTPOK"
); );
dataModel.addAttribute("books", bookRepository.findAll());
return bookListPageURL; return bookListPageURL;
} }
@ -72,13 +74,7 @@ public class BookController {
Model dataModel Model dataModel
) { ) {
httpServerLogger.logMessageNormal(
requestData,
bookAddPageURL + ": " + "HTTPOK"
);
Book newBook = new Book(); Book newBook = new Book();
dataModel.addAttribute("book", newBook); dataModel.addAttribute("book", newBook);
dataModel.addAttribute("categories", categoryRepository.findAll()); dataModel.addAttribute("categories", categoryRepository.findAll());
@ -86,6 +82,11 @@ public class BookController {
newBook.setYear(Year.now().getValue()); newBook.setYear(Year.now().getValue());
} }
httpServerLogger.logMessageNormal(
requestData,
bookAddPageURL + ": " + "HTTPOK"
);
return bookAddPageURL; return bookAddPageURL;
} }
@ -96,6 +97,7 @@ public class BookController {
public String webFormSaveNewBook( public String webFormSaveNewBook(
@Valid @ModelAttribute("book") Book book, @Valid @ModelAttribute("book") Book book,
BindingResult bindingResult, BindingResult bindingResult,
Model dataModel,
HttpServletRequest requestData HttpServletRequest requestData
) { ) {
@ -104,13 +106,13 @@ public class BookController {
return bookAddPageURL; return bookAddPageURL;
} }
bookRepository.save(book);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookAddPageURL + ": " + "HTTPOK" bookAddPageURL + ": " + "HTTPOK"
); );
bookRepository.save(book);
return "redirect:" + bookListPageURL; return "redirect:" + bookListPageURL;
} }
@ -126,13 +128,13 @@ public class BookController {
HttpServletRequest requestData HttpServletRequest requestData
) { ) {
bookRepository.deleteById(bookId);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookDeletePageURL + ": " + "HTTPOK" bookDeletePageURL + ": " + "HTTPOK"
); );
bookRepository.deleteById(bookId);
return "redirect:../" + bookListPageURL; return "redirect:../" + bookListPageURL;
} }
@ -141,7 +143,7 @@ public class BookController {
@RequestMapping( @RequestMapping(
value = bookEditPageURL + "/{id}", value = bookEditPageURL + "/{id}",
method = RequestMethod.GET
method = { RequestMethod.GET }
) )
public String webFormEditBook( public String webFormEditBook(
@PathVariable("id") long bookId, @PathVariable("id") long bookId,
@ -149,40 +151,50 @@ public class BookController {
HttpServletRequest requestData HttpServletRequest requestData
) { ) {
Book book = bookRepository.findById(bookId).get();
Iterable<Category> categories = categoryRepository.findAll();
dataModel.addAttribute("book", book);
dataModel.addAttribute("categories", categories);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookEditPageURL + ": " + "HTTPOK" bookEditPageURL + ": " + "HTTPOK"
); );
Book book = bookRepository.findById(bookId).get();
dataModel.addAttribute("book", book);
dataModel.addAttribute("categories", categoryRepository.findAll());
return bookEditPageURL; return bookEditPageURL;
} }
/* NOTE: We keep Id here for the sake of proper URL formatting.
* Keep URL even if the POST request has invalid data.
* Do actual modifications by the book *object*, though.
* Internally, we never use URL id as a reference for user modifications,
* but just as an URL end point.
*/
@RequestMapping( @RequestMapping(
value = bookEditPageURL + "/{id}",
value = bookEditPageURL + "/{id}",
method = RequestMethod.POST method = RequestMethod.POST
) )
public String webFormUpdateBook( public String webFormUpdateBook(
@Valid @ModelAttribute("book") Book book, @Valid @ModelAttribute("book") Book book,
BindingResult bindingResult, BindingResult bindingResult,
@PathVariable("id") long bookId,
HttpServletRequest requestData HttpServletRequest requestData
) { ) {
bookId = book.getId();
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
httpServerLogger.commonError("Book edit: error " + book.toString(), requestData); httpServerLogger.commonError("Book edit: error " + book.toString(), requestData);
return bookEditPageURL; return bookEditPageURL;
} }
bookRepository.save(book);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookEditPageURL + ": " + "HTTPOK" bookEditPageURL + ": " + "HTTPOK"
); );
bookRepository.save(book);
return "redirect:../" + bookListPageURL; return "redirect:../" + bookListPageURL;
} }


Loading…
Cancel
Save