BAEL-3777: Improved CLI example

This commit is contained in:
Sorin Zamfir 2020-02-01 22:57:12 +02:00
parent eec738467d
commit c0415f7639
2 changed files with 29 additions and 31 deletions

View File

@ -1,24 +1,18 @@
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.WebApplicationType;
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 implements CommandLineRunner {
private static final Logger LOG = LoggerFactory.getLogger(DomainLayerApplication.class);
public static void main(final String[] args) {
SpringApplication application = new SpringApplication(DomainLayerApplication.class);
@ -35,15 +29,9 @@ public class DomainLayerApplication implements CommandLineRunner {
@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");
orderController.createCompleteOrder();
orderController.createIncompleteOrder();
// uncomment to stop the context when execution is done
// context.close();
}
}

View File

@ -1,7 +1,10 @@
package com.baeldung.dddhexagonalspring.application.cli;
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.stereotype.Component;
@ -9,7 +12,9 @@ import com.baeldung.dddhexagonalspring.domain.Product;
import com.baeldung.dddhexagonalspring.domain.service.OrderService;
@Component
public class CliOrderController{
public class CliOrderController {
private static final Logger LOG = LoggerFactory.getLogger(CliOrderController.class);
private final OrderService orderService;
@ -18,20 +23,25 @@ public class CliOrderController{
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) {
public void createCompleteOrder() {
LOG.info("<<Create complete order>>");
UUID orderId = createOrder();
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;
}
}