diff options
| author | Kostya <mail@sartin.in> | 2026-10-09 10:18:41 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-09 10:19:12 +0300 |
| commit | 246c1ab5ffce1a1bb3c0534868b29f6e4c862a62 (patch) | |
| tree | 2418952c9844205d7f003b130f39fc1738a952f1 | |
| parent | 95eb4ff500defc4e34d043bbc096593ffcadecea (diff) | |
| download | guess-market-246c1ab5ffce1a1bb3c0534868b29f6e4c862a62.tar.gz guess-market-246c1ab5ffce1a1bb3c0534868b29f6e4c862a62.tar.xz guess-market-246c1ab5ffce1a1bb3c0534868b29f6e4c862a62.zip | |
account: let users deposit cash into their account
Exercise 3 replaces the fixed initial cash of Exercise 2 with accounts
that users top up themselves. Add AccountContext.deposit, which credits
a DEPOSIT entry through LedgerContext so it carries a timestamp and a
running balance like every other ledger movement, and return the
updated account.
The amount crosses the api boundary as a string, like all money values.
Reject anything that is not a positive number with at most two decimal
places instead of rounding it, so a user is never credited a different
amount than they asked for. A deposit can bring a negative account back
above zero, which lifts the block.
4 files changed, 96 insertions, 3 deletions
diff --git a/api/src/main/java/market/guess/api/AccountContext.java b/api/src/main/java/market/guess/api/AccountContext.java index 8d880aa..e43760a 100644 --- a/api/src/main/java/market/guess/api/AccountContext.java +++ b/api/src/main/java/market/guess/api/AccountContext.java | |||
| @@ -7,4 +7,7 @@ public interface AccountContext { | |||
| 7 | Result<List<LedgerDTO>> getAllAccounts(); | 7 | Result<List<LedgerDTO>> getAllAccounts(); |
| 8 | 8 | ||
| 9 | Result<LedgerDTO> getAccount(String owner); | 9 | Result<LedgerDTO> getAccount(String owner); |
| 10 | |||
| 11 | /** Credits a positive amount (decimal string, e.g. "250.00") to the owner's account. */ | ||
| 12 | Result<LedgerDTO> deposit(String owner, String amount); | ||
| 10 | } | 13 | } |
diff --git a/service/src/main/java/market/guess/service/ledger/LocalAccountContext.java b/service/src/main/java/market/guess/service/ledger/LocalAccountContext.java index 84d23b4..489ed33 100644 --- a/service/src/main/java/market/guess/service/ledger/LocalAccountContext.java +++ b/service/src/main/java/market/guess/service/ledger/LocalAccountContext.java | |||
| @@ -1,20 +1,25 @@ | |||
| 1 | package market.guess.service.ledger; | 1 | package market.guess.service.ledger; |
| 2 | 2 | ||
| 3 | import java.math.BigDecimal; | ||
| 3 | import java.util.ArrayList; | 4 | import java.util.ArrayList; |
| 4 | import java.util.List; | 5 | import java.util.List; |
| 5 | import market.guess.api.AccountContext; | 6 | import market.guess.api.AccountContext; |
| 6 | import market.guess.api.Result; | 7 | import market.guess.api.Result; |
| 7 | import market.guess.model.ledger.LedgerDTO; | 8 | import market.guess.model.ledger.LedgerDTO; |
| 8 | import market.guess.model.ledger.LedgerEntryDTO; | 9 | import market.guess.model.ledger.LedgerEntryDTO; |
| 10 | import market.guess.model.ledger.LedgerType; | ||
| 9 | import market.guess.service.catalog.infrastructure.repository.UserRepository; | 11 | import market.guess.service.catalog.infrastructure.repository.UserRepository; |
| 10 | import market.guess.service.domain.User; | 12 | import market.guess.service.domain.User; |
| 13 | import market.guess.service.helpers.BigDecimalOptions; | ||
| 11 | import market.guess.service.helpers.InstantOptions; | 14 | import market.guess.service.helpers.InstantOptions; |
| 12 | 15 | ||
| 13 | public final class LocalAccountContext implements AccountContext { | 16 | public final class LocalAccountContext implements AccountContext { |
| 14 | private final UserRepository users; | 17 | private final UserRepository users; |
| 18 | private final LedgerContext ledger; | ||
| 15 | 19 | ||
| 16 | public LocalAccountContext(UserRepository users) { | 20 | public LocalAccountContext(UserRepository users, LedgerContext ledger) { |
| 17 | this.users = users; | 21 | this.users = users; |
| 22 | this.ledger = ledger; | ||
| 18 | } | 23 | } |
| 19 | 24 | ||
| 20 | @Override | 25 | @Override |
| @@ -32,6 +37,31 @@ public final class LocalAccountContext implements AccountContext { | |||
| 32 | return Result.ok(toDTO(opt.get())); | 37 | return Result.ok(toDTO(opt.get())); |
| 33 | } | 38 | } |
| 34 | 39 | ||
| 40 | @Override | ||
| 41 | public Result<LedgerDTO> deposit(String owner, String amount) { | ||
| 42 | var user = users.get(owner); | ||
| 43 | if (user.isEmpty()) { | ||
| 44 | return Result.error("ACCOUNT_NOT_FOUND", "Account not found: " + owner); | ||
| 45 | } | ||
| 46 | |||
| 47 | BigDecimal value; | ||
| 48 | try { | ||
| 49 | value = new BigDecimal(amount.trim()); | ||
| 50 | } catch (NullPointerException | NumberFormatException e) { | ||
| 51 | return Result.error("INVALID_AMOUNT", "Deposit amount must be a number, got: " + amount); | ||
| 52 | } | ||
| 53 | if (value.signum() <= 0) { | ||
| 54 | return Result.error("INVALID_AMOUNT", "Deposit amount must be greater than 0."); | ||
| 55 | } | ||
| 56 | if (value.stripTrailingZeros().scale() > 2) { | ||
| 57 | return Result.error("INVALID_AMOUNT", "Deposit amount can have at most 2 decimal places."); | ||
| 58 | } | ||
| 59 | |||
| 60 | ledger.credit( | ||
| 61 | user.get().getLedger(), LedgerType.DEPOSIT, BigDecimalOptions.toMoney(value), "Deposit"); | ||
| 62 | return Result.ok(toDTO(user.get())); | ||
| 63 | } | ||
| 64 | |||
| 35 | private LedgerDTO toDTO(User user) { | 65 | private LedgerDTO toDTO(User user) { |
| 36 | var ledger = user.getLedger(); | 66 | var ledger = user.getLedger(); |
| 37 | var entries = new ArrayList<LedgerEntryDTO>(); | 67 | var entries = new ArrayList<LedgerEntryDTO>(); |
diff --git a/service/src/test/java/market/guess/service/DepositTest.java b/service/src/test/java/market/guess/service/DepositTest.java new file mode 100644 index 0000000..048e07d --- /dev/null +++ b/service/src/test/java/market/guess/service/DepositTest.java | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | package market.guess.service; | ||
| 2 | |||
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 4 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| 5 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 6 | |||
| 7 | import java.time.Clock; | ||
| 8 | import market.guess.model.ledger.LedgerType; | ||
| 9 | import market.guess.service.catalog.infrastructure.repository.InMemoryUserRepository; | ||
| 10 | import market.guess.service.domain.User; | ||
| 11 | import market.guess.service.ledger.LedgerContext; | ||
| 12 | import market.guess.service.ledger.LocalAccountContext; | ||
| 13 | import org.junit.jupiter.api.BeforeEach; | ||
| 14 | import org.junit.jupiter.api.Test; | ||
| 15 | |||
| 16 | class DepositTest { | ||
| 17 | private InMemoryUserRepository userRepo; | ||
| 18 | private LocalAccountContext accounts; | ||
| 19 | |||
| 20 | @BeforeEach | ||
| 21 | void setUp() { | ||
| 22 | userRepo = new InMemoryUserRepository(); | ||
| 23 | userRepo.add(new User("alice", 100)); | ||
| 24 | accounts = new LocalAccountContext(userRepo, new LedgerContext(userRepo, Clock.systemUTC())); | ||
| 25 | } | ||
| 26 | |||
| 27 | @Test | ||
| 28 | void depositCreditsBalanceAndRecordsEntry() { | ||
| 29 | var res = accounts.deposit("Alice", "250.5"); | ||
| 30 | assertTrue(res.isSuccess(), res.getDetails()); | ||
| 31 | assertEquals("350.50", res.getData().balance()); | ||
| 32 | |||
| 33 | var entry = res.getData().entries().getLast(); | ||
| 34 | assertEquals(LedgerType.DEPOSIT, entry.type()); | ||
| 35 | assertEquals("250.50", entry.amount()); | ||
| 36 | assertEquals("350.50", entry.balanceAfter()); | ||
| 37 | } | ||
| 38 | |||
| 39 | @Test | ||
| 40 | void depositUnblocksNegativeAccount() { | ||
| 41 | var ledger = new LedgerContext(userRepo, Clock.systemUTC()); | ||
| 42 | ledger.debit("alice", LedgerType.COMMISSION, new java.math.BigDecimal("150"), "fee"); | ||
| 43 | assertTrue(userRepo.get("alice").orElseThrow().isBlocked()); | ||
| 44 | |||
| 45 | accounts.deposit("alice", "60"); | ||
| 46 | assertFalse(userRepo.get("alice").orElseThrow().isBlocked()); | ||
| 47 | } | ||
| 48 | |||
| 49 | @Test | ||
| 50 | void rejectsInvalidDeposits() { | ||
| 51 | for (var bad : new String[] {"0", "-5", "abc", "", "1.005", null}) { | ||
| 52 | var res = accounts.deposit("alice", bad); | ||
| 53 | assertFalse(res.isSuccess(), "should reject: " + bad); | ||
| 54 | assertEquals("INVALID_AMOUNT", res.getMessage()); | ||
| 55 | } | ||
| 56 | assertEquals("ACCOUNT_NOT_FOUND", accounts.deposit("nobody", "10").getMessage()); | ||
| 57 | assertEquals("100.00", accounts.getAccount("alice").getData().balance()); | ||
| 58 | assertTrue(accounts.getAccount("alice").getData().entries().isEmpty()); | ||
| 59 | } | ||
| 60 | } | ||
diff --git a/ui-desktop/src/main/java/market/guess/ui/desktop/ServiceEngine.java b/ui-desktop/src/main/java/market/guess/ui/desktop/ServiceEngine.java index 549991e..6921735 100644 --- a/ui-desktop/src/main/java/market/guess/ui/desktop/ServiceEngine.java +++ b/ui-desktop/src/main/java/market/guess/ui/desktop/ServiceEngine.java | |||
| @@ -44,9 +44,9 @@ public final class ServiceEngine { | |||
| 44 | Loader loader = new XMLLoaderV2(eventRepo, userRepo, mapper, validator); | 44 | Loader loader = new XMLLoaderV2(eventRepo, userRepo, mapper, validator); |
| 45 | 45 | ||
| 46 | this.catalogContext = new LocalCatalogContext(loader, marketContextInfrastructure, eventRepo); | 46 | this.catalogContext = new LocalCatalogContext(loader, marketContextInfrastructure, eventRepo); |
| 47 | this.accountContext = new LocalAccountContext(userRepo); | ||
| 48 | |||
| 49 | LedgerContext ledgerContext = new LedgerContext(userRepo, clock); | 47 | LedgerContext ledgerContext = new LedgerContext(userRepo, clock); |
| 48 | this.accountContext = new LocalAccountContext(userRepo, ledgerContext); | ||
| 49 | |||
| 50 | RiskEngine risk = new LocalRiskEngine(eventRepo); | 50 | RiskEngine risk = new LocalRiskEngine(eventRepo); |
| 51 | MatchingEngine matching = new LocalMatchingEngine(clock); | 51 | MatchingEngine matching = new LocalMatchingEngine(clock); |
| 52 | FulfillmentContext fulfillment = new LocalFulfillmentContext(ledgerContext); | 52 | FulfillmentContext fulfillment = new LocalFulfillmentContext(ledgerContext); |