reverted back AccountInfo and AccountInfoUpdateService changes

This commit is contained in:
dartpopikyardo
2016-09-02 11:53:50 +03:00
parent 47e9053285
commit dde554e442
4 changed files with 57 additions and 28 deletions

View File

@@ -16,18 +16,18 @@ public class AccountInfo {
private String description; private String description;
private long balance; private long balance;
private List<AccountChangeInfo> changes; private List<AccountChangeInfo> changes;
private List<AccountTransactionInfo> transactions; private Map<String, AccountTransactionInfo> transactions;
private String version; private String version;
private Date date; private Date date;
private AccountInfo() { private AccountInfo() {
} }
public AccountInfo(String id, String customerId, String title, String description, long balance, List<AccountChangeInfo> changes, List<AccountTransactionInfo> transactions, String version) { public AccountInfo(String id, String customerId, String title, String description, long balance, List<AccountChangeInfo> changes, Map<String, AccountTransactionInfo> transactions, String version) {
this(id, customerId, title, description, balance, changes, transactions, version, new Date()); 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, List<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 date) {
this.id = id; this.id = id;
this.customerId = customerId; this.customerId = customerId;
@@ -61,11 +61,11 @@ public class AccountInfo {
} }
public List<AccountChangeInfo> getChanges() { public List<AccountChangeInfo> getChanges() {
return changes; return changes == null ? Collections.EMPTY_LIST : changes;
} }
public List<AccountTransactionInfo> getTransactions() { public List<AccountTransactionInfo> getTransactions() {
return transactions; return transactions == null ? Collections.EMPTY_LIST : new ArrayList<>(transactions.values());
} }
public String getVersion() { public String getVersion() {

View File

@@ -31,32 +31,29 @@ public class AccountInfoUpdateService {
public void create(String accountId, String customerId, String title, BigDecimal initialBalance, String description, String version) { public void create(String accountId, String customerId, String title, BigDecimal initialBalance, String description, String version) {
try { try {
accountInfoRepository.save(new AccountInfo( WriteResult x = mongoTemplate.upsert(new Query(where("id").is(accountId).and("version").exists(false)),
accountId, new Update()
customerId, .set("customerId", customerId)
title, .set("title", title)
description, .set("description", description)
toIntegerRepr(initialBalance), .set("balance", toIntegerRepr(initialBalance))
Collections.<AccountChangeInfo>emptyList(), .set("version", version),
Collections.<AccountTransactionInfo>emptyList(), AccountInfo.class);
version));
logger.info("Saved in mongo"); logger.info("Saved in mongo");
} catch (DuplicateKeyException t) { } catch (DuplicateKeyException t) {
logger.warn("When saving ", t); logger.warn("When saving ", t);
} catch (Throwable t) { } catch (Throwable t) {
logger.error("Error during saving: ");
logger.error("Error during saving: ", t); logger.error("Error during saving: ", t);
throw new RuntimeException(t); throw new RuntimeException(t);
} }
} }
public void addTransaction(String eventId, String fromAccountId, AccountTransactionInfo ti) { public void addTransaction(String eventId, String accountId, AccountTransactionInfo ti) {
mongoTemplate.updateMulti(new Query(where("id").is(fromAccountId)), /* wrong .and("version").lt(eventId) */ mongoTemplate.upsert(new Query(where("id").is(accountId)),
new Update(). new Update().
push("transactions", ti). set("transactions." + eventId, ti),
set("version", eventId),
AccountInfo.class); AccountInfo.class);
} }
@@ -71,12 +68,10 @@ public class AccountInfoUpdateService {
} }
public void updateTransactionStatus(String accountId, String transactionId, TransferState status) { public void updateTransactionStatus(String accountId, String transactionId, TransferState status) {
AccountInfo account = accountInfoRepository.findOne(accountId); mongoTemplate.upsert(new Query(where("id").is(accountId)),
if (account != null) { new Update().
account.getTransactions().stream().filter(ati -> ati.getTransactionId().equals(transactionId)).forEach(ati -> ati.setStatus(status)); set("transactions." + transactionId +".status", status),
accountInfoRepository.save(account); AccountInfo.class);
}
} }
} }

View File

@@ -6,6 +6,7 @@ import io.eventuate.javaclient.spring.jdbc.IdGeneratorImpl;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountCreditedEvent; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountCreditedEvent;
import net.chrisrichardson.eventstore.javaexamples.banking.common.accounts.AccountChangeInfo; 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.accounts.AccountTransactionInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.transactions.TransferState;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -15,11 +16,9 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -108,7 +107,42 @@ public class AccountInfoUpdateServiceTest {
accountInfoUpdateService.create(accountId, customerId, title, initialBalance, description, version); accountInfoUpdateService.create(accountId, customerId, title, initialBalance, description, version);
accountInfoUpdateService.create(accountId, customerId, title, initialBalance, description, version); accountInfoUpdateService.create(accountId, customerId, title, initialBalance, description, version);
}
@Test
public void shouldUpdateTransactionStatus() {
IdGenerator x = new IdGeneratorImpl();
String accountId = x.genId().asString();
String customerId = x.genId().asString();
String version = x.genId().asString();
String title = "Checking account";
BigDecimal initialBalance = new BigDecimal("1345");
String description = "Some account";
accountInfoUpdateService.create(accountId, customerId, title, initialBalance, description, version);
String transactionId = x.genId().asString();
AccountTransactionInfo transactionInfo = new AccountTransactionInfo();
transactionInfo.setStatus(TransferState.INITIAL);
accountInfoUpdateService.addTransaction(transactionId, accountId, transactionInfo);
AccountInfo accountInfo = accountQueryService.findByAccountId(accountId);
assertEquals(accountId, accountInfo.getId());
assertFalse(accountInfo.getTransactions().isEmpty());
assertEquals(1, accountInfo.getTransactions().size());
assertEquals(TransferState.INITIAL, accountInfo.getTransactions().get(0).getStatus());
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.COMPLETED);
accountInfo = accountQueryService.findByAccountId(accountId);
assertEquals(accountId, accountInfo.getId());
assertFalse(accountInfo.getTransactions().isEmpty());
assertEquals(1, accountInfo.getTransactions().size());
assertEquals(TransferState.COMPLETED, accountInfo.getTransactions().get(0).getStatus());
} }
} }

View File

@@ -111,7 +111,7 @@ public abstract class AbstractRestAPITest {
new Producer<GetAccountsResponse>() { new Producer<GetAccountsResponse>() {
@Override @Override
public CompletableFuture<GetAccountsResponse> produce() { public CompletableFuture<GetAccountsResponse> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customer/"+customerId+"/accounts"), return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/"+customerId+"/accounts"),
GetAccountsResponse.class)); GetAccountsResponse.class));
} }
}, },