Подключил PostgreSQL и Flyway, добавил JDBC-хранилище счетов и интерфейс AccountStore
This commit is contained in:
1 parent
03004276af
commit
c22f1f668f
12 files changed
+163
-46
No files matched your search
@@ -0,0 +1,19 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17
|
||||
environment:
|
||||
POSTGRES_DB: easy_money
|
||||
POSTGRES_USER: easy_money
|
||||
POSTGRES_PASSWORD: easy_money_dev
|
||||
ports:
|
||||
- "127.0.0.1:5432:5432"
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U easy_money -d easy_money"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
@@ -38,6 +38,27 @@
|
||||
<artifactId>spring-boot-starter-webmvc-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-flyway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-database-postgresql</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -20,9 +20,7 @@ public class Account {
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new InvalidAccountNameException("Не указано имя счёта");
|
||||
}
|
||||
validateName(name);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -37,4 +35,10 @@ public class Account {
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
}
|
||||
|
||||
public static void validateName(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new InvalidAccountNameException("Не указано имя счёта");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AccountJournal {
|
||||
private final Map<Long, Account> accounts = new HashMap<>();
|
||||
|
||||
public void saveAccount(Account account) {
|
||||
if (account == null) {
|
||||
throw new IllegalArgumentException("Не указан счёт для сохранения в журнал");
|
||||
}
|
||||
accounts.put(account.getId(), account);
|
||||
}
|
||||
|
||||
public Account getAccountById(Long id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("Не указан ID счёта");
|
||||
}
|
||||
return accounts.get(id);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
package net.sytes.kashey.easymoney.config;
|
||||
|
||||
import model.AccountJournal;
|
||||
import model.OperationJournal;
|
||||
import service.AccountService;
|
||||
import service.Ledger;
|
||||
import service.OperationService;
|
||||
import service.*;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -31,12 +28,12 @@ public class AccountingConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AccountJournal accountJournal() {
|
||||
return new AccountJournal();
|
||||
public AccountStore accountStore() {
|
||||
return new InMemoryAccountStore();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AccountService accountService(AccountJournal accountJournal) {
|
||||
return new AccountService(accountJournal);
|
||||
public AccountService accountService(AccountStore accountStore) {
|
||||
return new AccountService(accountStore);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package net.sytes.kashey.easymoney.persistence;
|
||||
|
||||
import model.Account;
|
||||
import org.springframework.jdbc.core.simple.JdbcClient;
|
||||
import service.AccountStore;
|
||||
|
||||
public class AccountRepository implements AccountStore {
|
||||
private final JdbcClient jdbcClient;
|
||||
|
||||
public AccountRepository(JdbcClient jdbcClient) {
|
||||
this.jdbcClient = jdbcClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account create(String name) {
|
||||
|
||||
return jdbcClient.sql("""
|
||||
INSERT INTO accounts (name)
|
||||
VALUES (:name)
|
||||
RETURNING id, name
|
||||
""")
|
||||
.param("name", name)
|
||||
.query((rs, rowNum) -> new Account(
|
||||
rs.getLong("id"),
|
||||
rs.getString("name")
|
||||
))
|
||||
.single();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account findById(Long id) {
|
||||
return jdbcClient.sql("""
|
||||
SELECT id, name
|
||||
FROM accounts
|
||||
WHERE id = :id
|
||||
""")
|
||||
.param("id", id)
|
||||
.query((rs, rowNum) -> new Account(
|
||||
rs.getLong("id"),
|
||||
rs.getString("name")
|
||||
))
|
||||
.optional()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,31 @@
|
||||
package service;
|
||||
|
||||
import model.Account;
|
||||
import model.AccountJournal;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class AccountService {
|
||||
|
||||
private Long counterId = 0L;
|
||||
private final AccountJournal accountJournal;
|
||||
private final AccountStore accountStore;
|
||||
|
||||
public AccountService(AccountJournal accountJournal) {
|
||||
this.accountJournal = accountJournal;
|
||||
public AccountService(AccountStore accountStore) {
|
||||
this.accountStore = Objects.requireNonNull(
|
||||
accountStore, "Не указано хранилище счетов"
|
||||
);
|
||||
}
|
||||
|
||||
public synchronized Account create(String name) {
|
||||
Account newAccount = new Account(counterId++, name);
|
||||
accountJournal.saveAccount(newAccount);
|
||||
return newAccount;
|
||||
public Account create(String name) {
|
||||
Account.validateName(name);
|
||||
return accountStore.create(name);
|
||||
}
|
||||
|
||||
public synchronized Account getById(Long id) {
|
||||
Account account = accountJournal.getAccountById(id);
|
||||
public Account getById(Long id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("Не указан ID счёта");
|
||||
}
|
||||
|
||||
Account account = accountStore.findById(id);
|
||||
|
||||
if (account == null) {
|
||||
throw new AccountNotFoundException(id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package service;
|
||||
|
||||
import model.Account;
|
||||
|
||||
public interface AccountStore {
|
||||
|
||||
Account create(String name);
|
||||
|
||||
Account findById(Long id);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package service;
|
||||
|
||||
import model.Account;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class InMemoryAccountStore implements AccountStore {
|
||||
|
||||
private final Map<Long, Account> accounts = new HashMap<>();
|
||||
private long nextId = 0L;
|
||||
|
||||
@Override
|
||||
public synchronized Account create(String name) {
|
||||
Account account = new Account(nextId, name);
|
||||
accounts.put(account.getId(), account);
|
||||
nextId++;
|
||||
return account;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Account findById(Long id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("Не указан ID счёта");
|
||||
}
|
||||
|
||||
return accounts.get(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/easy_money
|
||||
spring.datasource.username=easy_money
|
||||
spring.datasource.password=easy_money_dev
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE accounts (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
CONSTRAINT accounts_name_not_blank
|
||||
CHECK (name !~ '^[[:space:]]*$')
|
||||
);
|
||||
@@ -1,7 +1,6 @@
|
||||
package service;
|
||||
|
||||
import model.Account;
|
||||
import model.AccountJournal;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -19,7 +18,7 @@ class AccountServiceTest {
|
||||
@Test
|
||||
void concurrentAccountCreationAssignsUniqueIdsAndStoresAllAccounts() throws Exception {
|
||||
// Given
|
||||
AccountService service = new AccountService(new AccountJournal());
|
||||
AccountService service = new AccountService(new InMemoryAccountStore());
|
||||
int taskCount = 100;
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
List<Account> accounts = new ArrayList<>();
|
||||
|
||||
Reference in new issue
Block a user