Skip to content

Commit

Permalink
remove folder from usecases and entities
Browse files Browse the repository at this point in the history
  • Loading branch information
gianluca-sabena committed Dec 1, 2023
1 parent 7986662 commit 4b53fb5
Show file tree
Hide file tree
Showing 29 changed files with 242 additions and 251 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,28 @@ An opinionated spring template based on a "clean architecture". (See credits)

## Configure
In order to switch adapter from memory to jdbc
- 1) edit [settings.gradle](./settings.gradle) and comment `include 'adapters:memory'` and comment out `include 'adapters:jdbc'`
- 2) In frameworks folder: edit batch and/or servlet
- 1) edit [settings.gradle](./settings.gradle) and comment `include 'adapters:memory'` and uncomment `include 'adapters:jdbc'`
- 2) In folder: frameworks / servlet edit file [build.gradle](./frameworks/servlet/build.gradle) and comment `implementation project(':adapters:memory')` and uncomment `implementation project(':adapters:jdbc')`
- 3) Repeat step 2 for all other frameworks...

## Gradle
## Run

- Run `./gradlew bootRun`
- Build (include tests) `./gradlew build`
- Test
- Framework servlet see [frameworks/servlet/README.md](./frameworks/servlet/README.md)

-


## Source

- Generate spring servlet from spring boot initializr
- Add example from official guide <https://github.com/spring-guides/tut-rest>
- Add code from <https://github.com/microservices-demo/orders>

## TODO
- Experiments with application properties
- JDBC with postgresql and oracle with

## Credits

Expand All @@ -53,5 +63,10 @@ This project was inspired by:
- Blog <https://medium.com/@viniciusromualdobusiness/clean-architecture-with-spring-boot-a-good-idea-d6f97e450130>
- Red Hat <https://developers.redhat.com/articles/2023/08/08/implementing-clean-architecture-solutions-practical-example>
- Example with ArchUnit <https://reflectoring.io/java-components-clean-boundaries/>

## Resources
- Baeldung - Rest with pagination <https://www.baeldung.com/rest-api-pagination-in-spring>
- Spring multi module <https://spring.io/guides/gs/multi-module/>
- Blog <https://www.arhohuttunen.com/hexagonal-architecture-spring-boot/>
- Blog <https://betterprogramming.pub/hexagonal-architecture-with-spring-boot-74e93030eba3>
- Blog <https://betterprogramming.pub/hexagonal-architecture-with-spring-boot-74e93030eba3>

4 changes: 2 additions & 2 deletions adapters/jdbc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ dependencyManagement {
}

