From 39786aaba5c769249865aead74a2f9b8319edc48 Mon Sep 17 00:00:00 2001 From: Kashey Date: Fri, 23 Oct 2026 17:24:52 +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=D1=80=D0=BE=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=20=D0=BE=D1=82=D0=BC=D0=B5=D0=BD=D1=8B=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B9=20=D1=81=20HTTP-?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8=20=D1=83=D1=81=D0=BF?= =?UTF-8?q?=D0=B5=D1=88=D0=BD=D1=8B=D1=85=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D1=81=D0=BE=D0=B2=20=D0=B8=20=D0=BD=D0=B5=D0=B8=D0=B7=D0=B2?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easymoney/api/OperationController.java | 15 +++++- .../api/OperationControllerTest.java | 54 ++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/sytes/kashey/easymoney/api/OperationController.java b/src/main/java/net/sytes/kashey/easymoney/api/OperationController.java index 5ae6cbc..30db1d1 100644 --- a/src/main/java/net/sytes/kashey/easymoney/api/OperationController.java +++ b/src/main/java/net/sytes/kashey/easymoney/api/OperationController.java @@ -31,7 +31,8 @@ public class OperationController { } @PostMapping("/opening-balance") - public ResponseEntity<@NonNull OperationResponse> createOpeningBalance(@RequestBody CreateOpeningBalanceRequest request) { + public ResponseEntity<@NonNull OperationResponse> createOpeningBalance( + @RequestBody CreateOpeningBalanceRequest request) { if (request.accountId() == null) { throw new InvalidOperationException("Не указан ID счёта"); } @@ -42,6 +43,18 @@ public class OperationController { return ResponseEntity.created(location).body(response); } + @PostMapping("/{id}/post") + public OperationResponse post(@PathVariable("id") Long id) { + operationService.post(id); + return getOperationResponse(operationService.getById(id)); + } + + @PostMapping("/{id}/unpost") + public OperationResponse unpost(@PathVariable("id") Long id) { + operationService.unpost(id); + return getOperationResponse(operationService.getById(id)); + } + private OperationResponse getOperationResponse(Operation operation) { return new OperationResponse( operation.getId(), diff --git a/src/test/java/net/sytes/kashey/easymoney/api/OperationControllerTest.java b/src/test/java/net/sytes/kashey/easymoney/api/OperationControllerTest.java index a7f1f39..8ca75a1 100644 --- a/src/test/java/net/sytes/kashey/easymoney/api/OperationControllerTest.java +++ b/src/test/java/net/sytes/kashey/easymoney/api/OperationControllerTest.java @@ -41,7 +41,6 @@ class OperationControllerTest { @Autowired private Ledger ledger; - @Test void gettingUnknownOperationReturnsNotFound() throws Exception { // When / Then @@ -160,4 +159,57 @@ class OperationControllerTest { .andExpect(jsonPath("$.detail").value("Счет не найден: 999")) .andExpect(jsonPath("$.status").value(404)); } + + @Test + void postingOperationByIdUpdatesBalanceAndReturnsPostedDocument() throws Exception { + // Given + LocalDate date = LocalDate.of(2026, 10, 23); + Account account = accountService.create("Карта"); + Operation operation = operationService.createOpeningBalance(date, account, new BigDecimal(15000)); + Long operationId = operation.getId(); + + // When / Then + mockMvc.perform(post("/api/operations/" + operationId + "/post")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", Long.class).value(operationId)) + .andExpect(jsonPath("$.posted").value(true)); + + assertEquals(0, new BigDecimal(15000).compareTo(ledger.balance(account, date))); + } + + @Test + void unpostingOperationByIdUpdatesBalanceAndReturnsUnpostedDocument() throws Exception { + // Given + LocalDate date = LocalDate.of(2026, 10, 23); + Account account = accountService.create("Карта"); + Operation operation = operationService.createOpeningBalance(date, account, new BigDecimal(15000)); + operationService.post(operation.getId()); + Long operationId = operation.getId(); + + // When / Then + mockMvc.perform(post("/api/operations/" + operationId + "/unpost")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", Long.class).value(operationId)) + .andExpect(jsonPath("$.posted").value(false)); + + assertEquals(0, BigDecimal.ZERO.compareTo(ledger.balance(account, date))); + } + + @Test + void postingUnknownOperationReturnsNotFound() throws Exception { + // When / Then + mockMvc.perform(post("/api/operations/999/post")) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.status").value(404)) + .andExpect(jsonPath("$.detail").value("Операция не найдена: 999")); + } + + @Test + void unpostingUnknownOperationReturnsNotFound() throws Exception { + // When / Then + mockMvc.perform(post("/api/operations/999/unpost")) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.status").value(404)) + .andExpect(jsonPath("$.detail").value("Операция не найдена: 999")); + } } \ No newline at end of file