BAEL-3777: Improved CLI example
This commit is contained in:
parent
eec738467d
commit
c0415f7639
|
@ -1,24 +1,18 @@
|
||||||
package com.baeldung.dddhexagonalspring;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.WebApplicationType;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
import com.baeldung.dddhexagonalspring.application.cli.CliOrderController;
|
import com.baeldung.dddhexagonalspring.application.cli.CliOrderController;
|
||||||
import com.baeldung.dddhexagonalspring.domain.Product;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@PropertySource(value = { "classpath:ddd-layers.properties" })
|
@PropertySource(value = { "classpath:ddd-layers.properties" })
|
||||||
public class DomainLayerApplication implements CommandLineRunner {
|
public class DomainLayerApplication implements CommandLineRunner {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(DomainLayerApplication.class);
|
|
||||||
|
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
SpringApplication application = new SpringApplication(DomainLayerApplication.class);
|
SpringApplication application = new SpringApplication(DomainLayerApplication.class);
|
||||||
|
@ -35,15 +29,9 @@ public class DomainLayerApplication implements CommandLineRunner {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(String... args) throws Exception {
|
public void run(String... args) throws Exception {
|
||||||
LOG.info("Placing a new CLI order with two products");
|
orderController.createCompleteOrder();
|
||||||
Product mobilePhone = new Product(UUID.randomUUID(), BigDecimal.valueOf(200), "mobile");
|
orderController.createIncompleteOrder();
|
||||||
Product razor = new Product(UUID.randomUUID(), BigDecimal.valueOf(50), "razor");
|
// uncomment to stop the context when execution is done
|
||||||
LOG.info("Creating order with mobile phone");
|
// context.close();
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package com.baeldung.dddhexagonalspring.application.cli;
|
package com.baeldung.dddhexagonalspring.application.cli;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ -9,7 +12,9 @@ import com.baeldung.dddhexagonalspring.domain.Product;
|
||||||
import com.baeldung.dddhexagonalspring.domain.service.OrderService;
|
import com.baeldung.dddhexagonalspring.domain.service.OrderService;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class CliOrderController{
|
public class CliOrderController {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(CliOrderController.class);
|
||||||
|
|
||||||
private final OrderService orderService;
|
private final OrderService orderService;
|
||||||
|
|
||||||
|
@ -18,20 +23,25 @@ public class CliOrderController{
|
||||||
this.orderService = orderService;
|
this.orderService = orderService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UUID createOrder(Product product) {
|
public void createCompleteOrder() {
|
||||||
return orderService.createOrder(product);
|
LOG.info("<<Create complete order>>");
|
||||||
}
|
UUID orderId = createOrder();
|
||||||
|
|
||||||
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);
|
orderService.completeOrder(orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createIncompleteOrder() {
|
||||||
|
LOG.info("<<Create incomplete order>>");
|
||||||
|
UUID orderId = createOrder();
|
||||||
|
}
|
||||||
|
|
||||||
|
private UUID createOrder() {
|
||||||
|
LOG.info("Placing a new 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 = orderService.createOrder(mobilePhone);
|
||||||
|
LOG.info("Adding a razor to the order");
|
||||||
|
orderService.addProduct(orderId, razor);
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue