diff --git a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java index 0e8294b..0236b2a 100644 --- a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java +++ b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java @@ -22,7 +22,7 @@ public class AccountController { @RequestMapping(method = RequestMethod.POST) public Observable createAccount(@Validated @RequestBody CreateAccountRequest request) { - return accountService.openAccount(request.getInitialBalance()) + return accountService.openAccount(request.getCustomerId(), request.getInitialBalance()) .map(entityAndEventInfo -> new CreateAccountResponse(entityAndEventInfo.getEntityIdentifier().getId())); } } diff --git a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java index 923b748..2e43d05 100644 --- a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java +++ b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java @@ -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; } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java index 20e28fc..1ec87c4 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -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; } } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java new file mode 100644 index 0000000..6622ebd --- /dev/null +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java @@ -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; + } +} diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java index 9ac8c0e..97fb7df 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java @@ -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; } } diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java index a50b1e7..ff3a0a8 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -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 { - private String ssn; - private String phoneNumber; - private Address address; + + private CustomerInfo customerInfo; public List 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(); } } diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java new file mode 100644 index 0000000..c8336bb --- /dev/null +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java @@ -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 customerRepository) { + return new CustomerService(customerRepository); + } + + @Bean + public AggregateRepository customerRepository(EventStore eventStore) { + return new AggregateRepository(Customer.class, eventStore); + } + +} + + diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java new file mode 100644 index 0000000..bbc2cb5 --- /dev/null +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java @@ -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 accountRepository; + + public CustomerService(AggregateRepository accountRepository) { + this.accountRepository = accountRepository; + } + + public rx.Observable> createCustomer(CustomerInfo customerInfo) { + return accountRepository.save(new CreateCustomerCommand(customerInfo)); + } + +} diff --git a/java-spring/customer-command-side-web/build.gradle b/java-spring/customer-command-side-web/build.gradle new file mode 100644 index 0000000..8809165 --- /dev/null +++ b/java-spring/customer-command-side-web/build.gradle @@ -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" +} diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java new file mode 100644 index 0000000..e86255c --- /dev/null +++ b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java @@ -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 handlers = new ArrayList(adapter.getReturnValueHandlers()); + handlers.add(0, new ObservableReturnValueHandler()); + adapter.setReturnValueHandlers(handlers); + return new FakeThing(); + } + +} \ No newline at end of file diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java new file mode 100644 index 0000000..0ce9ca4 --- /dev/null +++ b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java @@ -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 createAccount(@Validated @RequestBody CustomerInfo request) { + return customerService.createCustomer(request) + .map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request)); + } +} diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java new file mode 100644 index 0000000..e81c302 --- /dev/null +++ b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java @@ -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; + } +} diff --git a/java-spring/settings.gradle b/java-spring/settings.gradle index a6b3b3a..a00f633 100644 --- a/java-spring/settings.gradle +++ b/java-spring/settings.gradle @@ -26,4 +26,5 @@ include 'e2e-test' rootProject.name = 'java-spring-event-sourcing-example' include 'common-auth' +include 'customer-command-side-web'