Skip to content

Commit 196c4b8

Browse files
committed
Added JSON and XML examples
1 parent 6c306c3 commit 196c4b8

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<properties>
1818
<java.version>17</java.version>
1919
<vaadin.version>22.0.2</vaadin.version>
20+
<jooq.version>3.15.5</jooq.version>
2021
</properties>
2122

2223
<dependencyManagement>

src/main/java/io/seventytwo/demo/api/order/OrderControllerV2.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@ select json_agg(po) as orders
3838
@GetMapping(value = "/api/v2/orders", produces = MediaType.APPLICATION_XML_VALUE)
3939
public String getOrdersAsXml(@RequestParam Integer customerId) {
4040
return dslContext.fetchOne("""
41-
select xmlelement(
42-
name "orders",
43-
xmlagg(xmlelement(name "order",
44-
xmlattributes(p.id, p.order_date),
45-
xmlelement(name "items",
46-
(select xmlagg(xmlelement(name "item",
47-
xmlattributes(i.id, i.quantity, pr.name)))
48-
from order_item i
49-
join product pr on i.product_id = pr.id
50-
where i.order_id = p.id)))
51-
)
52-
)
41+
select xmlelement(name "orders",
42+
xmlagg(xmlelement(name "order",
43+
xmlattributes(p.id, p.order_date),
44+
xmlelement(name "items",
45+
(select xmlagg(xmlelement(name "item",
46+
xmlattributes(i.id, i.quantity, pr.name)))
47+
from order_item i
48+
join product pr on i.product_id = pr.id
49+
where i.order_id = p.id)
50+
)
51+
)
52+
)
53+
)
5354
from purchase_order p
5455
where p.customer_id = ?
5556
""", customerId).into(String.class);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.seventytwo.demo.api.order;
2+
3+
import org.jooq.DSLContext;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.util.List;
10+
11+
import static io.seventytwo.demo.database.tables.OrderItem.ORDER_ITEM;
12+
import static io.seventytwo.demo.database.tables.Product.PRODUCT;
13+
import static io.seventytwo.demo.database.tables.PurchaseOrder.PURCHASE_ORDER;
14+
import static org.jooq.Records.mapping;
15+
import static org.jooq.impl.DSL.multiset;
16+
import static org.jooq.impl.DSL.select;
17+
18+
@RestController
19+
public class OrderControllerV3 {
20+
21+
private final DSLContext dslContext;
22+
23+
public OrderControllerV3(DSLContext dslContext) {
24+
this.dslContext = dslContext;
25+
}
26+
27+
@GetMapping(value = "/api/v3/orders", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
28+
public List<OrderDTO> getOrdersAsRecords(@RequestParam Integer customerId) {
29+
return dslContext
30+
.select(PURCHASE_ORDER.ID,
31+
PURCHASE_ORDER.ORDER_DATE,
32+
multiset(
33+
select(ORDER_ITEM.ID, ORDER_ITEM.QUANTITY, PRODUCT.NAME)
34+
.from(ORDER_ITEM)
35+
.join(PRODUCT).on(PRODUCT.ID.eq(ORDER_ITEM.PRODUCT_ID))
36+
.where(ORDER_ITEM.ORDER_ID.eq(PURCHASE_ORDER.ID))
37+
).convertFrom(r -> r.into(OrderDTO.OrderItemDTO.class))
38+
)
39+
.from(PURCHASE_ORDER)
40+
.where(PURCHASE_ORDER.CUSTOMER_ID.eq(customerId))
41+
.fetch(mapping(OrderDTO::new));
42+
}
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.seventytwo.demo.api.order;
2+
3+
import java.time.LocalDate;
4+
import java.util.List;
5+
6+
public record OrderDTO(Integer id, LocalDate orderDate, List<OrderItemDTO> items) {
7+
8+
public record OrderItemDTO(Integer id, Integer quantity, String productName) {
9+
}
10+
}

src/test/api/orders.http

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,16 @@ Accept: application/json
1212
# GET oders for customer v2 as XML
1313
GET http://localhost:8080/api/v2/orders?customerId=1128
1414
Accept: application/xml
15+
16+
###
17+
18+
# GET oders for customer v3 as JSON
19+
GET http://localhost:8080/api/v3/orders?customerId=1128
20+
Accept: application/json
21+
22+
###
23+
24+
# GET oders for customer v3 as XML
25+
GET http://localhost:8080/api/v3/orders?customerId=1128
26+
Accept: application/xml
27+

0 commit comments

Comments
 (0)