diff --git a/hexagonal-architecture-poc/pom.xml b/hexagonal-architecture-poc/pom.xml new file mode 100644 index 0000000000..b31a6dec90 --- /dev/null +++ b/hexagonal-architecture-poc/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.5.RELEASE + + + com.baeldung + hexagonal-architecture-poc + 0.0.1-SNAPSHOT + hexagonal-architecture-poc + Demo project for Hexagonal Architecture POC + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/HexagonalArchitecturePocApplication.java b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/HexagonalArchitecturePocApplication.java new file mode 100644 index 0000000000..1690fbbeb5 --- /dev/null +++ b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/HexagonalArchitecturePocApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.hexagonal; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HexagonalArchitecturePocApplication { + + public static void main(String[] args) { + SpringApplication.run(HexagonalArchitecturePocApplication.class, args); + } + +} diff --git a/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/boundary/input/OrderService.java b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/boundary/input/OrderService.java new file mode 100644 index 0000000000..07793f75ab --- /dev/null +++ b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/boundary/input/OrderService.java @@ -0,0 +1,11 @@ +package com.baeldung.hexagonal.boundary.input; + +import org.springframework.stereotype.Service; + +import com.baeldung.hexagonal.core.entities.Order; + +@Service public interface OrderService { + Iterable getOrders(); + Order createOrder(Double total); + boolean registerOrder(Order order); +} diff --git a/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/boundary/output/OrderRepository.java b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/boundary/output/OrderRepository.java new file mode 100644 index 0000000000..ea1d38d296 --- /dev/null +++ b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/boundary/output/OrderRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.hexagonal.boundary.output; + +import java.util.List; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.hexagonal.core.entities.Order; + +@Repository public interface OrderRepository extends CrudRepository{ +} diff --git a/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/core/entities/Order.java b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/core/entities/Order.java new file mode 100644 index 0000000000..db0c4efd54 --- /dev/null +++ b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/core/entities/Order.java @@ -0,0 +1,27 @@ +package com.baeldung.hexagonal.core.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity @Table(name="Orders") public class Order { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; + Double total; + + public Order() { + super(); + } + public Double getTotal() { + return total; + } + public void setTotal(Double total) { + this.total = total; + } + public Long getId() { + return id; + } + + +} diff --git a/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/core/services/OrderServiceImpl.java b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/core/services/OrderServiceImpl.java new file mode 100644 index 0000000000..d3d8f1ba6a --- /dev/null +++ b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/core/services/OrderServiceImpl.java @@ -0,0 +1,35 @@ +package com.baeldung.hexagonal.core.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.hexagonal.boundary.input.OrderService; +import com.baeldung.hexagonal.boundary.output.OrderRepository; +import com.baeldung.hexagonal.core.entities.Order; + +@Service public class OrderServiceImpl implements OrderService { + + @Autowired OrderRepository orderRepository; + + @Override + public Iterable getOrders() { + return orderRepository.findAll(); + } + + @Override + public boolean registerOrder(Order order) { + if (order.getTotal() > 0) + orderRepository.save(order); + else + return false; + + return true; + } + + @Override + public Order createOrder(Double total) { + Order order = new Order(); + order.setTotal(total); + return order; + } +} diff --git a/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/outside/OrderController.java b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/outside/OrderController.java new file mode 100644 index 0000000000..85c2e42368 --- /dev/null +++ b/hexagonal-architecture-poc/src/main/java/com/baeldung/hexagonal/outside/OrderController.java @@ -0,0 +1,32 @@ +package com.baeldung.hexagonal.outside; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.hexagonal.boundary.input.OrderService; +import com.baeldung.hexagonal.core.entities.Order; + +@RestController public class OrderController { + + @Autowired OrderService orderService; + + @GetMapping(path = "/orders/list") + public Iterable getOrders(){ + return orderService.getOrders(); + } + + @GetMapping(path = "/orders/add") + public String placeOrder(@RequestParam Double total) { + boolean isPlaced = orderService.registerOrder(orderService.createOrder(total)); + return isPlaced ? "Ok" : "Nok"; + } + + @GetMapping(path = "/orders/add-commission") + public String placeCommissionOrder(@RequestParam Double total) { + boolean isPlaced = orderService.registerOrder(orderService.createOrder(total*1.05)); + return isPlaced ? "Ok" : "Nok"; + } + +} diff --git a/hexagonal-architecture-poc/src/main/resources/application.properties b/hexagonal-architecture-poc/src/main/resources/application.properties new file mode 100644 index 0000000000..bc2fdde8c1 --- /dev/null +++ b/hexagonal-architecture-poc/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect