diff --git a/pom.xml b/pom.xml
index 100ad54..af2e220 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,20 +5,37 @@
4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 4.0.8
+
+
+
net.sytes.kashey
easy-money
1.0-SNAPSHOT
- 21
+ 21
UTF-8
- org.junit.jupiter
- junit-jupiter
- 5.11.4
+ org.springframework.boot
+ spring-boot-starter-webmvc
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+ org.springframework.boot
+ spring-boot-starter-webmvc-test
test
@@ -26,9 +43,8 @@
- org.apache.maven.plugins
- maven-surefire-plugin
- 3.5.2
+ org.springframework.boot
+ spring-boot-maven-plugin
diff --git a/src/main/java/net/sytes/kashey/easymoney/EasyMoneyApplication.java b/src/main/java/net/sytes/kashey/easymoney/EasyMoneyApplication.java
new file mode 100644
index 0000000..6ea60d1
--- /dev/null
+++ b/src/main/java/net/sytes/kashey/easymoney/EasyMoneyApplication.java
@@ -0,0 +1,12 @@
+package net.sytes.kashey.easymoney;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class EasyMoneyApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(EasyMoneyApplication.class, args);
+ }
+}
diff --git a/src/main/java/net/sytes/kashey/easymoney/api/ApiExceptionHandler.java b/src/main/java/net/sytes/kashey/easymoney/api/ApiExceptionHandler.java
new file mode 100644
index 0000000..9ddef8f
--- /dev/null
+++ b/src/main/java/net/sytes/kashey/easymoney/api/ApiExceptionHandler.java
@@ -0,0 +1,17 @@
+package net.sytes.kashey.easymoney.api;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ProblemDetail;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import service.OperationNotFoundException;
+
+@RestControllerAdvice
+public class ApiExceptionHandler {
+
+ @ExceptionHandler(OperationNotFoundException.class)
+ public ProblemDetail handleOperationNotFound(OperationNotFoundException exception) {
+ return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, exception.getMessage());
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/net/sytes/kashey/easymoney/api/OperationController.java b/src/main/java/net/sytes/kashey/easymoney/api/OperationController.java
new file mode 100644
index 0000000..977d40b
--- /dev/null
+++ b/src/main/java/net/sytes/kashey/easymoney/api/OperationController.java
@@ -0,0 +1,34 @@
+package net.sytes.kashey.easymoney.api;
+
+import model.Operation;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import service.OperationService;
+
+@RestController
+@RequestMapping("/api/operations")
+public class OperationController {
+
+ private final OperationService service;
+
+ public OperationController(OperationService service) {
+ this.service = service;
+ }
+
+ @GetMapping("/{id}")
+ public OperationResponse getById(@PathVariable("id") Long id) {
+ Operation operation = service.getById(id);
+
+ return new OperationResponse(
+ operation.getId(),
+ operation.getDate(),
+ operation.getType(),
+ operation.getSourceAccount() == null ? null : operation.getSourceAccount().getId(),
+ operation.getDestinationAccount() == null ? null : operation.getDestinationAccount().getId(),
+ operation.getTotal(),
+ service.isPosted(operation)
+ );
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/net/sytes/kashey/easymoney/api/OperationResponse.java b/src/main/java/net/sytes/kashey/easymoney/api/OperationResponse.java
new file mode 100644
index 0000000..b108d30
--- /dev/null
+++ b/src/main/java/net/sytes/kashey/easymoney/api/OperationResponse.java
@@ -0,0 +1,17 @@
+package net.sytes.kashey.easymoney.api;
+
+import model.OperationType;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+
+public record OperationResponse(
+ Long id,
+ LocalDate date,
+ OperationType type,
+ Long sourceAccountId,
+ Long destinationAccountId,
+ BigDecimal total,
+ boolean posted
+) {
+}
\ No newline at end of file
diff --git a/src/main/java/net/sytes/kashey/easymoney/api/StatusController.java b/src/main/java/net/sytes/kashey/easymoney/api/StatusController.java
new file mode 100644
index 0000000..6f55ca6
--- /dev/null
+++ b/src/main/java/net/sytes/kashey/easymoney/api/StatusController.java
@@ -0,0 +1,15 @@
+package net.sytes.kashey.easymoney.api;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Map;
+
+@RestController
+public class StatusController {
+
+ @GetMapping("/api/status")
+ public Map status() {
+ return Map.of("status", "ok");
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/net/sytes/kashey/easymoney/config/AccountingConfig.java b/src/main/java/net/sytes/kashey/easymoney/config/AccountingConfig.java
new file mode 100644
index 0000000..b531861
--- /dev/null
+++ b/src/main/java/net/sytes/kashey/easymoney/config/AccountingConfig.java
@@ -0,0 +1,30 @@
+package net.sytes.kashey.easymoney.config;
+
+import model.OperationJournal;
+import service.Ledger;
+import service.OperationService;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class AccountingConfig {
+
+ @Bean
+ public OperationJournal operationJournal() {
+ return new OperationJournal();
+ }
+
+ @Bean
+ public Ledger ledger() {
+ return new Ledger();
+ }
+
+ @Bean
+ public OperationService operationService(
+ OperationJournal journal,
+ Ledger ledger
+ ) {
+ return new OperationService(journal, ledger);
+ }
+}
diff --git a/src/main/java/service/OperationNotFoundException.java b/src/main/java/service/OperationNotFoundException.java
new file mode 100644
index 0000000..175e0df
--- /dev/null
+++ b/src/main/java/service/OperationNotFoundException.java
@@ -0,0 +1,8 @@
+package service;
+
+public class OperationNotFoundException extends RuntimeException {
+
+ public OperationNotFoundException(Long operationId) {
+ super("Операция не найдена: " + operationId);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/service/OperationService.java b/src/main/java/service/OperationService.java
index 1311715..97f41da 100644
--- a/src/main/java/service/OperationService.java
+++ b/src/main/java/service/OperationService.java
@@ -44,6 +44,10 @@ public class OperationService {
return ledger.isPosted(operation.getId());
}
+ public Operation getById(Long operationId) {
+ return requiredOperation(operationId);
+ }
+
private Operation requiredOperation(Long operationId) {
if (operationId == null) {
@@ -53,9 +57,7 @@ public class OperationService {
Operation operation = journal.getOperationById(operationId);
if (operation == null) {
- throw new IllegalArgumentException(
- "Операция не найдена: " + operationId
- );
+ throw new OperationNotFoundException(operationId);
}
return operation;
}
diff --git a/src/test/java/net/sytes/kashey/easymoney/api/OperationControllerTest.java b/src/test/java/net/sytes/kashey/easymoney/api/OperationControllerTest.java
new file mode 100644
index 0000000..5b867d0
--- /dev/null
+++ b/src/test/java/net/sytes/kashey/easymoney/api/OperationControllerTest.java
@@ -0,0 +1,31 @@
+package net.sytes.kashey.easymoney.api;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
+import org.springframework.context.annotation.Import;
+
+import net.sytes.kashey.easymoney.config.AccountingConfig;
+
+import org.springframework.test.web.servlet.MockMvc;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+@WebMvcTest(OperationController.class)
+@Import(AccountingConfig.class)
+class OperationControllerTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @Test
+ void gettingUnknownOperationReturnsNotFound() throws Exception {
+ // When / Then
+ mockMvc.perform(get("/api/operations/999"))
+ .andExpect(status().isNotFound())
+ .andExpect(jsonPath("$.status").value(404))
+ .andExpect(jsonPath("$.detail")
+ .value("Операция не найдена: 999"));
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/service/OperationServiceTest.java b/src/test/java/service/OperationServiceTest.java
index 23d15be..0a40823 100644
--- a/src/test/java/service/OperationServiceTest.java
+++ b/src/test/java/service/OperationServiceTest.java
@@ -148,7 +148,7 @@ class OperationServiceTest {
operationService.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
// When / Then
- assertThrows(IllegalArgumentException.class, () -> operationService.post(999L));
+ assertThrows(OperationNotFoundException.class, () -> operationService.post(999L));
}
@Test
@@ -162,6 +162,6 @@ class OperationServiceTest {
operationService.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
// When / Then
- assertThrows(IllegalArgumentException.class, () -> operationService.unpost(999L));
+ assertThrows(OperationNotFoundException.class, () -> operationService.unpost(999L));
}
}
\ No newline at end of file