Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions hexagonal-simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The Simple way to work with Hexagonal Architecture


We divided this application into six maven projects: Domain, Rest, Port, Principal, Persistence, and Hexagonal-Example. Its objective is explicit about that architecture. There are other approaches. The division into six projects is to explain the separation of the responsibilities.

The API can be called by http://localhost:8080/timesheet
In this resource, there is a call with a post method.
19 changes: 19 additions & 0 deletions hexagonal-simple/domain/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>hexagonal-example</artifactId>
<groupId>com.baeldung.hexagonal</groupId>
<version>1.0.0</version>
</parent>
<artifactId>domain</artifactId>
<dependencies>
<dependency>
<groupId>com.baeldung.hexagonal</groupId>
<artifactId>ports</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.hexagonal.services;

import java.time.LocalDateTime;

import com.baeldung.hexagonal.dtos.TimeSheetDto;
import com.baeldung.hexagonal.exceptions.TimeSheetConflictException;
import com.baeldung.hexagonal.ports.TimeSheetPersistencePort;
import com.baeldung.hexagonal.ports.TimeSheetServicePort;
import com.spencerwi.either.Either;

public class TimeSheetService implements TimeSheetServicePort {


private TimeSheetPersistencePort timeSheetDataBasePort;


public TimeSheetService(TimeSheetPersistencePort timeSheetDataBasePort) {
this.timeSheetDataBasePort = timeSheetDataBasePort;
}


public Either<TimeSheetConflictException, TimeSheetDto> save(TimeSheetDto dto) {
if(hasUser(dto)) {
dto.setRegisteredAt(LocalDateTime.now());
return Either.right(timeSheetDataBasePort.save(dto));
}else {
return Either.left(new TimeSheetConflictException("User not Found"));
}
}

private boolean hasUser(TimeSheetDto dto) {
return timeSheetDataBasePort.hasUser(dto.getUserId());
}




}
40 changes: 40 additions & 0 deletions hexagonal-simple/persistence/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>hexagonal-example</artifactId>
<groupId>com.baeldung.hexagonal</groupId>
<version>1.0.0</version>
</parent>
<artifactId>persistence</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.baeldung.hexagonal</groupId>
<artifactId>ports</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.hexagonal.database.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baeldung.hexagonal.dtos.TimeSheetDto;
import com.baeldung.hexagonal.entities.TimeSheet;
import com.baeldung.hexagonal.ports.TimeSheetPersistencePort;
import com.baeldung.hexagonal.repositories.TimeSheetRepository;
import com.baeldung.hexagonal.repositories.UserRepository;

@Service
public class TimeSheetPersistencePortImpl implements TimeSheetPersistencePort {

@Autowired
TimeSheetRepository tRepository;
@Autowired
UserRepository uRepository;

@Override
public TimeSheetDto save(TimeSheetDto timeSheet) {
TimeSheet entSheet = tRepository.save(TimeSheet.builder().User(uRepository.findById(timeSheet.getUserId()).get())
.registeredAt(timeSheet.getRegisteredAt())
.typeRegitry(timeSheet.getRegisterType()).build());
timeSheet.setId(entSheet.getId());
return timeSheet;

}

@Override
public boolean hasUser(Long id) {
return uRepository.findById(id).isPresent();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.hexagonal.entities;

import java.time.LocalDateTime;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
@Entity
public class TimeSheet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "USER_ID", nullable = false)
private User User;
private String typeRegitry;
private LocalDateTime registeredAt;




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.hexagonal.entities;

import javax.persistence.Entity;
import javax.persistence.Id;

import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class User {

@Id
private Long id;
private String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.hexagonal.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.baeldung.hexagonal.entities.TimeSheet;

@Repository
public interface TimeSheetRepository extends JpaRepository<TimeSheet, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.hexagonal.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.baeldung.hexagonal.entities.User;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
1 change: 1 addition & 0 deletions hexagonal-simple/persistence/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
insert into User (id,name) values (1,'SILVIO GOMES')
28 changes: 28 additions & 0 deletions hexagonal-simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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.4.3</version>
<relativePath/>
</parent>
<groupId>com.baeldung.hexagonal</groupId>
<artifactId>hexagonal-example</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<description>Demo project for hexagonal architecture. This is the principal pom</description>
<properties>
<java.version>8</java.version>
</properties>

<modules>
<module>domain</module>
<module>rest</module>
<module>persistence</module>
<module>principal</module>
<module>ports</module>
</modules>

</project>
23 changes: 23 additions & 0 deletions hexagonal-simple/ports/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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>com.baeldung.hexagonal</groupId>
<artifactId>hexagonal-example</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>ports</artifactId>
<name>ports</name>
<description>Adapters to others modules</description>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.spencerwi</groupId>
<artifactId>Either.java</artifactId>
<version>2.7.0</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.hexagonal.dtos;

import java.time.LocalDateTime;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class TimeSheetDto {

private Long id;
private Long userId;
private String registerType;
private LocalDateTime registeredAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.hexagonal.exceptions;

public class TimeSheetConflictException extends RuntimeException {

private static final long serialVersionUID = 1L;

public TimeSheetConflictException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.hexagonal.ports;

import com.baeldung.hexagonal.dtos.TimeSheetDto;

public interface TimeSheetPersistencePort {

TimeSheetDto save(TimeSheetDto timeSheet);
boolean hasUser(Long id);



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.hexagonal.ports;

import com.baeldung.hexagonal.dtos.TimeSheetDto;
import com.baeldung.hexagonal.exceptions.TimeSheetConflictException;
import com.spencerwi.either.Either;

public interface TimeSheetServicePort{

Either<TimeSheetConflictException, TimeSheetDto> save(TimeSheetDto dto) throws TimeSheetConflictException;
}
Loading