From 1651eae80cf213c1fa511b5152ee85c21c202116 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Sat, 3 Oct 2020 03:40:02 +0300 Subject: [PATCH] Implement findAllPublished method for BookRepository Signed-off-by: Pekka Helenius --- .../model/book/BookRepositoryCustom.java | 3 +++ .../model/book/BookRepositoryImpl.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java index c93e555..a8ce905 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryCustom.java @@ -1,5 +1,7 @@ package com.fjordtek.bookstore.model.book; +import java.util.List; + /** * * @author Pekka Helenius @@ -9,4 +11,5 @@ public interface BookRepositoryCustom { void updateWithoutPrice(Book book); + List findAllPublished(); } \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java index 5f5d778..e2d248a 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/book/BookRepositoryImpl.java @@ -1,8 +1,11 @@ package com.fjordtek.bookstore.model.book; +import java.util.List; + import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; +import javax.persistence.TypedQuery; import org.springframework.transaction.annotation.Transactional; @@ -54,4 +57,25 @@ public class BookRepositoryImpl implements BookRepositoryCustom { } + /* + * Find all published books + */ + @Override + public List findAllPublished() { + try { + + TypedQuery query = entityManager.createQuery( + "SELECT i FROM Book i WHERE publish = 1", + Book.class + ); + + return query.getResultList(); + + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + } \ No newline at end of file