Skip to content

Commit 1af25c3

Browse files
author
Sorin Zamfir
committed
BAEL-3777: Included CLI mode
1 parent c109960 commit 1af25c3

5 files changed

Lines changed: 81 additions & 6 deletions

File tree

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
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;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.CommandLineRunner;
310
import org.springframework.boot.SpringApplication;
411
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
import org.springframework.context.ConfigurableApplicationContext;
513
import org.springframework.context.annotation.PropertySource;
614

15+
import com.baeldung.dddhexagonalspring.application.cli.CliOrderController;
16+
import com.baeldung.dddhexagonalspring.domain.Product;
17+
718
@SpringBootApplication
819
@PropertySource(value = { "classpath:ddd-layers.properties" })
9-
public class DomainLayerApplication {
20+
public class DomainLayerApplication implements CommandLineRunner {
21+
private static final Logger LOG = LoggerFactory.getLogger(DomainLayerApplication.class);
22+
1023
public static void main(final String[] args) {
11-
SpringApplication.run(DomainLayerApplication.class, args);
24+
SpringApplication application = new SpringApplication(DomainLayerApplication.class);
25+
// uncomment to run just the console application
26+
// application.setWebApplicationType(WebApplicationType.NONE);
27+
application.run(args);
28+
}
29+
30+
@Autowired
31+
public CliOrderController orderController;
32+
33+
@Autowired
34+
public ConfigurableApplicationContext context;
35+
36+
@Override
37+
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");
1248
}
1349
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.dddhexagonalspring.application.cli;
2+
3+
import java.util.UUID;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Component;
7+
8+
import com.baeldung.dddhexagonalspring.domain.Product;
9+
import com.baeldung.dddhexagonalspring.domain.service.OrderService;
10+
11+
@Component
12+
public class CliOrderController{
13+
14+
private final OrderService orderService;
15+
16+
@Autowired
17+
public CliOrderController(OrderService orderService) {
18+
this.orderService = orderService;
19+
}
20+
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);
27+
}
28+
29+
public void deleteProduct(UUID orderId, UUID productId) {
30+
orderService.deleteProduct(orderId, productId);
31+
32+
}
33+
34+
public void completeOrder(UUID orderId) {
35+
orderService.completeOrder(orderId);
36+
}
37+
38+
}

ddd/src/main/java/com/baeldung/dddhexagonalspring/application/controller/OrderController.java renamed to ddd/src/main/java/com/baeldung/dddhexagonalspring/application/rest/RestOrderController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.dddhexagonalspring.application.controller;
1+
package com.baeldung.dddhexagonalspring.application.rest;
22

33
import com.baeldung.dddhexagonalspring.application.request.AddProductRequest;
44
import com.baeldung.dddhexagonalspring.application.request.CreateOrderRequest;
@@ -12,12 +12,12 @@
1212

1313
@RestController
1414
@RequestMapping("/orders")
15-
public class OrderController {
15+
public class RestOrderController {
1616

1717
private final OrderService orderService;
1818

1919
@Autowired
20-
public OrderController(OrderService orderService) {
20+
public RestOrderController(OrderService orderService) {
2121
this.orderService = orderService;
2222
}
2323

ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/cassandra/CassandraDbOrderRepository.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.baeldung.dddhexagonalspring.domain.repository.OrderRepository;
1212

1313
@Component
14-
@Primary
1514
public class CassandraDbOrderRepository implements OrderRepository {
1615

1716
private final SpringDataCassandraOrderRepository orderRepository;

ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/mongo/MongoDbOrderRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import java.util.UUID;
55

66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.annotation.Primary;
78
import org.springframework.stereotype.Component;
89

910
import com.baeldung.dddhexagonalspring.domain.Order;
1011
import com.baeldung.dddhexagonalspring.domain.repository.OrderRepository;
1112

1213
@Component
14+
@Primary
1315
public class MongoDbOrderRepository implements OrderRepository {
1416

1517
private final SpringDataMongoOrderRepository orderRepository;

0 commit comments

Comments
 (0)