- updated swagger description
- added /customers/{id}/toaccounts endpoint
This commit is contained in:
@@ -3,6 +3,13 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.cu
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.where;
|
||||
|
||||
/**
|
||||
* Created by Main on 04.02.2016.
|
||||
@@ -11,19 +18,25 @@ public class CustomerInfoUpdateService {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private CustomerInfoRepository accountInfoRepository;
|
||||
private QuerySideCustomerRepository accountInfoRepository;
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
public CustomerInfoUpdateService(CustomerInfoRepository accountInfoRepository) {
|
||||
public CustomerInfoUpdateService(QuerySideCustomerRepository accountInfoRepository, MongoTemplate mongoTemplate) {
|
||||
this.accountInfoRepository = accountInfoRepository;
|
||||
this.mongoTemplate = mongoTemplate;
|
||||
}
|
||||
|
||||
public void create(String id, CustomerInfo customerInfo) {
|
||||
try {
|
||||
accountInfoRepository.save(new CustomerInfoWithId(id,
|
||||
customerInfo.getEmail(),
|
||||
customerInfo.getSsn(),
|
||||
customerInfo.getPhoneNumber(),
|
||||
customerInfo.getAddress())
|
||||
accountInfoRepository.save(new QuerySideCustomer(id,
|
||||
"",
|
||||
"",
|
||||
customerInfo.getEmail(),
|
||||
customerInfo.getSsn(),
|
||||
customerInfo.getPhoneNumber(),
|
||||
customerInfo.getAddress(),
|
||||
Collections.<ToAccountInfo>emptyList()
|
||||
)
|
||||
);
|
||||
logger.info("Saved in mongo");
|
||||
} catch (Throwable t) {
|
||||
@@ -32,4 +45,11 @@ public class CustomerInfoUpdateService {
|
||||
}
|
||||
}
|
||||
|
||||
public void addToAccount(String id, ToAccountInfo accountInfo) {
|
||||
mongoTemplate.updateMulti(new Query(where("id").is(id)),
|
||||
new Update().
|
||||
push("toAccounts", accountInfo),
|
||||
QuerySideCustomer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
|
||||
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo;
|
||||
|
||||
/**
|
||||
* Created by Main on 05.02.2016.
|
||||
*/
|
||||
public class CustomerInfoWithId extends CustomerInfo {
|
||||
private String id;
|
||||
|
||||
public CustomerInfoWithId(String id, String email, String ssn, String phoneNumber, Address address) {
|
||||
super(email, ssn, phoneNumber, address);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -7,22 +7,22 @@ import java.util.List;
|
||||
|
||||
public class CustomerQueryService {
|
||||
|
||||
private CustomerInfoRepository customerInfoRepository;
|
||||
private QuerySideCustomerRepository querySideCustomerRepository;
|
||||
|
||||
public CustomerQueryService(CustomerInfoRepository customerInfoRepository) {
|
||||
this.customerInfoRepository = customerInfoRepository;
|
||||
public CustomerQueryService(QuerySideCustomerRepository querySideCustomerRepository) {
|
||||
this.querySideCustomerRepository = querySideCustomerRepository;
|
||||
}
|
||||
|
||||
public Observable<CustomerInfoWithId> findByCustomerId(EntityIdentifier customerId) {
|
||||
CustomerInfoWithId customer = customerInfoRepository.findOne(customerId.getId());
|
||||
public Observable<QuerySideCustomer> findByCustomerId(EntityIdentifier customerId) {
|
||||
QuerySideCustomer customer = querySideCustomerRepository.findOne(customerId.getId());
|
||||
if (customer == null)
|
||||
return Observable.error(new CustomerNotFoundException(customerId.getId()));
|
||||
else
|
||||
return Observable.just(customer);
|
||||
}
|
||||
|
||||
public Observable<List<CustomerInfoWithId>> findByEmail(String email){
|
||||
List<CustomerInfoWithId> customers = customerInfoRepository.findByEmailLike(email);
|
||||
public Observable<List<QuerySideCustomer>> findByEmail(String email){
|
||||
List<QuerySideCustomer> customers = querySideCustomerRepository.findByEmailLike(email);
|
||||
if (customers.isEmpty())
|
||||
return Observable.error(new CustomersNotFoundException());
|
||||
else
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
|
||||
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerAddedToAccount;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent;
|
||||
import net.chrisrichardson.eventstore.subscriptions.CompoundEventHandler;
|
||||
import net.chrisrichardson.eventstore.subscriptions.DispatchedEvent;
|
||||
@@ -28,10 +29,21 @@ public class CustomerQueryWorkflow implements CompoundEventHandler {
|
||||
public Observable<Object> create(DispatchedEvent<CustomerCreatedEvent> de) {
|
||||
CustomerCreatedEvent event = de.event();
|
||||
String id = de.getEntityIdentifier().getId();
|
||||
String eventId = de.eventId().asString();
|
||||
logger.info("**************** customer version=" + id + ", " + eventId);
|
||||
|
||||
customerInfoUpdateService.create(id, event.getCustomerInfo());
|
||||
return Observable.just(null);
|
||||
}
|
||||
@EventHandlerMethod
|
||||
public Observable<Object> addToAccount(DispatchedEvent<CustomerAddedToAccount> de) {
|
||||
CustomerAddedToAccount event = de.event();
|
||||
String id = de.getEntityIdentifier().getId();
|
||||
|
||||
ToAccountInfo toAccountInfo = new ToAccountInfo(event.getAccountId(),
|
||||
event.getTitle(),
|
||||
event.getAccountOwner());
|
||||
|
||||
customerInfoUpdateService.addToAccount(id, toAccountInfo);
|
||||
return Observable.just(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
|
||||
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Main on 05.02.2016.
|
||||
*/
|
||||
public class QuerySideCustomer extends CustomerInfo {
|
||||
private String id;
|
||||
protected String firstName;
|
||||
protected String lastName;
|
||||
private List<ToAccountInfo> toAccounts;
|
||||
|
||||
public QuerySideCustomer(String id, String firstName, String lastName, String email, String ssn, String phoneNumber, Address address, List<ToAccountInfo> toAccounts) {
|
||||
super(email, ssn, phoneNumber, address);
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.toAccounts = toAccounts;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public List<ToAccountInfo> getToAccounts() {
|
||||
return toAccounts;
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,12 @@ public class QuerySideCustomerConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CustomerInfoUpdateService customerInfoUpdateService(CustomerInfoRepository customerInfoRepository) {
|
||||
return new CustomerInfoUpdateService(customerInfoRepository);
|
||||
public CustomerInfoUpdateService customerInfoUpdateService(QuerySideCustomerRepository querySideCustomerRepository) {
|
||||
return new CustomerInfoUpdateService(querySideCustomerRepository);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CustomerQueryService customerQueryService(CustomerInfoRepository accountInfoRepository) {
|
||||
public CustomerQueryService customerQueryService(QuerySideCustomerRepository accountInfoRepository) {
|
||||
return new CustomerQueryService(accountInfoRepository);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
interface CustomerInfoRepository extends MongoRepository<CustomerInfoWithId, String> {
|
||||
interface QuerySideCustomerRepository extends MongoRepository<QuerySideCustomer, String> {
|
||||
|
||||
List<CustomerInfoWithId> findByEmailLike(String email);
|
||||
List<QuerySideCustomer> findByEmailLike(String email);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Main on 08.02.2016.
|
||||
*/
|
||||
public class ToAccountInfo {
|
||||
private String id;
|
||||
private String title;
|
||||
private String owner;
|
||||
|
||||
public ToAccountInfo(String id, String title, String owner) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user