|
1 | 1 | package org.infinispan.inmemory;
|
2 | 2 |
|
3 |
| -import io.quarkus.runtime.StartupEvent; |
| 3 | +import org.infinispan.client.hotrod.RemoteCache; |
4 | 4 | import org.infinispan.client.hotrod.RemoteCacheManager;
|
5 |
| -import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; |
6 |
| -import org.jboss.logging.Logger; |
| 5 | +import org.infinispan.client.hotrod.Search; |
| 6 | +import org.infinispan.inmemory.config.InmemoryCatalogueConfig; |
| 7 | +import org.infinispan.inmemory.schema.PurchasedProductKey; |
| 8 | +import org.infinispan.inmemory.schema.PurchasedProductValue; |
| 9 | +import org.infinispan.inmemory.schema.RetailProductValue; |
| 10 | +import org.infinispan.query.dsl.Query; |
| 11 | +import org.infinispan.query.dsl.QueryFactory; |
7 | 12 |
|
8 | 13 | import javax.enterprise.context.ApplicationScoped;
|
9 |
| -import javax.enterprise.event.Observes; |
10 | 14 | import javax.inject.Inject;
|
11 | 15 | import javax.ws.rs.Consumes;
|
12 | 16 | import javax.ws.rs.GET;
|
13 | 17 | import javax.ws.rs.Path;
|
| 18 | +import javax.ws.rs.PathParam; |
14 | 19 | import javax.ws.rs.Produces;
|
| 20 | +import javax.ws.rs.QueryParam; |
15 | 21 | import javax.ws.rs.core.MediaType;
|
16 | 22 | import javax.ws.rs.core.Response;
|
| 23 | +import javax.ws.rs.core.Response.Status; |
| 24 | +import java.util.List; |
17 | 25 |
|
18 | 26 | @ApplicationScoped
|
19 |
| -@Path("/products") |
20 | 27 | @Produces(MediaType.APPLICATION_JSON)
|
21 | 28 | @Consumes(MediaType.APPLICATION_JSON)
|
| 29 | +@Path("/") |
22 | 30 | public class InmemoryCatalogResource {
|
23 |
| - private static final Logger LOGGER = Logger.getLogger(InmemoryCatalogResource.class); |
24 |
| - |
25 |
| - void onStart(@Observes StartupEvent ev) { |
26 |
| - LOGGER.info("Infinispan SQL Store is starting Powered by Quarkus"); |
27 |
| - LOGGER.info(" _ _ _ _ _ _ _ _"); |
28 |
| - LOGGER.info(" / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\"); |
29 |
| - LOGGER.info("( S | q | l | S | t | o | r | e )"); |
30 |
| - LOGGER.info(" \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/"); |
| 31 | + final RemoteCache<Long, RetailProductValue> catalogue; |
| 32 | + final RemoteCache<PurchasedProductKey, PurchasedProductValue> soldProducts; |
| 33 | + |
| 34 | + @Inject public InmemoryCatalogResource(InmemoryCatalogueConfig inmemoryCatalogueConfig, RemoteCacheManager cacheManager) { |
| 35 | + this.catalogue = cacheManager.getCache(inmemoryCatalogueConfig.catalogCacheName()); |
| 36 | + this.soldProducts = cacheManager.getCache(inmemoryCatalogueConfig.soldProductsName()); |
| 37 | + } |
| 38 | + |
| 39 | + @GET |
| 40 | + public String health() { |
| 41 | + return String.format("Service is up! catalogue[%d] sold_products[%d]", catalogue.size(), soldProducts.size()); |
| 42 | + } |
| 43 | + |
| 44 | + @GET |
| 45 | + @Path("catalogue/{code}") |
| 46 | + public Response getByCode(@PathParam("code") String code) { |
| 47 | + String queryByCode = "from retail.RetailProductValue where code= :code"; |
| 48 | + QueryFactory queryFactory = Search.getQueryFactory(catalogue); |
| 49 | + Query<RetailProductValue> query = queryFactory.create(queryByCode); |
| 50 | + query.setParameter("code", code); |
| 51 | + query.maxResults(1); |
| 52 | + List<RetailProductValue> list = query.execute().list(); |
| 53 | + if (list.isEmpty()) { |
| 54 | + return Response.status(Status.NOT_FOUND).build(); |
| 55 | + } |
| 56 | + |
| 57 | + return Response.ok(list.get(0)).build(); |
31 | 58 | }
|
32 | 59 |
|
33 | 60 | @GET
|
34 |
| - public Response catalog() { |
35 |
| - return Response.ok("in progress").build(); |
| 61 | + @Path("catalogue") |
| 62 | + public Response queryCatalogue(@QueryParam("name") String name, |
| 63 | + @QueryParam("stock") Integer stock, |
| 64 | + @QueryParam("price-max") Integer priceMax, |
| 65 | + @QueryParam("price-min") Integer priceMin) { |
| 66 | + if (name == null) { |
| 67 | + name = "*"; |
| 68 | + } else { |
| 69 | + name = "*" + name + "*"; |
| 70 | + |
| 71 | + } |
| 72 | + String fullTextQuery = String.format("from retail.RetailProductValue where name: '%s'", name); |
| 73 | + |
| 74 | + if (stock != null) { |
| 75 | + fullTextQuery = fullTextQuery + String.format(" and stock >= %d", stock); |
| 76 | + } |
| 77 | + |
| 78 | + if (priceMin != null && priceMax != null) { |
| 79 | + fullTextQuery = fullTextQuery + String.format(" and price : [%d to %d]", priceMin, priceMax); |
| 80 | + } |
| 81 | + |
| 82 | + QueryFactory queryFactory = Search.getQueryFactory(catalogue); |
| 83 | + Query<RetailProductValue> query = queryFactory.create(fullTextQuery); |
| 84 | + query.maxResults(Integer.MAX_VALUE); // all. TODO: use pagination |
| 85 | + return Response.ok(query.execute().list()).build(); |
36 | 86 | }
|
37 | 87 |
|
| 88 | + @GET |
| 89 | + @Path("sales") |
| 90 | + public Response querySales(@QueryParam("name") String name, @QueryParam("country") String country) { |
| 91 | + if (name == null) { |
| 92 | + name = "*"; |
| 93 | + } else { |
| 94 | + name = "*" + name + "*"; |
| 95 | + } |
| 96 | + |
| 97 | + String fullTextQuery = "from retail.PurchasedProductValue where name: '" + name + "'"; |
| 98 | + |
| 99 | + if (country != null) { |
| 100 | + fullTextQuery = fullTextQuery + "and country='" + country +"'"; |
| 101 | + } |
| 102 | + QueryFactory queryFactory = Search.getQueryFactory(soldProducts); |
| 103 | + Query<RetailProductValue> query = queryFactory.create(fullTextQuery); |
| 104 | + query.maxResults(Integer.MAX_VALUE); // TODO: All. Use pagination |
| 105 | + return Response.ok(query.execute().list()).build(); |
| 106 | + } |
38 | 107 |
|
39 | 108 | }
|
0 commit comments