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<>();