Skip to content

Commit

Permalink
Added OpenAccountController and some uuid generator utility
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuss committed Aug 16, 2022
1 parent 354b190 commit 3a595e5
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 1 deletion.
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
);
}
}
}
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;
}
}
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;
}
}
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;
}
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();
}
}
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();
}
}

0 comments on commit 3a595e5

Please sign in to comment.