Merge branch 'private-event-sourcing-examples-38' into private-event-sourcing-examples-46

* private-event-sourcing-examples-38:
  removed redundant EventHandler method from accounts-commandside-backend
  fixed getDate()
  private-event-sourcing-examples-26 solved comments on previous commit: - changed getting Content-Type header for GatewayController - updated transaction state change logic - renamed "date" field in AccountInfo
This commit is contained in:
Andrew Revinsky (DART)
2016-09-22 21:39:19 +03:00
6 changed files with 48 additions and 25 deletions

View File

@@ -35,12 +35,4 @@ public class AccountWorkflow {
return ctx.update(Account.class, fromAccountId, new CreditAccountCommand(amount, transactionId));
}
@EventHandlerMethod
public CompletableFuture<EntityWithIdAndVersion<Account>> deleteAccount(EventHandlerContext<CustomerToAccountDeleted> ctx) {
CustomerToAccountDeleted event = ctx.getEvent();
String accountId = event.getAccountId();
return ctx.update(Account.class, accountId, new DeleteAccountCommand());
}
}

View File

@@ -1,5 +1,6 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.chrisrichardson.eventstore.javaexamples.banking.common.accounts.AccountChangeInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.accounts.AccountTransactionInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.transactions.TransferState;
@@ -19,7 +20,8 @@ public class AccountInfo {
private List<AccountChangeInfo> changes;
private Map<String, AccountTransactionInfo> transactions;
private String version;
private Date date;
@JsonProperty("date")
private Date creationDate;
private AccountInfo() {
}
@@ -28,7 +30,7 @@ public class AccountInfo {
this(id, customerId, title, description, balance, changes, transactions, version, new Date());
}
public AccountInfo(String id, String customerId, String title, String description, long balance, List<AccountChangeInfo> changes, Map<String, AccountTransactionInfo> transactions, String version, Date date) {
public AccountInfo(String id, String customerId, String title, String description, long balance, List<AccountChangeInfo> changes, Map<String, AccountTransactionInfo> transactions, String version, Date creationDate) {
this.id = id;
this.customerId = customerId;
@@ -38,7 +40,7 @@ public class AccountInfo {
this.changes = changes;
this.transactions = transactions;
this.version = version;
this.date = date;
this.creationDate = creationDate;
}
public String getId() {
@@ -73,7 +75,7 @@ public class AccountInfo {
return version;
}
public Date getDate() {
return date;
public Date getCreationDate() {
return creationDate;
}
}

View File

@@ -40,7 +40,7 @@ public class AccountInfoUpdateService {
.set("description", description)
.set("balance", toIntegerRepr(initialBalance))
.push("changes", ci)
.set("date", new Date())
.set("date", getFromEventId(version))
.set("version", version),
AccountInfo.class);
logger.info("Saved in mongo");
@@ -81,4 +81,12 @@ public class AccountInfoUpdateService {
set("transactions." + transactionId + ".status", status),
AccountInfo.class);
}
private Date getFromEventId(String eventId) {
String[] s = eventId.split("-");
if (s.length != 2) {
return new Date();
}
return new Date(Long.parseUnsignedLong(s[0], 16));
}
}

View File

@@ -15,6 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.Date;
import static net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.MoneyUtil.toIntegerRepr;
@@ -40,6 +41,7 @@ public class AccountQueryWorkflow {
String customerId = event.getCustomerId();
String title = event.getTitle();
String description = event.getDescription();
accountInfoUpdateService.create(id, customerId, title, initialBalance, description, eventId);
}
@@ -74,7 +76,6 @@ public class AccountQueryWorkflow {
String accountId = de.getEntityId();
String transactionId = de.getEvent().getTransactionId();
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.DEBITED);
saveChange(de, -1);
}
@@ -83,16 +84,37 @@ public class AccountQueryWorkflow {
String accountId = de.getEntityId();
String transactionId = de.getEvent().getTransactionId();
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.COMPLETED);
saveChange(de, +1);
}
@EventHandlerMethod
public void recordFailed(DispatchedEvent<AccountDebitFailedDueToInsufficientFundsEvent> de) {
String accountId = de.getEntityId();
String transactionId = de.getEvent().getTransactionId();
public void updateDebitTransactionState(DispatchedEvent<DebitRecordedEvent> de) {
String transactionId = de.getEntityId();
String fromAccountId = de.getEvent().getDetails().getFromAccountId();
String toAccountId = de.getEvent().getDetails().getToAccountId();
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.FAILED_DUE_TO_INSUFFICIENT_FUNDS);
accountInfoUpdateService.updateTransactionStatus(fromAccountId, transactionId, TransferState.DEBITED);
accountInfoUpdateService.updateTransactionStatus(toAccountId, transactionId, TransferState.DEBITED);
}
@EventHandlerMethod
public void updateCreditTransactionState(DispatchedEvent<CreditRecordedEvent> de) {
String transactionId = de.getEntityId();
String fromAccountId = de.getEvent().getDetails().getFromAccountId();
String toAccountId = de.getEvent().getDetails().getToAccountId();
accountInfoUpdateService.updateTransactionStatus(fromAccountId, transactionId, TransferState.COMPLETED);
accountInfoUpdateService.updateTransactionStatus(toAccountId, transactionId, TransferState.COMPLETED);
}
@EventHandlerMethod
public void recordFailed(DispatchedEvent<FailedDebitRecordedEvent> de) {
String transactionId = de.getEntityId();
String fromAccountId = de.getEvent().getDetails().getFromAccountId();
String toAccountId = de.getEvent().getDetails().getToAccountId();
accountInfoUpdateService.updateTransactionStatus(fromAccountId, transactionId, TransferState.FAILED_DUE_TO_INSUFFICIENT_FUNDS);
accountInfoUpdateService.updateTransactionStatus(toAccountId, transactionId, TransferState.FAILED_DUE_TO_INSUFFICIENT_FUNDS);
}
public <T extends AccountChangedEvent> void saveChange(DispatchedEvent<T> de, int delta) {
@@ -107,5 +129,4 @@ public class AccountQueryWorkflow {
accountInfoUpdateService.updateBalance(accountId, changeId, balanceDelta, ci);
}
}

