added customer-command-side

This commit is contained in:
dartpopikyardo
2016-02-03 23:04:15 +03:00
parent cbee50658e
commit e0691a61a2
13 changed files with 245 additions and 55 deletions

View File

@@ -22,7 +22,7 @@ public class AccountController {
@RequestMapping(method = RequestMethod.POST)
public Observable<CreateAccountResponse> createAccount(@Validated @RequestBody CreateAccountRequest request) {
return accountService.openAccount(request.getInitialBalance())
return accountService.openAccount(request.getCustomerId(), request.getInitialBalance())
.map(entityAndEventInfo -> new CreateAccountResponse(entityAndEventInfo.getEntityIdentifier().getId()));
}
}

View File

@@ -7,6 +7,9 @@ import java.math.BigDecimal;
public class CreateAccountRequest {
@NotNull
private String customerId;
@NotNull
@DecimalMin("0")
private BigDecimal initialBalance;
@@ -14,11 +17,19 @@ public class CreateAccountRequest {
public CreateAccountRequest() {
}
public CreateAccountRequest(BigDecimal initialBalance) {
public CreateAccountRequest(String customerId, BigDecimal initialBalance) {
this.initialBalance = initialBalance;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public BigDecimal getInitialBalance() {
return initialBalance;
}

View File

@@ -7,40 +7,20 @@ import net.chrisrichardson.eventstore.Event;
*/
public class CustomerCreatedEvent implements Event {
private String ssn;
private String phoneNumber;
private Address address;
private CustomerInfo customerInfo;
public CustomerCreatedEvent() {
}
public CustomerCreatedEvent(String ssn, String phoneNumber, Address address) {
this.ssn = ssn;
this.phoneNumber = phoneNumber;
this.address = address;
public CustomerCreatedEvent(CustomerInfo customerInfo) {
this.customerInfo = customerInfo;
}
public String getSsn() {
return ssn;
public CustomerInfo getCustomerInfo() {
return customerInfo;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
public void setCustomerInfo(CustomerInfo customerInfo) {
this.customerInfo = customerInfo;
}
}

View File

@@ -0,0 +1,53 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers;
/**
* Created by popikyardo on 03.02.16.
*/
public class CustomerInfo {
private String email;
private String ssn;
private String phoneNumber;
private Address address;
public CustomerInfo() {
}
public CustomerInfo(String email, String ssn, String phoneNumber, Address address) {
this.email = email;
this.ssn = ssn;
this.phoneNumber = phoneNumber;
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

View File

@@ -1,31 +1,19 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo;
/**
* Created by popikyardo on 02.02.16.
*/
public class CreateCustomerCommand implements CustomerCommand {
private String ssn;
private String phoneNumber;
private Address address;
private CustomerInfo customerInfo;
public CreateCustomerCommand(String socialSecurityNum, String phoneNumber, Address address) {
this.ssn = ssn;
this.phoneNumber = phoneNumber;
this.address = address;
public CreateCustomerCommand(CustomerInfo customerInfo) {
this.customerInfo = customerInfo;
}
public String getSsn() {
return ssn;
}
public String getPhoneNumber() {
return phoneNumber;
}
public Address getAddress() {
return address;
public CustomerInfo getCustomerInfo() {
return customerInfo;
}
}

View File

@@ -3,8 +3,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.
import net.chrisrichardson.eventstore.Event;
import net.chrisrichardson.eventstore.EventUtil;
import net.chrisrichardson.eventstore.ReflectiveMutableCommandProcessingAggregate;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo;
import java.util.List;
@@ -12,18 +12,15 @@ import java.util.List;
* Created by popikyardo on 02.02.16.
*/
public class Customer extends ReflectiveMutableCommandProcessingAggregate<Customer, CustomerCommand> {
private String ssn;
private String phoneNumber;
private Address address;
private CustomerInfo customerInfo;
public List<Event> process(CreateCustomerCommand cmd) {
return EventUtil.events(new CustomerCreatedEvent(cmd.getSsn(), cmd.getPhoneNumber(), cmd.getAddress()));
return EventUtil.events(new CustomerCreatedEvent(cmd.getCustomerInfo()));
}
public void apply(CustomerCreatedEvent event) {
ssn = event.getSsn();
phoneNumber = event.getPhoneNumber();
address = event.getAddress();
customerInfo = event.getCustomerInfo();
}
}

View File

@@ -0,0 +1,25 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers;
import net.chrisrichardson.eventstore.EventStore;
import net.chrisrichardson.eventstore.javaapi.consumer.EnableJavaEventHandlers;
import net.chrisrichardson.eventstore.repository.AggregateRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableJavaEventHandlers
public class CustomerConfiguration {
@Bean
public CustomerService customerService(AggregateRepository<Customer, CustomerCommand> customerRepository) {
return new CustomerService(customerRepository);
}
@Bean
public AggregateRepository<Customer, CustomerCommand> customerRepository(EventStore eventStore) {
return new AggregateRepository<Customer, CustomerCommand>(Customer.class, eventStore);
}
}

View File

@@ -0,0 +1,21 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers;
import net.chrisrichardson.eventstore.EntityWithIdAndVersion;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo;
import net.chrisrichardson.eventstore.repository.AggregateRepository;
public class CustomerService {
private final AggregateRepository<Customer, CustomerCommand> accountRepository;
public CustomerService(AggregateRepository<Customer, CustomerCommand> accountRepository) {
this.accountRepository = accountRepository;
}
public rx.Observable<EntityWithIdAndVersion<Customer>> createCustomer(CustomerInfo customerInfo) {
return accountRepository.save(new CreateCustomerCommand(customerInfo));
}
}

View File

@@ -0,0 +1,11 @@
apply plugin: 'java'
dependencies {
compile project(":customer-command-side-backend")
compile project(":common-web")
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
testCompile "net.chrisrichardson.eventstore.client:eventstore-jdbc_2.10:$eventStoreClientVersion"
}

View File

@@ -0,0 +1,35 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by popikyardo on 03.02.16.
*/
@Configuration
@Import({CustomerConfiguration.class})
@ComponentScan
public class CommandSideWebCustomersConfiguration extends WebMvcConfigurerAdapter {
class FakeThing {}
@Bean
public FakeThing init(RequestMappingHandlerAdapter adapter) {
// https://jira.spring.io/browse/SPR-13083
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(adapter.getReturnValueHandlers());
handlers.add(0, new ObservableReturnValueHandler());
adapter.setReturnValueHandlers(handlers);
return new FakeThing();
}
}

View File

@@ -0,0 +1,32 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerService;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import rx.Observable;
/**
* Created by popikyardo on 03.02.16.
*/
@RestController
@RequestMapping("/accounts")
public class CustomerController {
private CustomerService customerService;
@Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@RequestMapping(method = RequestMethod.POST)
public Observable<CustomerResponse> createAccount(@Validated @RequestBody CustomerInfo request) {
return customerService.createCustomer(request)
.map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request));
}
}

View File

@@ -0,0 +1,36 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo;
/**
* Created by popikyardo on 03.02.16.
*/
public class CustomerResponse {
private String id;
private CustomerInfo customerInfo;
public CustomerResponse() {
}
public CustomerResponse(String id, CustomerInfo customerInfo) {
this.id = id;
this.customerInfo = customerInfo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public CustomerInfo getCustomerInfo() {
return customerInfo;
}
public void setCustomerInfo(CustomerInfo customerInfo) {
this.customerInfo = customerInfo;
}
}

View File

@@ -26,4 +26,5 @@ include 'e2e-test'
rootProject.name = 'java-spring-event-sourcing-example'
include 'common-auth'
include 'customer-command-side-web'