Browse Source

Fix categories not showing up after invalid edits; Rename view

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

+ 41
- 30
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -27,15 +27,14 @@ import com.fjordtek.bookstore.model.CategoryRepository;
@Controller @Controller
public class BookController { 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 bookEditPageURL = "bookedit";
protected static final String bookSavePageURL = "booksave";
private static final String landingPageView = "index";
private static final String bookListPageView = "booklist";
private static final String bookAddPageView = "bookadd";
private static final String bookDeletePageView = "bookdelete";
private static final String bookEditPageView = "bookedit";
private HttpServerLogger httpServerLogger = new HttpServerLogger(); private HttpServerLogger httpServerLogger = new HttpServerLogger();
private HttpExceptionHandler httpExceptionHandler = new HttpExceptionHandler();
//private HttpExceptionHandler httpExceptionHandler = new HttpExceptionHandler();
@Autowired @Autowired
private BookRepository bookRepository; private BookRepository bookRepository;
@ -46,19 +45,23 @@ public class BookController {
////////////////////////////// //////////////////////////////
// LIST PAGE // LIST PAGE
@RequestMapping( @RequestMapping(
value = bookListPageURL,
value = bookListPageView,
method = { RequestMethod.GET, RequestMethod.POST } method = { RequestMethod.GET, RequestMethod.POST }
) )
public String defaultWebFormGet(HttpServletRequest requestData, Model dataModel) { public String defaultWebFormGet(HttpServletRequest requestData, Model dataModel) {
dataModel.addAttribute("books", bookRepository.findAll()); dataModel.addAttribute("books", bookRepository.findAll());
dataModel.addAttribute("deletepage", bookDeletePageView);
dataModel.addAttribute("editpage", bookEditPageView);
dataModel.addAttribute("addpage", bookAddPageView);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookListPageURL + ": " + "HTTPOK"
bookListPageView + ": " + "HTTPOK"
); );
return bookListPageURL;
return bookListPageView;
} }
@ -66,7 +69,7 @@ public class BookController {
// ADD BOOK // ADD BOOK
@RequestMapping( @RequestMapping(
value = bookAddPageURL,
value = bookAddPageView,
method = { RequestMethod.GET, RequestMethod.PUT } method = { RequestMethod.GET, RequestMethod.PUT }
) )
public String webFormAddBook( public String webFormAddBook(
@ -78,20 +81,23 @@ public class BookController {
dataModel.addAttribute("book", newBook); dataModel.addAttribute("book", newBook);
dataModel.addAttribute("categories", categoryRepository.findAll()); dataModel.addAttribute("categories", categoryRepository.findAll());
dataModel.addAttribute("addpage", bookAddPageView);
dataModel.addAttribute("listpage", bookListPageView);
if (newBook.getYear() == 0) { if (newBook.getYear() == 0) {
newBook.setYear(Year.now().getValue()); newBook.setYear(Year.now().getValue());
} }
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookAddPageURL + ": " + "HTTPOK"
bookAddPageView + ": " + "HTTPOK"
); );
return bookAddPageURL;
return bookAddPageView;
} }
@RequestMapping( @RequestMapping(
value = bookAddPageURL,
value = bookAddPageView,
method = RequestMethod.POST method = RequestMethod.POST
) )
public String webFormSaveNewBook( public String webFormSaveNewBook(
@ -103,24 +109,24 @@ public class BookController {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
httpServerLogger.commonError("Book add: error " + book.toString(), requestData); httpServerLogger.commonError("Book add: error " + book.toString(), requestData);
return bookAddPageURL;
return bookAddPageView;
} }
bookRepository.save(book); bookRepository.save(book);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookAddPageURL + ": " + "HTTPOK"
bookAddPageView + ": " + "HTTPOK"
); );
return "redirect:" + bookListPageURL;
return "redirect:" + bookListPageView;
} }
////////////////////////////// //////////////////////////////
// DELETE BOOK // DELETE BOOK
@RequestMapping( @RequestMapping(
value = bookDeletePageURL + "/{id}",
value = bookDeletePageView + "/{id}",
method = RequestMethod.GET method = RequestMethod.GET
) )
public String webFormDeleteBook( public String webFormDeleteBook(
@ -132,17 +138,17 @@ public class BookController {
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookDeletePageURL + ": " + "HTTPOK"
bookDeletePageView + ": " + "HTTPOK"
); );
return "redirect:../" + bookListPageURL;
return "redirect:../" + bookListPageView;
} }
////////////////////////////// //////////////////////////////
// UPDATE BOOK // UPDATE BOOK
@RequestMapping( @RequestMapping(
value = bookEditPageURL + "/{id}",
value = bookEditPageView + "/{id}",
method = { RequestMethod.GET } method = { RequestMethod.GET }
) )
public String webFormEditBook( public String webFormEditBook(
@ -156,12 +162,14 @@ public class BookController {
dataModel.addAttribute("book", book); dataModel.addAttribute("book", book);
dataModel.addAttribute("categories", categories); dataModel.addAttribute("categories", categories);
dataModel.addAttribute("listpage", bookListPageView);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookEditPageURL + ": " + "HTTPOK"
bookEditPageView + ": " + "HTTPOK"
); );
return bookEditPageURL;
return bookEditPageView;
} }
/* NOTE: We keep Id here for the sake of proper URL formatting. /* NOTE: We keep Id here for the sake of proper URL formatting.
@ -171,43 +179,46 @@ public class BookController {
* but just as an URL end point. * but just as an URL end point.
*/ */
@RequestMapping( @RequestMapping(
value = bookEditPageURL + "/{id}",
value = bookEditPageView + "/{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,
Model dataModel,
@PathVariable("id") Long bookId, @PathVariable("id") Long bookId,
HttpServletRequest requestData HttpServletRequest requestData
) { ) {
bookId = book.getId(); bookId = book.getId();
Iterable<Category> categories = categoryRepository.findAll();
dataModel.addAttribute("categories", categories);
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 bookEditPageView;
} }
bookRepository.save(book); bookRepository.save(book);
httpServerLogger.logMessageNormal( httpServerLogger.logMessageNormal(
requestData, requestData,
bookEditPageURL + ": " + "HTTPOK"
bookEditPageView + ": " + "HTTPOK"
); );
return "redirect:../" + bookListPageURL;
return "redirect:../" + bookListPageView;
} }
////////////////////////////// //////////////////////////////
// REDIRECTS // REDIRECTS
@RequestMapping( @RequestMapping(
value = { "/", landingPageURL },
value = { "/", landingPageView },
method = RequestMethod.GET method = RequestMethod.GET
) )
@ResponseStatus(HttpStatus.FOUND) @ResponseStatus(HttpStatus.FOUND)
public String redirectToDefaultWebForm() { public String redirectToDefaultWebForm() {
return "redirect:" + bookListPageURL;
return "redirect:" + bookListPageView;
} }
// Other URL requests // Other URL requests
@ -216,7 +227,7 @@ public class BookController {
) )
public String errorWebForm(HttpServletRequest requestData) { public String errorWebForm(HttpServletRequest requestData) {
//return httpExceptionHandler.notFoundErrorHandler(requestData); //return httpExceptionHandler.notFoundErrorHandler(requestData);
return "redirect:" + bookListPageURL;
return "redirect:" + bookListPageView;
} }
@RequestMapping( @RequestMapping(


Loading…
Cancel
Save