|
1 | 1 | package org.infinispan.retail;
|
2 | 2 |
|
3 |
| -import io.quarkus.hibernate.orm.panache.PanacheQuery; |
4 |
| -import io.quarkus.panache.common.Sort; |
5 |
| -import io.quarkus.runtime.StartupEvent; |
| 3 | +import org.infinispan.retail.model.Customer; |
| 4 | +import org.infinispan.retail.model.CustomerCommand; |
6 | 5 | import org.infinispan.retail.model.RetailProduct;
|
7 | 6 | import org.jboss.logging.Logger;
|
8 | 7 |
|
9 | 8 | import javax.enterprise.context.ApplicationScoped;
|
10 |
| -import javax.enterprise.event.Observes; |
11 | 9 | import javax.transaction.Transactional;
|
12 |
| -import javax.ws.rs.Consumes; |
13 |
| -import javax.ws.rs.DELETE; |
14 |
| -import javax.ws.rs.GET; |
15 |
| -import javax.ws.rs.PATCH; |
16 |
| -import javax.ws.rs.POST; |
17 |
| -import javax.ws.rs.PUT; |
18 |
| -import javax.ws.rs.Path; |
19 |
| -import javax.ws.rs.PathParam; |
20 |
| -import javax.ws.rs.Produces; |
21 |
| -import javax.ws.rs.QueryParam; |
22 |
| -import javax.ws.rs.core.Context; |
23 |
| -import javax.ws.rs.core.MediaType; |
24 |
| -import javax.ws.rs.core.Response; |
25 |
| -import javax.ws.rs.core.UriInfo; |
26 | 10 | import java.util.List;
|
27 | 11 |
|
28 | 12 | @ApplicationScoped
|
29 |
| -@Path("/commands") |
30 |
| -@Produces(MediaType.APPLICATION_JSON) |
31 |
| -@Consumes(MediaType.APPLICATION_JSON) |
32 |
| -public class CommandResource { |
| 13 | +public class CommandLoader { |
33 | 14 |
|
34 |
| - @GET |
35 |
| - public List<RetailProduct> catalog(@QueryParam("page") Integer page, @QueryParam("size") Integer size) { |
36 |
| - if (page == null && size == null) { |
37 |
| - return RetailProduct.listAll(); |
38 |
| - } |
39 |
| - PanacheQuery<RetailProduct> query = RetailProduct.findAll(Sort.by("name")); |
40 |
| - query.page(page, size); |
41 |
| - return query.list(); |
42 |
| - } |
43 |
| - |
44 |
| - @GET |
45 |
| - @Path("/{code}") |
46 |
| - public Response getRetailProductByCode(@PathParam("code") String code) { |
47 |
| - RetailProduct retailProduct = RetailProduct.findByCode(code); |
48 |
| - return Response.ok(retailProduct).build(); |
49 |
| - } |
50 |
| - |
51 |
| - @POST |
52 |
| - @Transactional |
53 |
| - public Response create(RetailProduct retailProduct, @Context UriInfo uriInfo) { |
54 |
| - retailProduct.persist(); |
55 |
| - return Response.created(uriInfo.getAbsolutePathBuilder().path(retailProduct.code).build()).build(); |
56 |
| - } |
| 15 | + private static final Logger LOGGER = Logger.getLogger(CommandLoader.class); |
57 | 16 |
|
58 |
| - @PUT |
59 |
| - @Path("/{code}") |
60 | 17 | @Transactional
|
61 |
| - public Response update(@PathParam("code") String sku, RetailProduct retailProduct) { |
62 |
| - RetailProduct existingProduct = RetailProduct.findByCode(sku); |
63 |
| - existingProduct.name = retailProduct.name; |
64 |
| - existingProduct.price = retailProduct.price; |
65 |
| - existingProduct.stock = retailProduct.stock; |
66 |
| - existingProduct.persist(); |
67 |
| - return Response.ok(existingProduct).build(); |
68 |
| - } |
| 18 | + public void initCommands() { |
| 19 | + LOGGER.info("Creating commands"); |
| 20 | + List<RetailProduct> products = RetailProduct.listAll(); |
| 21 | + List<Customer> customers = Customer.listAll(); |
69 | 22 |
|
70 |
| - @PATCH |
71 |
| - @Path("/{code}") |
72 |
| - @Transactional |
73 |
| - public Response updateStock(@PathParam("code") String code, @QueryParam("stock") Integer stock) { |
74 |
| - int currentStock = RetailProduct.findCurrentStock(code); |
75 |
| - RetailProduct.update("stock = ?1 where code= ?2", currentStock + stock, code); |
76 |
| - return Response.ok().build(); |
77 |
| - } |
| 23 | + CustomerCommand customerCommand = new CustomerCommand(); |
| 24 | + customerCommand.promotion = true; |
| 25 | + customerCommand.buyer = customers.get(0); |
| 26 | + customerCommand.products = products; |
78 | 27 |
|
79 |
| - @DELETE |
80 |
| - @Path("/{code}") |
81 |
| - @Transactional |
82 |
| - public Response delete(@PathParam("code") String code) { |
83 |
| - RetailProduct.delete("code", code); |
84 |
| - return Response.ok().build(); |
| 28 | + customerCommand.persist(); |
85 | 29 | }
|
86 | 30 |
|
87 | 31 | }
|
0 commit comments