Skip to content

Commit 4e687a6

Browse files
authored
Update README and CB updates (#931)
1 parent 6cd38c2 commit 4e687a6

File tree

12 files changed

+132
-64
lines changed

12 files changed

+132
-64
lines changed

cloudbank-v4/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ This is an example of the `customer32` application:
250250
251251
1. Test `customer` service
252252
253-
1. REST endpoint
253+
1. GET REST endpoint.
254254
255255
```shell
256256
curl -s http://$IP/api/v1/customer | jq
@@ -272,6 +272,21 @@ This is an example of the `customer32` application:
272272
]
273273
```
274274
275+
1. POST endpoint to create a customer.
276+
277+
```shell
278+
curl -i -X POST 'http://$IP/api/v1/customer' -H 'Content-Type: application/json' -d '{"customerId": "bobsmith", "customerName": "Bob Smith", "customerEmail": "bob@smith.com"}'
279+
```
280+
281+
Should return the URI of the created object:
282+
283+
```text
284+
HTTP/1.1 201
285+
Location: http://localhost:8080/api/v1/customer/bobsmith
286+
Content-Length: 0
287+
Date: Tue, 03 Sep 2024 21:01:25 GMT
288+
```
289+
275290
1. Test `creditscore` service
276291
277292
1. REST endpoint

cloudbank-v4/account/src/main/java/com/example/accounts/controller/AccountController.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public AccountController(AccountRepository accountRepository, JournalRepository
3737

3838
/**
3939
* Get all Accounts.
40+
*
4041
* @return List off accounts
4142
*/
4243
@GetMapping("/accounts")
@@ -46,26 +47,34 @@ public List<Account> getAllAccounts() {
4647

4748
/**
4849
* Create an account.
49-
* @param account Account object
50-
* @return Http Status Code
50+
*
51+
* @param account Account object.
52+
* @return Returns HTTP Status code or the URI of the created object.
5153
*/
5254
@PostMapping("/account")
5355
public ResponseEntity<Account> createAccount(@RequestBody Account account) {
54-
try {
55-
Account newAccount = accountRepository.saveAndFlush(account);
56-
URI location = ServletUriComponentsBuilder
57-
.fromCurrentRequest()
58-
.path("/{id}")
59-
.buildAndExpand(newAccount.getAccountId())
60-
.toUri();
61-
return ResponseEntity.created(location).build();
62-
} catch (Exception e) {
63-
return new ResponseEntity<>(account, HttpStatus.INTERNAL_SERVER_ERROR);
56+
boolean exists = accountRepository.existsById(account.getAccountId());
57+
58+
if (!exists) {
59+
try {
60+
Account newAccount = accountRepository.saveAndFlush(account);
61+
URI location = ServletUriComponentsBuilder
62+
.fromCurrentRequest()
63+
.path("/{id}")
64+
.buildAndExpand(newAccount.getAccountId())
65+
.toUri();
66+
return ResponseEntity.created(location).build();
67+
} catch (Exception e) {
68+
return new ResponseEntity<>(account, HttpStatus.INTERNAL_SERVER_ERROR);
69+
}
70+
} else {
71+
return new ResponseEntity<>(account, HttpStatus.CONFLICT);
6472
}
6573
}
6674

6775
/**
6876
* Find an account by Account Id.
77+
*
6978
* @param accountId Account Id
7079
* @return An account
7180
*/
@@ -82,6 +91,7 @@ public ResponseEntity<Account> getAccountById(@PathVariable("accountId") long ac
8291

8392
/**
8493
* Find an account by customer Id.
94+
*
8595
* @param customerId Customer Id
8696
* @return A list opf Account(s)
8797
*/
@@ -101,6 +111,7 @@ public ResponseEntity<List<Account>> getAccountsByCustomerId(@PathVariable("cust
101111

102112
/**
103113
* Delete an Account with specific Id.
114+
*
104115
* @param accountId Account ID
105116
* @return HTTP Status Code
106117
*/
@@ -116,6 +127,7 @@ public ResponseEntity<HttpStatus> deleteAccount(@PathVariable("accountId") long
116127

117128
/**
118129
* Get transactions (Journal) for an Account Id.
130+
*
119131
* @param accountId Account Id
120132
* @return List of Journal object(s)
121133
*/
@@ -135,21 +147,28 @@ public ResponseEntity<List<Journal>> getTransactions(@PathVariable("accountId")
135147

136148
/**
137149
* Create a Journal entry.
150+
*
138151
* @param journalEntry Journal object
139152
* @return HTTP Status Code
140153
*/
141154
@PostMapping("/account/journal")
142155
public ResponseEntity<Journal> postSimpleJournalEntry(@RequestBody Journal journalEntry) {
143-
try {
144-
Journal newJournalEntry = journalRepository.saveAndFlush(journalEntry);
145-
return new ResponseEntity<>(newJournalEntry, HttpStatus.CREATED);
146-
} catch (Exception e) {
147-
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
156+
boolean exists = journalRepository.existsById(journalEntry.getJournalId());
157+
if (!exists) {
158+
try {
159+
Journal newJournalEntry = journalRepository.saveAndFlush(journalEntry);
160+
return new ResponseEntity<>(newJournalEntry, HttpStatus.CREATED);
161+
} catch (Exception e) {
162+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
163+
}
164+
} else {
165+
return new ResponseEntity<>(journalEntry, HttpStatus.CONFLICT);
148166
}
149167
}
150168

151169
/**
152170
* Find Journal entries by Account Id.
171+
*
153172
* @param accountId Account Id
154173
* @return Journal object(s)
155174
*/
@@ -160,12 +179,14 @@ public List<Journal> getJournalEntriesForAccount(@PathVariable("accountId") long
160179

161180
/**
162181
* Clears the Journal Entry.
182+
*
163183
* @param journalId Journal Id
164184
* @return HTTP Status Code
165185
*/
166186
@PostMapping("/account/journal/{journalId}/clear")
167187
public ResponseEntity<Journal> clearJournalEntry(@PathVariable long journalId) {
168188
try {
189+
boolean exists = journalRepository.existsById(journalId);
169190
Optional<Journal> data = journalRepository.findById(journalId);
170191
if (data.isPresent()) {
171192
Journal newJournalEntry = data.get();

cloudbank-v4/customer/src/main/java/com/example/customer/controller/CustomerController.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
package com.example.customer.controller;
55

6+
import java.net.URI;
67
import java.util.List;
78
import java.util.Optional;
89

910
import com.example.customer.model.Customers;
1011
import com.example.customer.repository.CustomersRepository;
12+
import lombok.extern.slf4j.Slf4j;
1113
import org.springframework.http.HttpStatus;
1214
import org.springframework.http.ResponseEntity;
1315
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -19,9 +21,11 @@
1921
import org.springframework.web.bind.annotation.RequestMapping;
2022
import org.springframework.web.bind.annotation.ResponseStatus;
2123
import org.springframework.web.bind.annotation.RestController;
24+
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
2225

2326
@RestController
2427
@RequestMapping("/api/v1")
28+
@Slf4j
2529
public class CustomerController {
2630
final CustomersRepository customersRepository;
2731

@@ -44,6 +48,7 @@ public List<Customers> findByCustomerByName(@PathVariable String customerName) {
4448

4549
/**
4650
* Get Customer with specific ID.
51+
*
4752
* @param id The CustomerId
4853
* @return If the customers is found, a customer and HTTP Status code.
4954
*/
@@ -60,6 +65,7 @@ public ResponseEntity<Customers> getCustomerById(@PathVariable("id") String id)
6065

6166
/**
6267
* Get customer that contains an email.
68+
*
6369
* @param email of the customer
6470
* @return Returns a customer if found
6571
*/
@@ -70,27 +76,40 @@ public List<Customers> getCustomerByEmail(@PathVariable("email") String email) {
7076

7177
/**
7278
* Create a customer.
73-
* @param customer Customer object with the customer details
74-
* @return Returns a HTTP Status code
79+
*
80+
* @param customer Customer object with the customer details.
81+
* @return Returns HTTP Status code or the URI of the created object.
7582
*/
7683
@PostMapping("/customer")
7784
public ResponseEntity<Customers> createCustomer(@RequestBody Customers customer) {
78-
try {
79-
Customers newCustomer = customersRepository.save(new Customers(
80-
customer.getCustomerId(),
81-
customer.getCustomerName(),
82-
customer.getCustomerEmail(),
83-
customer.getCustomerOtherDetails()));
84-
return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
85+
boolean exists = customersRepository.existsById(customer.getCustomerId());
8586

86-
} catch (Exception e) {
87-
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
87+
if (!exists) {
88+
try {
89+
Customers newCustomer = customersRepository.saveAndFlush(new Customers(
90+
customer.getCustomerId(),
91+
customer.getCustomerName(),
92+
customer.getCustomerEmail(),
93+
customer.getCustomerOtherDetails()));
94+
95+
URI location = ServletUriComponentsBuilder
96+
.fromCurrentRequest()
97+
.path("/{id}")
98+
.buildAndExpand(newCustomer.getCustomerId())
99+
.toUri();
100+
return ResponseEntity.created(location).build();
101+
} catch (Exception e) {
102+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
103+
}
104+
} else {
105+
return new ResponseEntity<>(customer, HttpStatus.CONFLICT);
88106
}
89107
}
90108

91109
/**
92110
* Update a specific Customer (ID).
93-
* @param id The id of the customer
111+
*
112+
* @param id The id of the customer
94113
* @param customer A customer object
95114
* @return A Http Status code
96115
*/
@@ -114,6 +133,7 @@ public ResponseEntity<Customers> updateCustomer(@PathVariable("id") String id, @
114133

115134
/**
116135
* Delete a specific customer (ID).
136+
*
117137
* @param customerId the Id of the customer to be deleted
118138
* @return A Http Status code
119139
*/
@@ -129,11 +149,12 @@ public ResponseEntity<HttpStatus> deleteCustomer(@PathVariable("customerId") Str
129149

130150
/**
131151
* Method isn't implemented.
152+
*
132153
* @param amount Loan amount
133154
* @return A Http Status
134155
*/
135156
@PostMapping("/customer/applyLoan/{amount}")
136-
public ResponseEntity<HttpStatus> applyForLoan(@PathVariable ("amount") long amount) {
157+
public ResponseEntity<HttpStatus> applyForLoan(@PathVariable("amount") long amount) {
137158
try {
138159
// Check Credit Rating
139160
// Amount vs Rating approval?

docs-source/cloudbank/content/account/cr-accounts.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ weight = 7
111111

112112
Now we want to create an endpoint to create a new account. Open `AccountController.java` and add a new `createAccount` method. This method should return `ResponseEntity<Account>` this will allow you to return the account object, but also gives you access to set headers, status code and so on. The method needs to take an `Account` as an argument. Add the `RequestBody` annotation to the argument to tell Spring Boot that the input data will be in the HTTP request's body.
113113

114-
Inside the method, you should use the `saveAndFlush` method on the JPA Repository to save a new instance of `Account` in the database. The `saveAndFlush` method returns the created object. If the save was successful, return the created object and set the HTTP Status Code to 201 (Created). If there is an error, set the HTTP Status Code to 500 (Internal Server Error).
114+
Inside the method, you should use the `saveAndFlush` method on the JPA Repository to save a new instance of `Account` in the database. The `saveAndFlush` method returns the created object. If the save was successful, return the URI to the created object and set the HTTP Status Code to 201 (Created). If the object already exists set the HTTP Status code to 409 (Conflict). If there is an error, set the HTTP Status Code to 500 (Internal Server Error).
115115

116116
Here's what the new method (and imports) should look like:
117117
@@ -130,16 +130,22 @@ weight = 7
130130
131131
@PostMapping("/account")
132132
public ResponseEntity<Account> createAccount(@RequestBody Account account) {
133-
try {
134-
Account newAccount = accountRepository.saveAndFlush(account);
135-
URI location = ServletUriComponentsBuilder
136-
.fromCurrentRequest()
137-
.path("/{id}")
138-
.buildAndExpand(newAccount.getAccountId())
139-
.toUri();
140-
return ResponseEntity.created(location).build();
141-
} catch (Exception e) {
142-
return new ResponseEntity<>(account, HttpStatus.INTERNAL_SERVER_ERROR);
133+
boolean exists = accountRepository.existsById(account.getAccountId());
134+
135+
if (!exists) {
136+
try {
137+
Account newAccount = accountRepository.saveAndFlush(account);
138+
URI location = ServletUriComponentsBuilder
139+
.fromCurrentRequest()
140+
.path("/{id}")
141+
.buildAndExpand(newAccount.getAccountId())
142+
.toUri();
143+
return ResponseEntity.created(location).build();
144+
} catch (Exception e) {
145+
return new ResponseEntity<>(account, HttpStatus.INTERNAL_SERVER_ERROR);
146+
}
147+
} else {
148+
return new ResponseEntity<>(account, HttpStatus.CONFLICT);
143149
}
144150
}
145151
```

docs-source/cloudbank/content/account/deploy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ weight = 9
123123
\_/ |_) (_| (_| __) \_ |_ _|_
124124
========================================================================================
125125
Application Name: Oracle Backend Platform :: Command Line Interface
126-
Application Version: (1.2.0)
127-
:: Spring Boot (v3.3.0) ::
126+
Application Version: (1.3.0)
127+
:: Spring Boot (v3.3.3) ::
128128
129129
Ask for help:
130130
- Slack: https://oracledevs.slack.com/archives/C03ALDSV272

docs-source/cloudbank/content/check/check-processing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ Next, you will create the "Check Processing" microservice which you will receive
419419
\_/ |_) (_| (_| __) \_ |_ _|_
420420
========================================================================================
421421
Application Name: Oracle Backend Platform :: Command Line Interface
422-
Application Version: (1.2.0)
423-
:: Spring Boot (v3.3.0) ::
422+
Application Version: (1.3.0)
423+
:: Spring Boot (v3.3.3) ::
424424

425425
Ask for help:
426426
- Slack: https://oracledevs.slack.com/archives/C03ALDSV272

docs-source/cloudbank/content/check/create-testrunner.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ Next, you will create the "Test Runner" microservice which you will use to simul
264264
\_/ |_) (_| (_| __) \_ |_ _|_
265265
========================================================================================
266266
Application Name: Oracle Backend Platform :: Command Line Interface
267-
Application Version: (1.2.0)
268-
:: Spring Boot (v3.3.0) ::
267+
Application Version: (1.3.0)
268+
:: Spring Boot (v3.3.3) ::
269269

270270
Ask for help:
271271
- Slack: https://oracledevs.slack.com/archives/C03ALDSV272

docs-source/cloudbank/content/check/update-account.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,16 @@ Starting with the account service that you built in the previous lab, you will t
113113

114114
@PostMapping("/account/journal")
115115
public ResponseEntity<Journal> postSimpleJournalEntry(@RequestBody Journal journalEntry) {
116-
try {
117-
Journal _journalEntry = journalRepository.saveAndFlush(journalEntry);
118-
return new ResponseEntity<>(_journalEntry, HttpStatus.CREATED);
119-
} catch (Exception e) {
120-
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
116+
boolean exists = journalRepository.existsById(journalEntry.getJournalId());
117+
if (!exists) {
118+
try {
119+
Journal newJournalEntry = journalRepository.saveAndFlush(journalEntry);
120+
return new ResponseEntity<>(newJournalEntry, HttpStatus.CREATED);
121+
} catch (Exception e) {
122+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
123+
}
124+
} else {
125+
return new ResponseEntity<>(journalEntry, HttpStatus.CONFLICT);
121126
}
122127
}
123128
```
@@ -195,8 +200,8 @@ Starting with the account service that you built in the previous lab, you will t
195200
\_/ |_) (_| (_| __) \_ |_ _|_
196201
========================================================================================
197202
Application Name: Oracle Backend Platform :: Command Line Interface
198-
Application Version: (1.2.0)
199-
:: Spring Boot (v3.3.0) ::
203+
Application Version: (1.3.0)
204+
:: Spring Boot (v3.3.3) ::
200205
201206
Ask for help:
202207
- Slack: https://oracledevs.slack.com/archives/C03ALDSV272

docs-source/cloudbank/content/deploy-cli/deploy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ weight = 4
3737
\_/ |_) (_| (_| __) \_ |_ _|_
3838
========================================================================================
3939
Application Name: Oracle Backend Platform :: Command Line Interface
40-
Application Version: (1.2.0)
41-
:: Spring Boot (v3.3.0) ::
40+
Application Version: (1.3.0)
41+
:: Spring Boot (v3.3.3) ::
4242
4343
Ask for help:
4444
- Slack: https://oracledevs.slack.com/archives/C03ALDSV272

0 commit comments

Comments
 (0)