Skip to content

Commit bf9a113

Browse files
committed
Add missing ISBN value check for Edit web form
Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
1 parent 840a8f0 commit bf9a113

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ public interface BookRepository extends CrudRepository<Book, Long> {
1010

1111
public List<Book> findById(String title);
1212

13+
/* Assume a single book with a single ISBN, or multiple books with possibly duplicate ISBNs?
14+
* For meanwhile, we have a UNIQUE constraint for ISBN values. If this policy changes,
15+
* this method must be changed, as well. Since database allows only UNIQUE values for ISBNs
16+
* we return a single book.
17+
*/
18+
//public List<Book> findByIsbn(String isbn);
19+
public Book findByIsbn(String isbn);
20+
1321
public boolean existsByIsbn(String isbn);
1422

1523
}

bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ public String webFormUpdateBook(
209209
bindingResult.rejectValue("name", "error.user", "Wrong book");
210210
}
211211

212+
// TODO consider better solution. Add custom Hibernate annotation for Book class?
213+
Book bookI = bookRepository.findByIsbn(book.getIsbn());
214+
215+
// If existing ISBN value is not attached to the current book...
216+
if (bookI.getId() != book.getId()) {
217+
bindingResult.rejectValue("isbn", "error.user", "ISBN code already exists");
218+
}
219+
212220
if (bindingResult.hasErrors()) {
213221
responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);
214222
httpServerLogger.log(requestData, responseData);

0 commit comments

Comments
 (0)