-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added OpenAccountController and some uuid generator utility
- Loading branch information
Showing
6 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...acct/acct.cmd/src/main/java/co/marcuss/acct/cmd/api/controller/OpenAccountController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package co.marcuss.acct.cmd.api.controller; | ||
|
||
import co.marcuss.acct.cmd.api.commands.OpenAccountCommand; | ||
import co.marcuss.acct.cmd.api.dto.OpenAccountResponse; | ||
import co.marcuss.acct.commons.dto.BaseResponse; | ||
import co.marcuss.cqrs.core.infrastructure.CommandDispatcher; | ||
import co.marcuss.cqrs.core.utils.UUIDGenerator; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j(topic = "OpenAccount") | ||
@RestController | ||
@RequestMapping(path = "api/v1/open-bank-account") | ||
public class OpenAccountController { | ||
|
||
@Autowired | ||
private CommandDispatcher commandDispatcher; | ||
|
||
@PostMapping | ||
public ResponseEntity<BaseResponse> openAccount(@RequestBody OpenAccountCommand command) { | ||
var id = UUIDGenerator.generateUIID(command.getAccountType().toString()); | ||
command.setId(id | ||
); | ||
try { | ||
commandDispatcher.send(command); | ||
return new ResponseEntity<>( | ||
new OpenAccountResponse("Account Opened Successfully", id), | ||
HttpStatus.CREATED | ||
); | ||
} | ||
catch (IllegalStateException e) { | ||
log.warn("Bad Request Caused By: {}", e.getMessage()); | ||
return new ResponseEntity<>( | ||
new OpenAccountResponse("Bad Request Caused By: "+ e.getMessage(), id), | ||
HttpStatus.BAD_REQUEST | ||
); | ||
} | ||
catch (Exception e) { | ||
log.error("Error opening an account.", e); | ||
log.error("Error opening an account. id: {}", id); | ||
return new ResponseEntity<>( | ||
new OpenAccountResponse("Server failed to fulfill the request.", null), | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
bank-acct/acct.cmd/src/main/java/co/marcuss/acct/cmd/api/dto/OpenAccountResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package co.marcuss.acct.cmd.api.dto; | ||
|
||
import co.marcuss.acct.commons.dto.BaseResponse; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class OpenAccountResponse extends BaseResponse { | ||
private String id; | ||
|
||
public OpenAccountResponse(String message, String id) { | ||
super(message); | ||
this.id = id; | ||
} | ||
} |
11 changes: 10 additions & 1 deletion
11
bank-acct/acct.commons/src/main/java/co/marcuss/acct/commons/dto/AccountType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package co.marcuss.acct.commons.dto; | ||
|
||
public enum AccountType { | ||
SAVINGS, CHECKING | ||
SAVINGS("SV"), CHECKING("CH"); | ||
private String value; | ||
AccountType(String name) { | ||
this.value = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
bank-acct/acct.commons/src/main/java/co/marcuss/acct/commons/dto/BaseResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package co.marcuss.acct.commons.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class BaseResponse { | ||
|
||
private String message; | ||
} |
26 changes: 26 additions & 0 deletions
26
cqrs-es/cqrs.core/src/main/java/co/marcuss/cqrs/core/utils/PrefixedUIID.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package co.marcuss.cqrs.core.utils; | ||
|
||
import java.util.UUID; | ||
|
||
public final class PrefixedUIID implements java.io.Serializable, Comparable<PrefixedUIID> { | ||
|
||
private UUID id; | ||
private String prefix; | ||
|
||
public PrefixedUIID(String prefix, UUID id) { | ||
this.prefix = prefix; | ||
this.id = id; | ||
} | ||
|
||
public int compareTo(PrefixedUIID o) { | ||
if (this.id.compareTo(o.id) == 0) { | ||
return prefix.compareTo(o.prefix); | ||
} | ||
return this.id.compareTo(o.id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return prefix + id.toString(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
cqrs-es/cqrs.core/src/main/java/co/marcuss/cqrs/core/utils/UUIDGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package co.marcuss.cqrs.core.utils; | ||
|
||
import java.util.UUID; | ||
|
||
public class UUIDGenerator { | ||
|
||
public static String generateUIID(String prefix) { | ||
return new PrefixedUIID(prefix, generateUIID()).toString(); | ||
} | ||
|
||
public static UUID generateUIID() { | ||
return UUID.randomUUID(); | ||
} | ||
} |