77import org .springframework .web .bind .annotation .RequestMapping ;
88import org .springframework .web .bind .annotation .RestController ;
99import com .example .accounts .repository .AccountRepository ;
10+ import com .example .accounts .repository .JournalRepository ;
1011import java .util .List ;
1112import com .example .accounts .model .Account ;
13+ import com .example .accounts .model .Journal ;
1214import org .springframework .web .bind .annotation .PostMapping ;
1315import org .springframework .web .bind .annotation .RequestBody ;
1416import org .springframework .http .HttpStatus ;
2527public 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}
0 commit comments