Подключил PostgreSQL и Flyway, добавил JDBC-хранилище счетов и интерфейс AccountStore

This commit is contained in:
Kashey committed 2026-10-28 19:31:27 +03:00
1 parent 03004276af
commit c22f1f668f
12 files changed
+163 -46

No files matched your search

+19
View File
@@ -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:
+21
View File
@@ -38,6 +38,27 @@
<artifactId>spring-boot-starter-webmvc-test</artifactId> <artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </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> </dependencies>
<build> <build>
+7 -3
View File
@@ -20,9 +20,7 @@ public class Account {
} }
public void setName(String name) { public void setName(String name) {
if (name == null || name.isBlank()) { validateName(name);
throw new InvalidAccountNameException("Не указано имя счёта");
}
this.name = name; this.name = name;
} }
@@ -37,4 +35,10 @@ public class Account {
public int hashCode() { public int hashCode() {
return id.hashCode(); return id.hashCode();
} }
public static void validateName(String name) {
if (name == null || name.isBlank()) {
throw new InvalidAccountNameException("Не указано имя счёта");
}
}
} }
-22
View File
@@ -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; package net.sytes.kashey.easymoney.config;
import model.AccountJournal;
import model.OperationJournal; import model.OperationJournal;
import service.AccountService; import service.*;
import service.Ledger;
import service.OperationService;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -31,12 +28,12 @@ public class AccountingConfig {
} }
@Bean @Bean
public AccountJournal accountJournal() { public AccountStore accountStore() {
return new AccountJournal(); return new InMemoryAccountStore();
} }
@Bean @Bean
public AccountService accountService(AccountJournal accountJournal) { public AccountService accountService(AccountStore accountStore) {
return new AccountService(accountJournal); 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);
}
}
+17 -11
View File
@@ -1,25 +1,31 @@
package service; package service;
import model.Account; import model.Account;
import model.AccountJournal;
import java.util.Objects;
public class AccountService { public class AccountService {
private Long counterId = 0L; private final AccountStore accountStore;
private final AccountJournal accountJournal;
public AccountService(AccountJournal accountJournal) { public AccountService(AccountStore accountStore) {
this.accountJournal = accountJournal; this.accountStore = Objects.requireNonNull(
accountStore, "Не указано хранилище счетов"
);
} }
public synchronized Account create(String name) { public Account create(String name) {
Account newAccount = new Account(counterId++, name); Account.validateName(name);
accountJournal.saveAccount(newAccount); return accountStore.create(name);
return newAccount;
} }
public synchronized Account getById(Long id) { public Account getById(Long id) {
Account account = accountJournal.getAccountById(id); if (id == null) {
throw new IllegalArgumentException("Не указан ID счёта");
}
Account account = accountStore.findById(id);
if (account == null) { if (account == null) {
throw new AccountNotFoundException(id); throw new AccountNotFoundException(id);
} }
+10
View File
@@ -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; package service;
import model.Account; import model.Account;
import model.AccountJournal;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
@@ -19,7 +18,7 @@ class AccountServiceTest {
@Test @Test
void concurrentAccountCreationAssignsUniqueIdsAndStoresAllAccounts() throws Exception { void concurrentAccountCreationAssignsUniqueIdsAndStoresAllAccounts() throws Exception {
// Given // Given
AccountService service = new AccountService(new AccountJournal()); AccountService service = new AccountService(new InMemoryAccountStore());
int taskCount = 100; int taskCount = 100;
CountDownLatch start = new CountDownLatch(1); CountDownLatch start = new CountDownLatch(1);
List<Account> accounts = new ArrayList<>(); List<Account> accounts = new ArrayList<>();