From c22f1f668fb67fc6fe4962e3345d97d41a42e0d5 Mon Sep 17 00:00:00 2001 From: Kashey Date: Wed, 28 Oct 2026 19:31:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B8=D0=BB=20PostgreSQL=20=D0=B8=20Flyway,=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20JDBC-=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8=D1=89=D0=B5=20=D1=81=D1=87=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B8=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=20AccountStore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 19 ++++++++ pom.xml | 21 +++++++++ src/main/java/model/Account.java | 10 +++-- src/main/java/model/AccountJournal.java | 22 --------- .../easymoney/config/AccountingConfig.java | 13 +++--- .../persistence/AccountRepository.java | 45 +++++++++++++++++++ src/main/java/service/AccountService.java | 28 +++++++----- src/main/java/service/AccountStore.java | 10 +++++ .../java/service/InMemoryAccountStore.java | 29 ++++++++++++ src/main/resources/application.properties | 3 ++ .../db/migration/V1__create_accounts.sql | 6 +++ src/test/java/service/AccountServiceTest.java | 3 +- 12 files changed, 163 insertions(+), 46 deletions(-) create mode 100644 compose.yaml delete mode 100644 src/main/java/model/AccountJournal.java create mode 100644 src/main/java/net/sytes/kashey/easymoney/persistence/AccountRepository.java create mode 100644 src/main/java/service/AccountStore.java create mode 100644 src/main/java/service/InMemoryAccountStore.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/db/migration/V1__create_accounts.sql diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..1a49d4d --- /dev/null +++ b/compose.yaml @@ -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: \ No newline at end of file diff --git a/pom.xml b/pom.xml index af2e220..1f9218b 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,27 @@ spring-boot-starter-webmvc-test test + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.postgresql + postgresql + runtime + + + + org.springframework.boot + spring-boot-starter-flyway + + + + org.flywaydb + flyway-database-postgresql + diff --git a/src/main/java/model/Account.java b/src/main/java/model/Account.java index 4825b6e..9a69ec5 100644 --- a/src/main/java/model/Account.java +++ b/src/main/java/model/Account.java @@ -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("Не указано имя счёта"); + } + } } diff --git a/src/main/java/model/AccountJournal.java b/src/main/java/model/AccountJournal.java deleted file mode 100644 index 04fc6d2..0000000 --- a/src/main/java/model/AccountJournal.java +++ /dev/null @@ -1,22 +0,0 @@ -package model; - -import java.util.HashMap; -import java.util.Map; - -public class AccountJournal { - private final Map 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); - } -} diff --git a/src/main/java/net/sytes/kashey/easymoney/config/AccountingConfig.java b/src/main/java/net/sytes/kashey/easymoney/config/AccountingConfig.java index ad8e62a..cda300d 100644 --- a/src/main/java/net/sytes/kashey/easymoney/config/AccountingConfig.java +++ b/src/main/java/net/sytes/kashey/easymoney/config/AccountingConfig.java @@ -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); } } diff --git a/src/main/java/net/sytes/kashey/easymoney/persistence/AccountRepository.java b/src/main/java/net/sytes/kashey/easymoney/persistence/AccountRepository.java new file mode 100644 index 0000000..19ecc0f --- /dev/null +++ b/src/main/java/net/sytes/kashey/easymoney/persistence/AccountRepository.java @@ -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); + } +} diff --git a/src/main/java/service/AccountService.java b/src/main/java/service/AccountService.java index 7d10b05..5fea50f 100644 --- a/src/main/java/service/AccountService.java +++ b/src/main/java/service/AccountService.java @@ -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); } diff --git a/src/main/java/service/AccountStore.java b/src/main/java/service/AccountStore.java new file mode 100644 index 0000000..87c04a4 --- /dev/null +++ b/src/main/java/service/AccountStore.java @@ -0,0 +1,10 @@ +package service; + +import model.Account; + +public interface AccountStore { + + Account create(String name); + + Account findById(Long id); +} \ No newline at end of file diff --git a/src/main/java/service/InMemoryAccountStore.java b/src/main/java/service/InMemoryAccountStore.java new file mode 100644 index 0000000..6b5f66d --- /dev/null +++ b/src/main/java/service/InMemoryAccountStore.java @@ -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 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); + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..38c6d65 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.datasource.url=jdbc:postgresql://localhost:5432/easy_money +spring.datasource.username=easy_money +spring.datasource.password=easy_money_dev \ No newline at end of file diff --git a/src/main/resources/db/migration/V1__create_accounts.sql b/src/main/resources/db/migration/V1__create_accounts.sql new file mode 100644 index 0000000..8ef4f89 --- /dev/null +++ b/src/main/resources/db/migration/V1__create_accounts.sql @@ -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:]]*$') +); \ No newline at end of file diff --git a/src/test/java/service/AccountServiceTest.java b/src/test/java/service/AccountServiceTest.java index f957513..f761673 100644 --- a/src/test/java/service/AccountServiceTest.java +++ b/src/test/java/service/AccountServiceTest.java @@ -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 accounts = new ArrayList<>();