Browse Source

Add missing ISBN value check for Edit web form

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
bf9a113078
2 changed files with 16 additions and 0 deletions
  1. +8
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java
  2. +8
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

+ 8
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java View File

@ -10,6 +10,14 @@ public interface BookRepository extends CrudRepository<Book, Long> {
public List<Book> findById(String title); public List<Book> findById(String title);
/* Assume a single book with a single ISBN, or multiple books with possibly duplicate ISBNs?
* For meanwhile, we have a UNIQUE constraint for ISBN values. If this policy changes,
* this method must be changed, as well. Since database allows only UNIQUE values for ISBNs
* we return a single book.
*/
//public List<Book> findByIsbn(String isbn);
public Book findByIsbn(String isbn);
public boolean existsByIsbn(String isbn); public boolean existsByIsbn(String isbn);
} }

+ 8
- 0
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -209,6 +209,14 @@ public class BookController {
bindingResult.rejectValue("name", "error.user", "Wrong book"); bindingResult.rejectValue("name", "error.user", "Wrong book");
} }
// TODO consider better solution. Add custom Hibernate annotation for Book class?
Book bookI = bookRepository.findByIsbn(book.getIsbn());
// If existing ISBN value is not attached to the current book...
if (bookI.getId() != book.getId()) {
bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists");
}
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST); responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);


Loading…
Cancel
Save