Skip to content

Commit b532bf4

Browse files
author
Sorin Zamfir
committed
BAEL-3777: Improved CLI example
1 parent 97b6282 commit b532bf4

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
package com.baeldung.dddhexagonalspring;
22

3-
import java.math.BigDecimal;
4-
import java.util.UUID;
5-
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
83
import org.springframework.beans.factory.annotation.Autowired;
94
import org.springframework.boot.CommandLineRunner;
105
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.WebApplicationType;
117
import org.springframework.boot.autoconfigure.SpringBootApplication;
128
import org.springframework.context.ConfigurableApplicationContext;
139
import org.springframework.context.annotation.PropertySource;
1410

1511
import com.baeldung.dddhexagonalspring.application.cli.CliOrderController;
16-
import com.baeldung.dddhexagonalspring.domain.Product;
1712

1813
@SpringBootApplication
1914
@PropertySource(value = { "classpath:ddd-layers.properties" })
2015
public class DomainLayerApplication implements CommandLineRunner {
21-
private static final Logger LOG = LoggerFactory.getLogger(DomainLayerApplication.class);
2216

2317
public static void main(final String[] args) {
2418
SpringApplication application = new SpringApplication(DomainLayerApplication.class);
@@ -35,15 +29,9 @@ public static void main(final String[] args) {
3529

3630
@Override
3731
public void run(String... args) throws Exception {
38-
LOG.info("Placing a new CLI order with two products");
39-
Product mobilePhone = new Product(UUID.randomUUID(), BigDecimal.valueOf(200), "mobile");
40-
Product razor = new Product(UUID.randomUUID(), BigDecimal.valueOf(50), "razor");
41-
LOG.info("Creating order with mobile phone");
42-
UUID orderId = orderController.createOrder(mobilePhone);
43-
LOG.info("Adding a razor to the order");
44-
orderController.addProduct(orderId, razor);
45-
LOG.info("Completing order");
46-
orderController.completeOrder(orderId);
47-
LOG.info("Order placement complete");
32+
orderController.createCompleteOrder();
33+
orderController.createIncompleteOrder();
34+
// uncomment to stop the context when execution is done
35+
// context.close();
4836
}
4937
}
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.baeldung.dddhexagonalspring.application.cli;
22

3+
import java.math.BigDecimal;
34
import java.util.UUID;
45

6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
58
import org.springframework.beans.factory.annotation.Autowired;
69
import org.springframework.stereotype.Component;
710

811
import com.baeldung.dddhexagonalspring.domain.Product;
912
import com.baeldung.dddhexagonalspring.domain.service.OrderService;
1013

1114
@Component
12-
public class CliOrderController{
15+
public class CliOrderController {
16+
17+
private static final Logger LOG = LoggerFactory.getLogger(CliOrderController.class);
1318

1419
private final OrderService orderService;
1520

@@ -18,20 +23,25 @@ public CliOrderController(OrderService orderService) {
1823
this.orderService = orderService;
1924
}
2025

21-
public UUID createOrder(Product product) {
22-
return orderService.createOrder(product);
23-
}
24-
25-
public void addProduct(UUID orderId, Product product) {
26-
orderService.addProduct(orderId, product);
26+
public void createCompleteOrder() {
27+
LOG.info("<<Create complete order>>");
28+
UUID orderId = createOrder();
29+
orderService.completeOrder(orderId);
2730
}
2831

29-
public void deleteProduct(UUID orderId, UUID productId) {
30-
orderService.deleteProduct(orderId, productId);
32+
public void createIncompleteOrder() {
33+
LOG.info("<<Create incomplete order>>");
34+
UUID orderId = createOrder();
3135
}
3236

33-
public void completeOrder(UUID orderId) {
34-
orderService.completeOrder(orderId);
37+
private UUID createOrder() {
38+
LOG.info("Placing a new order with two products");
39+
Product mobilePhone = new Product(UUID.randomUUID(), BigDecimal.valueOf(200), "mobile");
40+
Product razor = new Product(UUID.randomUUID(), BigDecimal.valueOf(50), "razor");
41+
LOG.info("Creating order with mobile phone");
42+
UUID orderId = orderService.createOrder(mobilePhone);
43+
LOG.info("Adding a razor to the order");
44+
orderService.addProduct(orderId, razor);
45+
return orderId;
3546
}
36-
3747
}

0 commit comments

Comments
 (0)