From 7449b8fafaa66cc6a685961c8dc058c32c5b3640 Mon Sep 17 00:00:00 2001 From: Gregor Date: Sun, 20 Oct 2019 21:56:17 +0200 Subject: [PATCH] Implemented Queried API call example --- .../com/kumuluz/ee/samples/rest/CustomerResource.java | 11 +++++++++-- .../com/kumuluz/ee/samples/rest/CustomerService.java | 10 +++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerResource.java b/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerResource.java index a09b976e..84109bdb 100644 --- a/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerResource.java +++ b/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerResource.java @@ -20,6 +20,7 @@ */ package com.kumuluz.ee.samples.rest; +import com.kumuluz.ee.rest.beans.Queried; import com.kumuluz.ee.rest.beans.QueryParameters; import javax.enterprise.context.RequestScoped; @@ -30,6 +31,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import java.util.List; +import java.util.stream.Collectors; /** * @author Benjamin Kastelic, Marko Skrjanec @@ -49,8 +51,13 @@ public class CustomerResource { @GET public Response getAllCustomers() { - List customers = customerBean.getCustomers(createQuery()); - return Response.ok(customers).build(); + Queried customers = customerBean.getCustomers(createQuery()); + + List response = customers.stream() + //.map() // my api object mapper + .collect(Collectors.toList()); + + return Response.ok(response).header("total", customers.getTotalCount()).build(); } @GET diff --git a/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerService.java b/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerService.java index 8bc9d830..3f5cd073 100644 --- a/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerService.java +++ b/kumuluzee-rest/src/main/java/com/kumuluz/ee/samples/rest/CustomerService.java @@ -17,9 +17,10 @@ * out of or in connection with the software or the use or other dealings in the * software. See the License for the specific language governing permissions and * limitations under the License. -*/ + */ package com.kumuluz.ee.samples.rest; +import com.kumuluz.ee.rest.beans.Queried; import com.kumuluz.ee.rest.beans.QueryParameters; import com.kumuluz.ee.rest.utils.JPAUtils; @@ -27,7 +28,6 @@ import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.Transactional; -import java.util.List; /** * @author Benjamin Kastelic, Marko Skrjanec @@ -43,9 +43,9 @@ public Customer getCustomer(String customerId) { return em.find(Customer.class, customerId); } - public List getCustomers(QueryParameters query) { - List customers = JPAUtils.queryEntities(em, Customer.class, query); - return customers; + public Queried getCustomers(QueryParameters query) { + Queried customersQueried = JPAUtils.getQueried(em, Customer.class, query); + return customersQueried; }