Skip to content

1. CRUD

Account Microservice

The Account microservice is responsible for managing user accounts, basically, almost every application has a user account system. This microservice provides the necessary endpoints to create, read, update, and delete accounts. The microservice is built using Spring Boot and follows the Domain-Driven Design (DDD) approach.

The microservice is divided into two main modules: account and account-service:

  • the account module contains the API definition and the data transfer objects (DTOs) for the Account microservice;
  • the account-service module contains the service implementation, repository, and entity classes.
classDiagram
    namespace account {
        class AccountController {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class AccountIn {
            -String name
            -String email
            -String password
        }
        class AccountOut {
            -String id
            -String name
            -String email
        }
    }
    namespace account-service {
        class AccountResource {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class AccountService {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class AccountRepository {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class Account {
            -String id
            -String name
            -String email
            -String password
            -String sha256
        }
        class AccountModel {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
    }
    <<Interface>> AccountController
    AccountController ..> AccountIn
    AccountController ..> AccountOut

    <<Interface>> AccountRepository
    AccountController <|-- AccountResource
    AccountResource *-- AccountService
    AccountService *-- AccountRepository
    AccountService ..> Account
    AccountService ..> AccountModel
    AccountRepository ..> AccountModel

This approach allows the separation of concerns and the organization of the codebase into different modules, making it easier to maintain and scale the application. Also, it creates a facility to reuse the microservice by other microservices in the future - builts in Java.

The construction of the Account microservice follows the Clean Architecture approach, which promotes the total decoupling of business rules from interface layers. The diagram below illustrates the flow of data among the layers of the Account microservice:

sequenceDiagram
    title Clean architecture's approach 
    Actor Request
    Request ->>+ Controller: 
    Controller ->>+ Service: parser (AccountIn -> Account)
    Service ->>+ Repository: parser (Account -> AccountModel)
    Repository ->>+ Database: 
    Database ->>- Repository: 
    Repository ->>- Service: parser (Account <- AccountModel)
    Service ->>- Controller: parser (AccountOut <- Account)
    Controller ->>- Request: 

Previously to build the Account microservice, it is necessary to prepare the environment by installing the database to persist the data. For that, we will use a Docker Compose file to create a PostgreSQL container, as well as, a cluster to isolate the microservices from external access, creating a secure environment - trusted layer. A Docker Compose file is a YAML file that defines how Docker containers should behave in production. The file contains the configuration for the database, the microservices, and the network configuration.

flowchart LR
    subgraph api [Trusted Layer]
        direction TB
        account e3@==> db@{ shape: cyl, label: "Database" }
    end
    internet e1@==>|request| account:::red
    e1@{ animate: true }
    e3@{ animate: true }
    classDef red fill:#fcc

Docker Compose

📁 api/
├── 📁 account/
├── 📁 account-service/
├── 📄 .env
└── 📄 compose.yaml
<!-- --8<-- "https://raw.githubusercontent.com/Insper/platform/refs/heads/main/api/compose.yaml" -->
<!-- --8<-- "https://raw.githubusercontent.com/Insper/platform/refs/heads/main/api/.env" -->
docker compose up -d --build
[+] Running 2/2
✔ Network store_default Created 0.1s
✔ Container store-db-1 Started 0.2s

Account

📁 api/
└── 📁 account/
    ├── 📁 src/
       └── 📁 main/
           └── 📁 java/
               └── 📁 store/
                   └── 📁 account/
                       ├── 📄 AccountController.java
                       ├── 📄 AccountIn.java
                       └── 📄 AccountOut.java
    └── 📄 pom.xml
Source
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account/refs/heads/main/pom.xml" -->
AccountController.java
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account/refs/heads/main/src/main/java/store/account/AccountController.java" -->
AccountIn.java
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account/refs/heads/main/src/main/java/store/account/AccountIn.java" -->
AccountOut.java
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account/refs/heads/main/src/main/java/store/account/AccountOut.java" -->

mvn clean install

Account-Service

📁 api/
└── 📁 account-service/
    ├── 📁 src/
       └── 📁 main/
           ├── 📁 java/
              └── 📁 store/
                  └── 📁 account/
                      ├── 📄 Account.java
                      ├── 📄 AccountApplication.java
                      ├── 📄 AccountModel.java
                      ├── 📄 AccountParser.java
                      ├── 📄 AccountRepository.java
                      ├── 📄 AccountResource.java
                      └── 📄 AccountService.java
           └── 📁 resources/
               ├── 📄 application.yaml
               └── 📁 db/
                   └── 📁 migration/
                       ├── 📄 V2025.02.21.001__create_schema_account.sql
                       └── 📄 V2025.02.21.002__create_table_account.sql
    ├── 📄 pom.xml
    └── 📄 Dockerfile
Source
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/pom.xml" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/resources/application.yaml" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/java/store/account/Account.java" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/java/store/account/AccountApplication.java" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/java/store/account/AccountModel.java" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/java/store/account/AccountParser.java" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/java/store/account/AccountRepository.java" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/java/store/account/AccountResource.java" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/java/store/account/AccountService.java" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/resources/db/migration/V2025.02.21.001__create_schema_account.sql" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/src/main/resources/db/migration/V2025.02.21.002__create_table_account.sql" -->
<!-- --8<-- "https://raw.githubusercontent.com/hsandmann/insper.store.account-service/refs/heads/main/Dockerfile" -->
mvn clean package spring-boot:run