dependencies {
implementation project(':entities:user')
implementation project(':usecases:user')
implementation project(':entities')
implementation project(':usecases')
//implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.clean.adapter;

import com.example.clean.entities.user.User;
import com.example.clean.entities.User;
import com.example.clean.usecase.port.UserRepository;

import java.util.HashMap;
Expand All @@ -12,18 +12,17 @@
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
@Repository
public class DbUserRepository implements UserRepository {
Logger logger = LoggerFactory.getLogger(DbUserRepository.class);

// @Autowired
private JdbcTemplate jdbcTemplate;

private final Map<String, User> inMemoryDb = new HashMap<>();

@Autowired
public DbUserRepository(JdbcTemplate jdbcTemplate) {
logger.info("-------> DbUserRepository JDBC - {}", jdbcTemplate.toString());
this.jdbcTemplate = jdbcTemplate;
Expand All @@ -39,7 +38,7 @@ public Optional<User> findByEmail(final String email) {
@Override
public User create(final User user) {
logger.info("JDBC adapter - Create user: {}", user.getId());
jdbcTemplate.update("INSERT INTO person(id, email, password, lastName, firstName) VALUES (?,?,?,?,?)", user.getId(), user.getEmail(), user.getPassword(), user.getLastName(), user.getFirstName());
jdbcTemplate.update("INSERT INTO person(id, email, lastName, firstName) VALUES (?,?,?,?)", user.getId(), user.getEmail(), user.getLastName(), user.getFirstName());
//inMemoryDb.put(user.getId(), user);
return user;
}
Expand All @@ -52,9 +51,9 @@ public Optional<User> findById(final String id) {
@Override
public List<User> findAllUsers() {
return jdbcTemplate.query(
"SELECT id, email, password, lastName, firstName FROM person",
"SELECT id, email, lastName, firstName FROM person",
(rs, rowNum) -> User.builder().id(rs.getString("id")).email(rs.getString("email"))
.password(rs.getString("password")).lastName(rs.getString("lastName"))
.lastName(rs.getString("lastName"))
.firstName(rs.getString("firstName")).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ databaseChangeLog:
- column:
name: email
type: varchar(50)
- column:
name: password
type: varchar(50)
- column:
name: lastName
type: varchar(50)
Expand Down
4 changes: 2 additions & 2 deletions adapters/memory/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencyManagement {
}

dependencies {
implementation project(':entities:user')
implementation project(':usecases:user')
implementation project(':entities')
implementation project(':usecases')
implementation 'org.springframework.boot:spring-boot-starter'
}
File renamed without changes.
4 changes: 4 additions & 0 deletions entities/src/main/java/com/example/clean/entities/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.clean.entities;
// https://github.com/microservices-demo/orders
// package com.example.clean.entities.user;

86 changes: 86 additions & 0 deletions entities/src/main/java/com/example/clean/entities/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.example.clean.entities;

public class User {

private String id;
private String email;
private String password;
private String lastName;
private String firstName;

// required for mvc servlet json deserialization
User() {

}

private User(final String id, final String email, final String lastName, final String firstName) {
this.id = id;
this.email = email;
this.lastName = lastName;
this.firstName = firstName;
}

public static UserBuilder builder() {
return new UserBuilder();
}

public static class UserBuilder {
private String id;
private String email;
private String lastName;
private String firstName;

UserBuilder() {
}

public UserBuilder id(final String id) {
this.id = id;
return this;
}

public UserBuilder email(final String email) {
this.email = email;
return this;
}

public UserBuilder lastName(final String lastName) {
this.lastName = lastName;
return this;
}

public UserBuilder firstName(final String firstName) {
this.firstName = firstName;
return this;
}

public User build() {
return new User(id, email, lastName, firstName);
}
}

public String getId() {
return id;
}

public String getEmail() {
return email;
}

public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", email='" + email + '\'' +
", lastName='" + lastName + '\'' +
", firstName='" + firstName + '\'' +
'}';
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.stereotype.Component;

import com.example.clean.adapter.DbUserRepository;
//import com.example.clean.adapter.DbUserRepositoryJdbc;
import com.example.clean.usecase.FindUser;
import com.example.clean.usecase.port.UserRepository;
import com.example.clean.entities.user.User;
Expand Down
4 changes: 3 additions & 1 deletion frameworks/servlet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

[Generated from spring initializr](https://start.spring.io/#!type=gradle-project&language=java&platformVersion=3.1.5&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=rest-service&name=rest-service&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.rest-service&dependencies=web,devtools,security,actuator,jdbc,postgresql,modulith,batch,liquibase,prometheus)



## Test

Create a user
```curl -X POST http://127.0.0.1:8080/users --header "Content-Type: application/json" --data '{"email":"[email protected]","password":"xyz", "lastName": "Doe", "firstName": "John"}'```
```curl -X POST http://127.0.0.1:8080/users --header "Content-Type: application/json" --data '{"email":"[email protected]","password":"xyz", "lastName": "Me", "firstName": "You"}'```

List Users
````curl -X GET http://127.0.0.1:8080/users `
4 changes: 2 additions & 2 deletions frameworks/servlet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ repositories {
}

dependencies {
implementation project(':usecases:user')
implementation project(':entities:user')
implementation project(':usecases')
implementation project(':entities')
//implementation project(':adapters:memory')
implementation project(':adapters:jdbc')
implementation 'org.springframework.boot:spring-boot-starter-actuator'
Expand Down
Loading

0 comments on commit 4b53fb5

Please sign in to comment.