Initial Commit - Ports and Adapters

This commit is contained in:
Devansh
2021-06-07 04:25:00 +05:30
parent e9cf19c324
commit 55152deaaf
10 changed files with 195 additions and 0 deletions

View File

@@ -73,6 +73,7 @@
<module>spring-boot-actuator</module>
<module>spring-boot-data-2</module>
<module>spring-boot-react</module>
<module>hex-architecture</module>
</modules>
<dependencyManagement>

View File

@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>hex-architecture</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hex-architecture</name>
<description>Product store for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package com.baeldung.hexarchitecture.productstore;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HexagonalApplication {
public static void main(String[] args) {
SpringApplication.run(HexagonalApplication.class, args);
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.hexarchitecture.productstore.adapters.persistence;
import com.baeldung.hexarchitecture.productstore.domain.model.Product;
import com.baeldung.hexarchitecture.productstore.ports.ProductDatabasePort;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Service
public class ProductServiceAdapter implements ProductDatabasePort {
private Map<String, Product> productDataBase = new HashMap<>();
@Override
public void addProduct(Product product) {
String id = UUID.randomUUID().toString();
product.setId(id);
productDataBase.put(id, product);
}
@Override
public Product getProduct(String id) {
return productDataBase.get(id);
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.hexarchitecture.productstore.adapters.rest.controller;
import com.baeldung.hexarchitecture.productstore.adapters.persistence.ProductServiceAdapter;
import com.baeldung.hexarchitecture.productstore.domain.model.Product;
import com.baeldung.hexarchitecture.productstore.ports.ProductUserInterfacePort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/product/")
public class ProductControllerAdapter implements ProductUserInterfacePort {
@Autowired
private ProductServiceAdapter productServiceAdapter;
@Override
public void addProduct(@RequestBody Product product) {
productServiceAdapter.addProduct(product);
}
@Override
public Product getProduct(@PathVariable String id) {
return productServiceAdapter.getProduct(id);
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.hexarchitecture.productstore.domain.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;
@AllArgsConstructor
@NoArgsConstructor
@Setter
public class Product {
String id;
String name;
String description;
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.hexarchitecture.productstore.ports;
import com.baeldung.hexarchitecture.productstore.domain.model.Product;
public interface ProductDatabasePort {
void addProduct(Product product);
Product getProduct(String id);
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.hexarchitecture.productstore.ports;
import com.baeldung.hexarchitecture.productstore.domain.model.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
public interface ProductUserInterfacePort {
@PostMapping
void addProduct(@RequestBody Product product);
@GetMapping("{id}")
Product getProduct(@PathVariable String id);
}

View File

@@ -0,0 +1 @@
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration