Skip to content

Commit 56e5f5c

Browse files
committed
feat: creat account
1 parent bc89362 commit 56e5f5c

File tree

8 files changed

+124
-4
lines changed

8 files changed

+124
-4
lines changed

mvnw

100644100755
File mode changed.

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<artifactId>spring-boot-starter-web</artifactId>
3333
</dependency>
3434

35+
<dependency>
36+
<groupId>com.github.mifmif</groupId>
37+
<artifactId>generex</artifactId>
38+
<version>1.0.2</version>
39+
</dependency>
3540
<dependency>
3641
<groupId>com.h2database</groupId>
3742
<artifactId>h2</artifactId>

src/main/java/com/example/paul/controllers/AccountRestController.java

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.example.paul.models.Account;
44
import com.example.paul.services.AccountService;
55
import com.example.paul.utils.AccountInput;
6+
import com.example.paul.utils.CreateAccountInput;
67
import com.example.paul.utils.InputValidator;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -30,6 +31,9 @@ public class AccountRestController {
3031
private static final String NO_ACCOUNT_FOUND =
3132
"Unable to find an account matching this sort code and account number";
3233

34+
private static final String CREATE_ACCOUNT_FAILED =
35+
"Error happened during creating new account";
36+
3337
private final AccountService accountService;
3438

3539
@Autowired
@@ -62,6 +66,31 @@ public ResponseEntity<?> checkAccountBalance(
6266
}
6367
}
6468

