diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java index 19d4718..75f6367 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java @@ -10,6 +10,14 @@ public interface BookRepository extends CrudRepository { public List 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 findByIsbn(String isbn); + public Book findByIsbn(String isbn); + public boolean existsByIsbn(String isbn); } \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index 9e6dc2d..7d1e37d 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -209,6 +209,14 @@ public class BookController { 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()) { responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST); httpServerLogger.log(requestData, responseData);