|
3 | 3 | import com.example.paul.models.Account;
|
4 | 4 | import com.example.paul.services.AccountService;
|
5 | 5 | import com.example.paul.utils.AccountInput;
|
| 6 | +import com.example.paul.utils.CreateAccountInput; |
6 | 7 | import com.example.paul.utils.InputValidator;
|
7 | 8 | import org.slf4j.Logger;
|
8 | 9 | import org.slf4j.LoggerFactory;
|
@@ -30,6 +31,9 @@ public class AccountRestController {
|
30 | 31 | private static final String NO_ACCOUNT_FOUND =
|
31 | 32 | "Unable to find an account matching this sort code and account number";
|
32 | 33 |
|
| 34 | + private static final String CREATE_ACCOUNT_FAILED = |
| 35 | + "Error happened during creating new account"; |
| 36 | + |
33 | 37 | private final AccountService accountService;
|
34 | 38 |
|
35 | 39 | @Autowired
|
@@ -62,6 +66,31 @@ public ResponseEntity<?> checkAccountBalance(
|
62 | 66 | }
|
63 | 67 | }
|
64 | 68 |
|
| 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 | + |
65 | 94 | @ResponseStatus(HttpStatus.BAD_REQUEST)
|
66 | 95 | @ExceptionHandler(MethodArgumentNotValidException.class)
|
67 | 96 | public Map<String, String> handleValidationExceptions(
|
|
0 commit comments