Skip to content

Commit d0f6780

Browse files
authored
add api to get list of transactions in an account (#601)
Signed-off-by: Mark Nelson <[email protected]>
1 parent 09bd7bf commit d0f6780

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

cloudbank-v2/spring-apps/account/src/main/java/com/example/accounts/controller/AccountController.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import org.springframework.web.bind.annotation.RequestMapping;
88
import org.springframework.web.bind.annotation.RestController;
99
import com.example.accounts.repository.AccountRepository;
10+
import com.example.accounts.repository.JournalRepository;
1011
import java.util.List;
1112
import com.example.accounts.model.Account;
13+
import com.example.accounts.model.Journal;
1214
import org.springframework.web.bind.annotation.PostMapping;
1315
import org.springframework.web.bind.annotation.RequestBody;
1416
import org.springframework.http.HttpStatus;
@@ -25,14 +27,16 @@
2527
public class AccountController {
2628

2729
final AccountRepository accountRepository;
30+
final JournalRepository journalRepository;
2831

2932
@GetMapping("/hello")
3033
public String ping() {
3134
return "Hello from Spring Boot";
3235
}
3336

34-
public AccountController(AccountRepository accountRepository) {
37+
public AccountController(AccountRepository accountRepository, JournalRepository journalRepository) {
3538
this.accountRepository = accountRepository;
39+
this.journalRepository = journalRepository;
3640
}
3741

3842
@GetMapping("/accounts")
@@ -85,4 +89,18 @@ public ResponseEntity<HttpStatus> deleteAccount(@PathVariable("accountId") long
8589
}
8690
}
8791

92+
@GetMapping("/account/{accountId}/transactions")
93+
public ResponseEntity<List<Journal>> getTransactions(@PathVariable("accountId") long accountId) {
94+
try {
95+
List<Journal> transactions = new ArrayList<Journal>();
96+
transactions.addAll(journalRepository.findByAccountId(accountId));
97+
if (transactions.isEmpty()) {
98+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
99+
}
100+
return new ResponseEntity<>(transactions, HttpStatus.OK);
101+
} catch (Exception e) {
102+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
103+
}
104+
}
105+
88106
}

cloudbank-v2/spring-apps/account/src/main/java/com/example/accounts/repository/JournalRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010

1111
public interface JournalRepository extends JpaRepository<Journal, Long> {
1212
Journal findJournalByLraIdAndJournalType(String lraId, String journalType);
13+
List<Journal> findByAccountId(long accountId);
1314
}

0 commit comments

Comments
 (0)