From d6a548c7b70cfd12ddbf19604bdf8aeece490ca5 Mon Sep 17 00:00:00 2001 From: Kashey Date: Sun, 25 Oct 2026 22:59:34 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20AP?= =?UTF-8?q?I=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B0=D1=82=D0=BA=D0=B0=20=D1=81=D1=87=D1=91?= =?UTF-8?q?=D1=82=D0=B0=20=D0=BD=D0=B0=20=D0=B4=D0=B0=D1=82=D1=83=20=D0=B8?= =?UTF-8?q?=20HTTP-=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easymoney/api/AccountController.java | 25 +++++++++---- src/main/java/service/OperationService.java | 4 +++ .../easymoney/api/AccountControllerTest.java | 35 +++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/sytes/kashey/easymoney/api/AccountController.java b/src/main/java/net/sytes/kashey/easymoney/api/AccountController.java index 565d24b..c3f6378 100644 --- a/src/main/java/net/sytes/kashey/easymoney/api/AccountController.java +++ b/src/main/java/net/sytes/kashey/easymoney/api/AccountController.java @@ -2,33 +2,46 @@ package net.sytes.kashey.easymoney.api; import model.Account; import org.jspecify.annotations.NonNull; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import service.AccountService; +import service.OperationService; +import java.math.BigDecimal; import java.net.URI; +import java.time.LocalDate; @RestController @RequestMapping("/api/accounts") public class AccountController { - private final AccountService service; + private final AccountService accountService; + private final OperationService operationService; - public AccountController(AccountService service) { - this.service = service; + public AccountController(AccountService service, OperationService operationService) { + this.accountService = service; + this.operationService = operationService; } @GetMapping("/{id}") public AccountResponse getById(@PathVariable("id") Long id) { - Account account = service.getById(id); + Account account = accountService.getById(id); return new AccountResponse(account.getId(), account.getName()); } @PostMapping public ResponseEntity<@NonNull AccountResponse> saveAccount(@RequestBody CreateAccountRequest request) { - Account account = service.create(request.name()); - AccountResponse response = new AccountResponse(account.getId(), account.getName()); + Account account = accountService.create(request.name()); + AccountResponse response = new AccountResponse(account.getId(), account.getName()); URI location = URI.create("/api/accounts/" + account.getId()); return ResponseEntity.created(location).body(response); } + + @GetMapping("/{id}/balance") + public BigDecimal getBalance(@PathVariable("id") Long id, + @RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) { + Account account = accountService.getById(id); + return operationService.balance(account, date); + } } \ No newline at end of file diff --git a/src/main/java/service/OperationService.java b/src/main/java/service/OperationService.java index a03e346..cce90e1 100644 --- a/src/main/java/service/OperationService.java +++ b/src/main/java/service/OperationService.java @@ -58,6 +58,10 @@ public class OperationService { return operation; } + public synchronized BigDecimal balance(Account account, LocalDate date) { + return ledger.balance(account, date); + } + private Operation requiredOperation(Long operationId) { Operation operation = journal.getOperationById(operationId); diff --git a/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java b/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java index c27aa08..f595fc4 100644 --- a/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java +++ b/src/test/java/net/sytes/kashey/easymoney/api/AccountControllerTest.java @@ -1,5 +1,7 @@ package net.sytes.kashey.easymoney.api; +import model.Account; +import model.Operation; import net.sytes.kashey.easymoney.config.AccountingConfig; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -11,14 +13,21 @@ import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.springframework.test.web.servlet.MvcResult; +import service.AccountService; +import service.Ledger; +import service.OperationService; import tools.jackson.databind.ObjectMapper; +import java.math.BigDecimal; +import java.time.LocalDate; + @WebMvcTest(AccountController.class) @Import(AccountingConfig.class) class AccountControllerTest { @@ -27,6 +36,12 @@ class AccountControllerTest { private MockMvc mockMvc; @Autowired private ObjectMapper objectMapper; + @Autowired + private AccountService accountService; + @Autowired + private OperationService operationService; + @Autowired + private Ledger ledger; @Test void creatingAccountReturnsCreatedAccountAndLocation() throws Exception { @@ -98,4 +113,24 @@ class AccountControllerTest { .andExpect(jsonPath("$.id", Long.class).value(createdAccount.id())) .andExpect(jsonPath("$.name").value("Карта")); } + + @Test + void gettingAccountBalanceReturnsBalanceOnRequestedDate() throws Exception { + // Given + LocalDate date = LocalDate.of(2026, 10, 25); + Account account = accountService.create("Карта"); + Operation operation = operationService.createOpeningBalance(date, account, new BigDecimal(15000)); + operationService.post(operation.getId()); + Long accountId = account.getId(); + + // When / Then + MvcResult result = mockMvc.perform(get("/api/accounts/{id}/balance", accountId) + .param("date", date.toString())) + .andExpect(status().isOk()) + .andReturn(); + + BigDecimal response = objectMapper.readValue(result.getResponse().getContentAsString(), + BigDecimal.class); + assertEquals(0, response.compareTo(new BigDecimal(15000))); + } } \ No newline at end of file