Browse Source

Un-hardcode more messages

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.3-alpha
Pekka Helenius 4 years ago
parent
commit
28afed3c9f
1 changed files with 34 additions and 3 deletions
  1. +34
    -3
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

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

@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
@ -59,6 +60,9 @@ public class BookController {
@Autowired @Autowired
private Environment env; private Environment env;
@Autowired
private MessageSource msg;
@Autowired @Autowired
private CategoryRepository categoryRepository; private CategoryRepository categoryRepository;
@ -194,7 +198,16 @@ public class BookController {
// TODO consider better solution. Add custom Hibernate annotation for Book class? // TODO consider better solution. Add custom Hibernate annotation for Book class?
if (bookRepository.existsByIsbn(book.getIsbn())) { if (bookRepository.existsByIsbn(book.getIsbn())) {
bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists");
bindingResult.rejectValue(
"isbn",
"error.user",
msg.getMessage(
"book.error.isbn.exists",
null,
"book.error.isbn.exists [placeholder]",
requestData.getLocale()
)
);
} }
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
@ -355,13 +368,31 @@ public class BookController {
Long bookId = bookHash.getBookId(); Long bookId = bookHash.getBookId();
if (bookId == null) { if (bookId == null) {
bindingResultBook.rejectValue("name", "error.user", "Unknown book");
bindingResultBook.rejectValue(
"name",
"error.user",
msg.getMessage(
"book.error.unknownbook",
null,
"book.error.unknownbook [placeholder]",
requestData.getLocale()
)
);
} else { } else {
// If existing ISBN value is not attached to the current book... // If existing ISBN value is not attached to the current book...
if (bookI != null) { if (bookI != null) {
if (bookI.getId() != bookId) { if (bookI.getId() != bookId) {
bindingResultBook.rejectValue("isbn", "error.user", "ISBN code already exists");
bindingResultBook.rejectValue(
"isbn",
"error.user",
msg.getMessage(
"book.error.isbn.exists",
null,
"book.error.isbn.exists [placeholder]",
requestData.getLocale()
)
);
} }
} }


Loading…
Cancel
Save