View File

@@ -52,7 +52,7 @@ public class AccountQueryController {
public ResponseEntity<AccountHistoryResponse> getTransactionsHistory(@PathVariable String accountId) {
AccountInfo accountInfo = accountInfoQueryService.findByAccountId(accountId);
List<AccountHistoryEntry> historyEntries = new ArrayList<>();
historyEntries.add(new AccountOpenInfo(accountInfo.getDate(), AccountHistoryEntry.EntryType.account, accountInfo.getChanges().get(0).getAmount()));
historyEntries.add(new AccountOpenInfo(accountInfo.getCreationDate(), AccountHistoryEntry.EntryType.account, accountInfo.getChanges().get(0).getAmount()));
accountInfo.getTransactions().forEach(historyEntries::add);
return ResponseEntity.ok().body(new AccountHistoryResponse(historyEntries));

View File

@@ -59,12 +59,12 @@ public class GatewayController {
logger.info("request: {}", proxiedRequest);
HttpResponse proxiedResponse = httpClient.execute(proxiedRequest);
logger.info("Response {}", proxiedResponse.getStatusLine().getStatusCode());
return new ResponseEntity<>(read(proxiedResponse.getEntity().getContent()), processHeaders(proxiedResponse.getAllHeaders()), HttpStatus.valueOf(proxiedResponse.getStatusLine().getStatusCode()));
return new ResponseEntity<>(read(proxiedResponse.getEntity().getContent()), processHeaders(proxiedResponse.getFirstHeader("Content-Type")), HttpStatus.valueOf(proxiedResponse.getStatusLine().getStatusCode()));
}
private HttpHeaders processHeaders(Header[] headers) {
private HttpHeaders processHeaders(Header h) {
HttpHeaders result = new HttpHeaders();
Stream.of(headers).filter(h -> h.getName().equalsIgnoreCase("Content-Type")).forEach(h -> result.set(h.getName(), h.getValue()));
result.set(h.getName(), h.getValue());
return result;
}