From a8617b62fa2d6eec253a87ed23b2736fecbc2135 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sat, 1 Feb 2020 15:50:49 +0200 Subject: [PATCH] BAEL-3777: Included CLI mode --- .../DomainLayerApplication.java | 40 ++++++++++++++++++- .../application/cli/CliOrderController.java | 38 ++++++++++++++++++ .../RestOrderController.java} | 6 +-- .../cassandra/CassandraDbOrderRepository.java | 1 - .../mongo/MongoDbOrderRepository.java | 2 + 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 ddd/src/main/java/com/baeldung/dddhexagonalspring/application/cli/CliOrderController.java rename ddd/src/main/java/com/baeldung/dddhexagonalspring/application/{controller/OrderController.java => rest/RestOrderController.java} (91%) diff --git a/ddd/src/main/java/com/baeldung/dddhexagonalspring/DomainLayerApplication.java b/ddd/src/main/java/com/baeldung/dddhexagonalspring/DomainLayerApplication.java index 988f96042b..10fbc9c2f5 100644 --- a/ddd/src/main/java/com/baeldung/dddhexagonalspring/DomainLayerApplication.java +++ b/ddd/src/main/java/com/baeldung/dddhexagonalspring/DomainLayerApplication.java @@ -1,13 +1,49 @@ package com.baeldung.dddhexagonalspring; +import java.math.BigDecimal; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.PropertySource; +import com.baeldung.dddhexagonalspring.application.cli.CliOrderController; +import com.baeldung.dddhexagonalspring.domain.Product; + @SpringBootApplication @PropertySource(value = { "classpath:ddd-layers.properties" }) -public class DomainLayerApplication { +public class DomainLayerApplication implements CommandLineRunner { + private static final Logger LOG = LoggerFactory.getLogger(DomainLayerApplication.class); + public static void main(final String[] args) { - SpringApplication.run(DomainLayerApplication.class, args); + SpringApplication application = new SpringApplication(DomainLayerApplication.class); + // uncomment to run just the console application + // application.setWebApplicationType(WebApplicationType.NONE); + application.run(args); + } + + @Autowired + public CliOrderController orderController; + + @Autowired + public ConfigurableApplicationContext context; + + @Override + public void run(String... args) throws Exception { + LOG.info("Placing a new CLI order with two products"); + Product mobilePhone = new Product(UUID.randomUUID(), BigDecimal.valueOf(200), "mobile"); + Product razor = new Product(UUID.randomUUID(), BigDecimal.valueOf(50), "razor"); + LOG.info("Creating order with mobile phone"); + UUID orderId = orderController.createOrder(mobilePhone); + LOG.info("Adding a razor to the order"); + orderController.addProduct(orderId, razor); + LOG.info("Completing order"); + orderController.completeOrder(orderId); + LOG.info("Order placement complete"); } } diff --git a/ddd/src/main/java/com/baeldung/dddhexagonalspring/application/cli/CliOrderController.java b/ddd/src/main/java/com/baeldung/dddhexagonalspring/application/cli/CliOrderController.java new file mode 100644 index 0000000000..cf073f61c8 --- /dev/null +++ b/ddd/src/main/java/com/baeldung/dddhexagonalspring/application/cli/CliOrderController.java @@ -0,0 +1,38 @@ +package com.baeldung.dddhexagonalspring.application.cli; + +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.dddhexagonalspring.domain.Product; +import com.baeldung.dddhexagonalspring.domain.service.OrderService; + +@Component +public class CliOrderController{ + + private final OrderService orderService; + + @Autowired + public CliOrderController(OrderService orderService) { + this.orderService = orderService; + } + + public UUID createOrder(Product product) { + return orderService.createOrder(product); + } + + public void addProduct(UUID orderId, Product product) { + orderService.addProduct(orderId, product); + } + + public void deleteProduct(UUID orderId, UUID productId) { + orderService.deleteProduct(orderId, productId); + + } + + public void completeOrder(UUID orderId) { + orderService.completeOrder(orderId); + } + +} diff --git a/ddd/src/main/java/com/baeldung/dddhexagonalspring/application/controller/OrderController.java b/ddd/src/main/java/com/baeldung/dddhexagonalspring/application/rest/RestOrderController.java similarity index 91% rename from ddd/src/main/java/com/baeldung/dddhexagonalspring/application/controller/OrderController.java rename to ddd/src/main/java/com/baeldung/dddhexagonalspring/application/rest/RestOrderController.java index 80ba36d01b..9fcec44a30 100644 --- a/ddd/src/main/java/com/baeldung/dddhexagonalspring/application/controller/OrderController.java +++ b/ddd/src/main/java/com/baeldung/dddhexagonalspring/application/rest/RestOrderController.java @@ -1,4 +1,4 @@ -package com.baeldung.dddhexagonalspring.application.controller; +package com.baeldung.dddhexagonalspring.application.rest; import com.baeldung.dddhexagonalspring.application.request.AddProductRequest; import com.baeldung.dddhexagonalspring.application.request.CreateOrderRequest; @@ -12,12 +12,12 @@ import java.util.UUID; @RestController @RequestMapping("/orders") -public class OrderController { +public class RestOrderController { private final OrderService orderService; @Autowired - public OrderController(OrderService orderService) { + public RestOrderController(OrderService orderService) { this.orderService = orderService; } diff --git a/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/cassandra/CassandraDbOrderRepository.java b/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/cassandra/CassandraDbOrderRepository.java index 98a69cbc88..c14792ce75 100644 --- a/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/cassandra/CassandraDbOrderRepository.java +++ b/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/cassandra/CassandraDbOrderRepository.java @@ -11,7 +11,6 @@ import com.baeldung.dddhexagonalspring.domain.Order; import com.baeldung.dddhexagonalspring.domain.repository.OrderRepository; @Component -@Primary public class CassandraDbOrderRepository implements OrderRepository { private final SpringDataCassandraOrderRepository orderRepository; diff --git a/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/mongo/MongoDbOrderRepository.java b/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/mongo/MongoDbOrderRepository.java index e14cb88f5a..b48b738cbd 100644 --- a/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/mongo/MongoDbOrderRepository.java +++ b/ddd/src/main/java/com/baeldung/dddhexagonalspring/infrastracture/repository/mongo/MongoDbOrderRepository.java @@ -4,12 +4,14 @@ import java.util.Optional; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import com.baeldung.dddhexagonalspring.domain.Order; import com.baeldung.dddhexagonalspring.domain.repository.OrderRepository; @Component +@Primary public class MongoDbOrderRepository implements OrderRepository { private final SpringDataMongoOrderRepository orderRepository;