Account

1.

Primeiro passo Γ© criar o projeto no Spring Boot, para isso Γ© aconselhΓ‘vel utilizar o gerador de projetos que pode ser acessado atravΓ©s do link Spring Initializr.

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 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
        }
        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
Account
πŸ“ account
β”œβ”€β”€ πŸ“ src
β”‚   └── πŸ“ main
β”‚       └── πŸ“ java
β”‚           └── πŸ“ store
β”‚               └── πŸ“ account
β”‚                   β”œβ”€β”€ πŸ“„ AccountController.java
β”‚                   β”œβ”€β”€ πŸ“„ AccountIn.java
β”‚                   └── πŸ“„ AccountOut.java
└── πŸ“„ pom.xml
pom.xml
<?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>3.3.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>store</groupId>
    <artifactId>account</artifactId>
    <version>0.0.1</version>
    <name>account</name>
    <properties>
        <java.version>21</java.version>
        <spring-cloud.version>2023.0.2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
AccountController.java
package store.account;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

public interface AccountController {

    @GetMapping("/account")
    public List<AccountOut> findAll();

    @PostMapping("/account")
    public void create(@RequestBody AccountIn in);

}
AccountIn.java
package store.account;

import lombok.Builder;
import lombok.experimental.Accessors;

@Builder @Accessors(fluent = true)
public record AccountIn (
    String name,
    String email,
    String password
) {

}
AccountOut.java
package store.account;

import lombok.Builder;
import lombok.experimental.Accessors;

@Builder @Accessors(fluent = true)
public record AccountOut (
    String id,
    String name,
    String email
) {

}

mvn clean install
Service
πŸ“ account-service
β”œβ”€β”€ πŸ“ src
β”‚   └── πŸ“ main
β”‚       β”œβ”€β”€ πŸ“ java
β”‚       β”‚   └── πŸ“ store
β”‚       β”‚       └── πŸ“ account
β”‚       β”‚           β”œβ”€β”€ πŸ“„ AccountApplication.java
β”‚       β”‚           β”œβ”€β”€ πŸ“„ AccountResource.java
β”‚       β”‚           β”œβ”€β”€ πŸ“„ AccountService.java
β”‚       β”‚           β”œβ”€β”€ πŸ“„ AccountRepository.java
β”‚       β”‚           β”œβ”€β”€ πŸ“„ Account.java
β”‚       β”‚           β”œβ”€β”€ πŸ“„ AccountModel.java
β”‚       β”‚           └── πŸ“„ AccountParser.java
β”‚       └── πŸ“ resources
β”‚           └── πŸ“„ application.yaml
β”œβ”€β”€ πŸ“„ pom.xml
└── πŸ“„ Dockerfile
pom.xml
<?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>3.3.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>store</groupId>
    <artifactId>account-service</artifactId>
    <version>0.0.1</version>
    <name>account-service</name>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>store</groupId>
            <artifactId>account</artifactId>
            <version>${project.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
application.yaml
server:
  port: 8081

spring:
  application:
    name: account-service

  datasource:
    url: jdbc:postgresql://localhost:5432/store
    username: store
    password: store
    driver-class-name: org.postgresql.Driver
AccountApplication.java
package store.account;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AccountApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccountApplication.class, args);
    }

}
AccountResource.java
package store.account;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AccountResource implements AccountController {

    @Autowired
    private AccountService accountService;

    @Override
    public List<AccountOut> findAll() {
        return accountService.findAll().stream().map(AccountParser::to).toList();
    }

    @Override
    public void create(@RequestBody AccountIn in) {
        accountService.create(AccountParser.to(in));
    }

}

mvn clean package spring-boot:run