69+
70+
@PutMapping(value = "/accounts",
71+
consumes = MediaType.APPLICATION_JSON_VALUE,
72+
produces = MediaType.APPLICATION_JSON_VALUE)
73+
public ResponseEntity<?> createAccount(
74+
@Valid @RequestBody CreateAccountInput createAccountInput) {
75+
LOGGER.debug("Triggered AccountRestController.accountInput");
76+
77+
// Validate input
78+
if (InputValidator.isCreateAccountCriteriaValid(createAccountInput)) {
79+
// Attempt to retrieve the account information
80+
Account account = accountService.createAccount(
81+
createAccountInput.getBankName(), createAccountInput.getOwnerName());
82+
83+
// Return the account details, or warn that no account was found for given input
84+
if (account == null) {
85+
return new ResponseEntity<>(CREATE_ACCOUNT_FAILED, HttpStatus.NO_CONTENT);
86+
} else {
87+
return new ResponseEntity<>(account, HttpStatus.OK);
88+
}
89+
} else {
90+
return new ResponseEntity<>(INVALID_SEARCH_CRITERIA, HttpStatus.BAD_REQUEST);
91+
}
92+
}
93+
6594
@ResponseStatus(HttpStatus.BAD_REQUEST)
6695
@ExceptionHandler(MethodArgumentNotValidException.class)
6796
public Map<String, String> handleValidationExceptions(

src/main/java/com/example/paul/models/Account.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.paul.models;
22

33
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
45
import javax.persistence.Id;
56
import javax.persistence.Table;
67
import java.util.List;
@@ -11,7 +12,7 @@
1112
@Table(name = "account", schema = "online_bank")
1213
public class Account {
1314

14-
@Id
15+
@Id @GeneratedValue
1516
private long id;
1617

1718
private String sortCode;
@@ -27,6 +28,13 @@ public class Account {
2728
private transient List<Transaction> transactions;
2829

2930
protected Account() {}
31+
public Account(String bankName, String ownerName, String generateSortCode, String generateAccountNumber, double currentBalance) {
32+
this.sortCode = generateSortCode;
33+
this.accountNumber = generateAccountNumber;
34+
this.currentBalance = currentBalance;
35+
this.bankName = bankName;
36+
this.ownerName = ownerName;
37+
}
3038
public Account(long id, String sortCode, String accountNumber, double currentBalance, String bankName, String ownerName) {
3139
this.id = id;
3240
this.sortCode = sortCode;
@@ -35,6 +43,7 @@ public Account(long id, String sortCode, String accountNumber, double currentBal
3543
this.bankName = bankName;
3644
this.ownerName = ownerName;
3745
}
46+
3847
public Account(long id, String sortCode, String accountNumber, double currentBalance, String bankName, String ownerName, List<Transaction> transactions) {
3948
this.id = id;
4049
this.sortCode = sortCode;

src/main/java/com/example/paul/services/AccountService.java

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.example.paul.models.Account;
44
import com.example.paul.repositories.AccountRepository;
55
import com.example.paul.repositories.TransactionRepository;
6+
import com.example.paul.utils.CreateAccountInput;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Service;
89

@@ -27,4 +28,10 @@ public Account getAccount(String sortCode, String accountNumber) {
2728

2829
return account.orElse(null);
2930
}
31+
32+
public Account createAccount(String bankName, String ownerName) {
33+
CreateAccountInput createAccountInput = new CreateAccountInput();
34+
Account newAccount = new Account(bankName, ownerName, createAccountInput.generateSortCode(), createAccountInput.generateAccountNumber(), 0.00);
35+
return accountRepository.save(newAccount);
36+
}
3037
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.example.paul.utils;
2+
3+
import com.mifmif.common.regex.Generex;
4+
5+
import javax.validation.constraints.NotBlank;
6+
import java.util.Objects;
7+
8+
public class CreateAccountInput {
9+
10+
@NotBlank(message = "Bank name is mandatory")
11+
private String bankName;
12+
13+
@NotBlank(message = "Owner name is mandatory")
14+
private String ownerName;
15+
16+
17+
public CreateAccountInput() {}
18+
19+
public String getBankName() {
20+
return bankName;
21+
}
22+
23+
public void setBankName(String bankName) {
24+
this.bankName = bankName;
25+
}
26+
27+
public String getOwnerName() {
28+
return ownerName;
29+
}
30+
31+
public void setOwnerName(String ownerName) {
32+
this.ownerName = ownerName;
33+
}
34+
35+
public String generateSortCode() {
36+
Generex generex = new Generex(InputValidator.sortCodePattern.toString());
37+
return generex.getMatchedString(2);
38+
}
39+
40+
public String generateAccountNumber() {
41+
Generex generex = new Generex(InputValidator.accountNumberPattern.toString());
42+
return generex.getMatchedString(2);
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "CreateAccountInput{" +
48+
"bankName='" + bankName + '\'' +
49+
", ownerName='" + ownerName + '\'' +
50+
'}';
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) return true;
56+
if (o == null || getClass() != o.getClass()) return false;
57+
CreateAccountInput that = (CreateAccountInput) o;
58+
return Objects.equals(bankName, that.bankName) &&
59+
Objects.equals(ownerName, that.ownerName);
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return Objects.hash(bankName, ownerName);
65+
}
66+
}

src/main/java/com/example/paul/utils/InputValidator.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
public class InputValidator {
66

7-
private static final Pattern sortCodePattern = Pattern.compile("^[0-9]{2}-[0-9]{2}-[0-9]{2}$");
7+
static final Pattern sortCodePattern = Pattern.compile("^[0-9]{2}-[0-9]{2}-[0-9]{2}$");
88

9-
private static final Pattern accountNumberPattern = Pattern.compile("^[0-9]{8}$");
9+
static final Pattern accountNumberPattern = Pattern.compile("^[0-9]{8}$");
1010

1111
public static boolean isSearchCriteriaValid(AccountInput accountInput) {
1212
return sortCodePattern.matcher(accountInput.getSortCode()).find() &&
1313
accountNumberPattern.matcher(accountInput.getAccountNumber()).find();
1414
}
1515

16+
public static boolean isCreateAccountCriteriaValid(CreateAccountInput createAccountInput) {
17+
return (!createAccountInput.getBankName().isBlank() && !createAccountInput.getOwnerName().isBlank());
18+
}
19+
1620
public static boolean isSearchTransactionValid(TransactionInput transactionInput) {
1721
// TODO Add checks for large amounts; consider past history of account holder and location of transfers
1822

src/main/resources/application.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ spring:
1212
path: /h2-console
1313
jpa:
1414
hibernate:
15-
ddl-auto: none
15+
ddl-auto: create-drop
1616
server:
1717
port: 8080

0 commit comments

Comments
 (0)