Refactored the codebase.
This commit is contained in:
parent
bae59eade9
commit
dbbcfc2118
@ -3,7 +3,7 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.sapient.learning</groupId>
|
<groupId>com.sapient.learning</groupId>
|
||||||
<artifactId>java-events</artifactId>
|
<artifactId>java-es-cqrs</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>java-es-cqrs</name>
|
<name>java-es-cqrs</name>
|
||||||
<properties>
|
<properties>
|
||||||
@ -16,5 +16,12 @@
|
|||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.12</version>
|
<version>1.18.12</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -3,6 +3,7 @@ package com.baeldung.patterns.cqrs.projectors;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import com.baeldung.patterns.cqrs.repository.UserReadRepository;
|
import com.baeldung.patterns.cqrs.repository.UserReadRepository;
|
||||||
@ -21,36 +22,28 @@ public class UserProjector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void project(User user) {
|
public void project(User user) {
|
||||||
UserContact userContact = readRepository.getUserContact(user.getUserid());
|
UserContact userContact = Optional.ofNullable(readRepository.getUserContact(user.getUserid()))
|
||||||
if (userContact == null)
|
.orElse(new UserContact());
|
||||||
userContact = new UserContact();
|
|
||||||
userContact.setContacts(user.getContacts());
|
|
||||||
Map<String, Set<Contact>> contactByType = new HashMap<>();
|
Map<String, Set<Contact>> contactByType = new HashMap<>();
|
||||||
for (Contact contact : user.getContacts()) {
|
for (Contact contact : user.getContacts()) {
|
||||||
Set<Contact> contacts = contactByType.get(contact.getType());
|
Set<Contact> contacts = Optional.ofNullable(contactByType.get(contact.getType()))
|
||||||
if (contacts == null)
|
.orElse(new HashSet<>());
|
||||||
contacts = new HashSet<>();
|
|
||||||
contacts.add(contact);
|
contacts.add(contact);
|
||||||
contactByType.put(contact.getType(), contacts);
|
contactByType.put(contact.getType(), contacts);
|
||||||
}
|
}
|
||||||
userContact.setContactByType(contactByType);
|
userContact.setContactByType(contactByType);
|
||||||
readRepository.addUserContact(user.getUserid(), userContact);
|
readRepository.addUserContact(user.getUserid(), userContact);
|
||||||
|
|
||||||
UserAddress userAddress = readRepository.getUserAddress(user.getUserid());
|
UserAddress userAddress = Optional.ofNullable(readRepository.getUserAddress(user.getUserid()))
|
||||||
if (userAddress == null)
|
.orElse(new UserAddress());
|
||||||
userAddress = new UserAddress();
|
|
||||||
userAddress.setAddresses(user.getAddresses());
|
|
||||||
Map<String, Set<Address>> addressByRegion = new HashMap<>();
|
Map<String, Set<Address>> addressByRegion = new HashMap<>();
|
||||||
for (Address address : user.getAddresses()) {
|
for (Address address : user.getAddresses()) {
|
||||||
Set<Address> addresses = addressByRegion.get(address.getState());
|
Set<Address> addresses = Optional.ofNullable(addressByRegion.get(address.getState()))
|
||||||
if (addresses == null)
|
.orElse(new HashSet<>());
|
||||||
addresses = new HashSet<>();
|
|
||||||
addresses.add(address);
|
addresses.add(address);
|
||||||
addressByRegion.put(address.getState(), addresses);
|
addressByRegion.put(address.getState(), addresses);
|
||||||
}
|
}
|
||||||
userAddress.setAddressByRegion(addressByRegion);
|
userAddress.setAddressByRegion(addressByRegion);
|
||||||
readRepository.addUserAddress(user.getUserid(), userAddress);
|
readRepository.addUserAddress(user.getUserid(), userAddress);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
package com.baeldung.patterns.crud;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import com.baeldung.patterns.crud.repository.UserRepository;
|
|
||||||
import com.baeldung.patterns.crud.service.UserService;
|
|
||||||
import com.baeldung.patterns.domain.Address;
|
|
||||||
import com.baeldung.patterns.domain.Contact;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
UserRepository repository = new UserRepository();
|
|
||||||
UserService service = new UserService(repository);
|
|
||||||
String userId = UUID.randomUUID()
|
|
||||||
.toString();
|
|
||||||
|
|
||||||
service.createUser(userId, "Tom", "Sawyer");
|
|
||||||
service.updateUser(userId,
|
|
||||||
Stream.of(
|
|
||||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
|
||||||
new Contact("EMAIL", "tom.sawyer@rediff.com"),
|
|
||||||
new Contact("PHONE", "700-000-0001"))
|
|
||||||
.collect(Collectors.toSet()),
|
|
||||||
Stream.of(
|
|
||||||
new Address("New York", "NY", "10001"),
|
|
||||||
new Address("Los Angeles", "CA", "90001"),
|
|
||||||
new Address("Housten", "TX", "77001"))
|
|
||||||
.collect(Collectors.toSet()));
|
|
||||||
service.updateUser(userId,
|
|
||||||
Stream.of(
|
|
||||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
|
||||||
new Contact("PHONE", "700-000-0001"))
|
|
||||||
.collect(Collectors.toSet()),
|
|
||||||
Stream.of(
|
|
||||||
new Address("New York", "NY", "10001"),
|
|
||||||
new Address("Housten", "TX", "77001"))
|
|
||||||
.collect(Collectors.toSet()));
|
|
||||||
|
|
||||||
System.out.println(service.getContactByType(userId, "EMAIL"));
|
|
||||||
System.out.println(service.getAddressByRegion(userId, "NY"));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +1,6 @@
|
|||||||
package com.baeldung.patterns.domain;
|
package com.baeldung.patterns.domain;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -10,7 +9,6 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class UserAddress {
|
public class UserAddress {
|
||||||
|
|
||||||
private Set<Address> addresses = new HashSet<>();
|
|
||||||
private Map<String, Set<Address>> addressByRegion = new HashMap<>();
|
private Map<String, Set<Address>> addressByRegion = new HashMap<>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.baeldung.patterns.domain;
|
package com.baeldung.patterns.domain;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -10,7 +9,6 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class UserContact {
|
public class UserContact {
|
||||||
|
|
||||||
private Set<Contact> contacts = new HashSet<>();
|
|
||||||
private Map<String, Set<Contact>> contactByType = new HashMap<>();
|
private Map<String, Set<Contact>> contactByType = new HashMap<>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
package com.baeldung.patterns.es;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import com.baeldung.patterns.domain.Address;
|
|
||||||
import com.baeldung.patterns.domain.Contact;
|
|
||||||
import com.baeldung.patterns.es.repository.EventStore;
|
|
||||||
import com.baeldung.patterns.es.service.UserService;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
EventStore repository = new EventStore();
|
|
||||||
UserService service = new UserService(repository);
|
|
||||||
String userId = UUID.randomUUID()
|
|
||||||
.toString();
|
|
||||||
|
|
||||||
service.createUser(userId, "Tom", "Sawyer");
|
|
||||||
service.updateUser(userId,
|
|
||||||
Stream.of(
|
|
||||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
|
||||||
new Contact("EMAIL", "tom.sawyer@rediff.com"),
|
|
||||||
new Contact("PHONE", "700-000-0001"))
|
|
||||||
.collect(Collectors.toSet()),
|
|
||||||
Stream.of(
|
|
||||||
new Address("New York", "NY", "10001"),
|
|
||||||
new Address("Los Angeles", "CA", "90001"),
|
|
||||||
new Address("Housten", "TX", "77001"))
|
|
||||||
.collect(Collectors.toSet()));
|
|
||||||
service.updateUser(userId,
|
|
||||||
Stream.of(
|
|
||||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
|
||||||
new Contact("PHONE", "700-000-0001"))
|
|
||||||
.collect(Collectors.toSet()),
|
|
||||||
Stream.of(
|
|
||||||
new Address("New York", "NY", "10001"),
|
|
||||||
new Address("Housten", "TX", "77001"))
|
|
||||||
.collect(Collectors.toSet()));
|
|
||||||
|
|
||||||
System.out.println(service.getContactByType(userId, "EMAIL"));
|
|
||||||
System.out.println(service.getAddressByRegion(userId, "NY"));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.patterns.es.events;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@EqualsAndHashCode(callSuper = false)
|
|
||||||
public class UserRemovedEvent extends Event {
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.patterns.es.service;
|
package com.baeldung.patterns.es.service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -23,8 +22,7 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void createUser(String userId, String firstName, String lastName) {
|
public void createUser(String userId, String firstName, String lastName) {
|
||||||
UserCreatedEvent event = new UserCreatedEvent(userId, firstName, lastName);
|
repository.addEvent(userId, new UserCreatedEvent(userId, firstName, lastName));
|
||||||
repository.addEvent(userId, event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateUser(String userId, Set<Contact> contacts, Set<Address> addresses) throws Exception {
|
public void updateUser(String userId, Set<Contact> contacts, Set<Address> addresses) throws Exception {
|
||||||
@ -32,49 +30,30 @@ public class UserService {
|
|||||||
if (user == null)
|
if (user == null)
|
||||||
throw new Exception("User does not exist.");
|
throw new Exception("User does not exist.");
|
||||||
|
|
||||||
List<Contact> contactsToRemove = user.getContacts()
|
user.getContacts()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(c -> !contacts.contains(c))
|
.filter(c -> !contacts.contains(c))
|
||||||
.collect(Collectors.toList());
|
.forEach(c -> repository.addEvent(userId, new UserContactRemovedEvent(c.getType(), c.getDetail())));
|
||||||
for (Contact contact : contactsToRemove) {
|
contacts.stream()
|
||||||
UserContactRemovedEvent contactRemovedEvent = new UserContactRemovedEvent(contact.getType(), contact.getDetail());
|
|
||||||
repository.addEvent(userId, contactRemovedEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Contact> contactsToAdd = contacts.stream()
|
|
||||||
.filter(c -> !user.getContacts()
|
.filter(c -> !user.getContacts()
|
||||||
.contains(c))
|
.contains(c))
|
||||||
.collect(Collectors.toList());
|
.forEach(c -> repository.addEvent(userId, new UserContactAddedEvent(c.getType(), c.getDetail())));
|
||||||
for (Contact contact : contactsToAdd) {
|
user.getAddresses()
|
||||||
UserContactAddedEvent contactAddedEvent = new UserContactAddedEvent(contact.getType(), contact.getDetail());
|
|
||||||
repository.addEvent(userId, contactAddedEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Address> addressesToRemove = user.getAddresses()
|
|
||||||
.stream()
|
.stream()
|
||||||
.filter(a -> !addresses.contains(a))
|
.filter(a -> !addresses.contains(a))
|
||||||
.collect(Collectors.toList());
|
.forEach(a -> repository.addEvent(userId, new UserAddressRemovedEvent(a.getCity(), a.getState(), a.getPostcode())));
|
||||||
for (Address address : addressesToRemove) {
|
addresses.stream()
|
||||||
UserAddressRemovedEvent addressRemovedEvent = new UserAddressRemovedEvent(address.getCity(), address.getState(), address.getPostcode());
|
|
||||||
repository.addEvent(userId, addressRemovedEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Address> addressesToAdd = addresses.stream()
|
|
||||||
.filter(a -> !user.getAddresses()
|
.filter(a -> !user.getAddresses()
|
||||||
.contains(a))
|
.contains(a))
|
||||||
.collect(Collectors.toList());
|
.forEach(a -> repository.addEvent(userId, new UserAddressAddedEvent(a.getCity(), a.getState(), a.getPostcode())));
|
||||||
for (Address address : addressesToAdd) {
|
|
||||||
UserAddressAddedEvent addressAddedEvent = new UserAddressAddedEvent(address.getCity(), address.getState(), address.getPostcode());
|
|
||||||
repository.addEvent(userId, addressAddedEvent);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Contact> getContactByType(String userId, String contactType) throws Exception {
|
public Set<Contact> getContactByType(String userId, String contactType) throws Exception {
|
||||||
User user = UserUtility.recreateUserState(repository, userId);
|
User user = UserUtility.recreateUserState(repository, userId);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
throw new Exception("User does not exist.");
|
throw new Exception("User does not exist.");
|
||||||
Set<Contact> contacts = user.getContacts();
|
return user.getContacts()
|
||||||
return contacts.stream()
|
.stream()
|
||||||
.filter(c -> c.getType()
|
.filter(c -> c.getType()
|
||||||
.equals(contactType))
|
.equals(contactType))
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
@ -84,11 +63,10 @@ public class UserService {
|
|||||||
User user = UserUtility.recreateUserState(repository, userId);
|
User user = UserUtility.recreateUserState(repository, userId);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
throw new Exception("User does not exist.");
|
throw new Exception("User does not exist.");
|
||||||
Set<Address> addresses = user.getAddresses();
|
return user.getAddresses()
|
||||||
return addresses.stream()
|
.stream()
|
||||||
.filter(a -> a.getState()
|
.filter(a -> a.getState()
|
||||||
.equals(state))
|
.equals(state))
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ import com.baeldung.patterns.es.events.UserAddressRemovedEvent;
|
|||||||
import com.baeldung.patterns.es.events.UserContactAddedEvent;
|
import com.baeldung.patterns.es.events.UserContactAddedEvent;
|
||||||
import com.baeldung.patterns.es.events.UserContactRemovedEvent;
|
import com.baeldung.patterns.es.events.UserContactRemovedEvent;
|
||||||
import com.baeldung.patterns.es.events.UserCreatedEvent;
|
import com.baeldung.patterns.es.events.UserCreatedEvent;
|
||||||
import com.baeldung.patterns.es.events.UserRemovedEvent;
|
|
||||||
import com.baeldung.patterns.es.repository.EventStore;
|
import com.baeldung.patterns.es.repository.EventStore;
|
||||||
|
|
||||||
public class UserUtility {
|
public class UserUtility {
|
||||||
@ -27,9 +26,6 @@ public class UserUtility {
|
|||||||
user = new User(UUID.randomUUID()
|
user = new User(UUID.randomUUID()
|
||||||
.toString(), e.getFirstName(), e.getLastName());
|
.toString(), e.getFirstName(), e.getLastName());
|
||||||
}
|
}
|
||||||
if (event instanceof UserRemovedEvent) {
|
|
||||||
user = null;
|
|
||||||
}
|
|
||||||
if (event instanceof UserAddressAddedEvent) {
|
if (event instanceof UserAddressAddedEvent) {
|
||||||
UserAddressAddedEvent e = (UserAddressAddedEvent) event;
|
UserAddressAddedEvent e = (UserAddressAddedEvent) event;
|
||||||
Address address = new Address(e.getCity(), e.getState(), e.getPostCode());
|
Address address = new Address(e.getCity(), e.getState(), e.getPostCode());
|
||||||
|
@ -2,6 +2,7 @@ package com.baeldung.patterns.escqrs.projectors;
|
|||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import com.baeldung.patterns.cqrs.repository.UserReadRepository;
|
import com.baeldung.patterns.cqrs.repository.UserReadRepository;
|
||||||
@ -14,8 +15,6 @@ import com.baeldung.patterns.es.events.UserAddressAddedEvent;
|
|||||||
import com.baeldung.patterns.es.events.UserAddressRemovedEvent;
|
import com.baeldung.patterns.es.events.UserAddressRemovedEvent;
|
||||||
import com.baeldung.patterns.es.events.UserContactAddedEvent;
|
import com.baeldung.patterns.es.events.UserContactAddedEvent;
|
||||||
import com.baeldung.patterns.es.events.UserContactRemovedEvent;
|
import com.baeldung.patterns.es.events.UserContactRemovedEvent;
|
||||||
import com.baeldung.patterns.es.events.UserCreatedEvent;
|
|
||||||
import com.baeldung.patterns.es.events.UserRemovedEvent;
|
|
||||||
|
|
||||||
public class UserProjector {
|
public class UserProjector {
|
||||||
|
|
||||||
@ -28,10 +27,6 @@ public class UserProjector {
|
|||||||
public void project(String userId, List<Event> events) {
|
public void project(String userId, List<Event> events) {
|
||||||
|
|
||||||
for (Event event : events) {
|
for (Event event : events) {
|
||||||
if (event instanceof UserCreatedEvent)
|
|
||||||
apply(userId, (UserCreatedEvent) event);
|
|
||||||
if (event instanceof UserRemovedEvent)
|
|
||||||
apply(userId, (UserRemovedEvent) event);
|
|
||||||
if (event instanceof UserAddressAddedEvent)
|
if (event instanceof UserAddressAddedEvent)
|
||||||
apply(userId, (UserAddressAddedEvent) event);
|
apply(userId, (UserAddressAddedEvent) event);
|
||||||
if (event instanceof UserAddressRemovedEvent)
|
if (event instanceof UserAddressRemovedEvent)
|
||||||
@ -44,25 +39,13 @@ public class UserProjector {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void apply(String userId, UserCreatedEvent event) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void apply(String userId, UserRemovedEvent event) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void apply(String userId, UserAddressAddedEvent event) {
|
public void apply(String userId, UserAddressAddedEvent event) {
|
||||||
Address address = new Address(event.getCity(), event.getState(), event.getPostCode());
|
Address address = new Address(event.getCity(), event.getState(), event.getPostCode());
|
||||||
UserAddress userAddress = readRepository.getUserAddress(userId);
|
UserAddress userAddress = Optional.ofNullable(readRepository.getUserAddress(userId))
|
||||||
if (userAddress == null)
|
.orElse(new UserAddress());
|
||||||
userAddress = new UserAddress();
|
Set<Address> addresses = Optional.ofNullable(userAddress.getAddressByRegion()
|
||||||
userAddress.getAddresses()
|
.get(address.getState()))
|
||||||
.add(address);
|
.orElse(new HashSet<>());
|
||||||
Set<Address> addresses = userAddress.getAddressByRegion()
|
|
||||||
.get(address.getState());
|
|
||||||
if (addresses == null)
|
|
||||||
addresses = new HashSet<>();
|
|
||||||
addresses.add(address);
|
addresses.add(address);
|
||||||
userAddress.getAddressByRegion()
|
userAddress.getAddressByRegion()
|
||||||
.put(address.getState(), addresses);
|
.put(address.getState(), addresses);
|
||||||
@ -73,30 +56,21 @@ public class UserProjector {
|
|||||||
Address address = new Address(event.getCity(), event.getState(), event.getPostCode());
|
Address address = new Address(event.getCity(), event.getState(), event.getPostCode());
|
||||||
UserAddress userAddress = readRepository.getUserAddress(userId);
|
UserAddress userAddress = readRepository.getUserAddress(userId);
|
||||||
if (userAddress != null) {
|
if (userAddress != null) {
|
||||||
userAddress.getAddresses()
|
|
||||||
.remove(address);
|
|
||||||
Set<Address> addresses = userAddress.getAddressByRegion()
|
Set<Address> addresses = userAddress.getAddressByRegion()
|
||||||
.get(address.getState());
|
.get(address.getState());
|
||||||
if (addresses != null) {
|
if (addresses != null)
|
||||||
addresses.remove(address);
|
addresses.remove(address);
|
||||||
userAddress.getAddressByRegion()
|
|
||||||
.put(address.getState(), addresses);
|
|
||||||
}
|
|
||||||
readRepository.addUserAddress(userId, userAddress);
|
readRepository.addUserAddress(userId, userAddress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void apply(String userId, UserContactAddedEvent event) {
|
public void apply(String userId, UserContactAddedEvent event) {
|
||||||
Contact contact = new Contact(event.getContactType(), event.getContactDetails());
|
Contact contact = new Contact(event.getContactType(), event.getContactDetails());
|
||||||
UserContact userContact = readRepository.getUserContact(userId);
|
UserContact userContact = Optional.ofNullable(readRepository.getUserContact(userId))
|
||||||
if (userContact == null)
|
.orElse(new UserContact());
|
||||||
userContact = new UserContact();
|
Set<Contact> contacts = Optional.ofNullable(userContact.getContactByType()
|
||||||
userContact.getContacts()
|
.get(contact.getType()))
|
||||||
.add(contact);
|
.orElse(new HashSet<>());
|
||||||
Set<Contact> contacts = userContact.getContactByType()
|
|
||||||
.get(contact.getType());
|
|
||||||
if (contacts == null)
|
|
||||||
contacts = new HashSet<>();
|
|
||||||
contacts.add(contact);
|
contacts.add(contact);
|
||||||
userContact.getContactByType()
|
userContact.getContactByType()
|
||||||
.put(contact.getType(), contacts);
|
.put(contact.getType(), contacts);
|
||||||
@ -107,17 +81,11 @@ public class UserProjector {
|
|||||||
Contact contact = new Contact(event.getContactType(), event.getContactDetails());
|
Contact contact = new Contact(event.getContactType(), event.getContactDetails());
|
||||||
UserContact userContact = readRepository.getUserContact(userId);
|
UserContact userContact = readRepository.getUserContact(userId);
|
||||||
if (userContact != null) {
|
if (userContact != null) {
|
||||||
userContact.getContacts()
|
|
||||||
.remove(contact);
|
|
||||||
Set<Contact> contacts = userContact.getContactByType()
|
Set<Contact> contacts = userContact.getContactByType()
|
||||||
.get(contact.getType());
|
.get(contact.getType());
|
||||||
if (contacts != null) {
|
if (contacts != null)
|
||||||
contacts.remove(contact);
|
contacts.remove(contact);
|
||||||
userContact.getContactByType()
|
|
||||||
.put(contact.getType(), contacts);
|
|
||||||
}
|
|
||||||
readRepository.addUserContact(userId, userContact);
|
readRepository.addUserContact(userId, userContact);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
package com.baeldung.patterns.cqrs;
|
package com.baeldung.patterns.cqrs;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.patterns.cqrs.aggregates.UserAggregate;
|
import com.baeldung.patterns.cqrs.aggregates.UserAggregate;
|
||||||
import com.baeldung.patterns.cqrs.commands.CreateUserCommand;
|
import com.baeldung.patterns.cqrs.commands.CreateUserCommand;
|
||||||
import com.baeldung.patterns.cqrs.commands.UpdateUserCommand;
|
import com.baeldung.patterns.cqrs.commands.UpdateUserCommand;
|
||||||
@ -17,15 +22,25 @@ import com.baeldung.patterns.domain.Address;
|
|||||||
import com.baeldung.patterns.domain.Contact;
|
import com.baeldung.patterns.domain.Contact;
|
||||||
import com.baeldung.patterns.domain.User;
|
import com.baeldung.patterns.domain.User;
|
||||||
|
|
||||||
public class Main {
|
public class ApplicationUnitTest {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private UserWriteRepository writeRepository;
|
||||||
UserWriteRepository writeRepository = new UserWriteRepository();
|
private UserReadRepository readRepository;
|
||||||
UserReadRepository readRepository = new UserReadRepository();
|
private UserProjector projector;
|
||||||
UserProjector projector = new UserProjector(readRepository);
|
private UserAggregate userAggregate;
|
||||||
UserAggregate userAggregate = new UserAggregate(writeRepository);
|
private UserProjection userProjection;
|
||||||
UserProjection userProjection = new UserProjection(readRepository);
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
writeRepository = new UserWriteRepository();
|
||||||
|
readRepository = new UserReadRepository();
|
||||||
|
projector = new UserProjector(readRepository);
|
||||||
|
userAggregate = new UserAggregate(writeRepository);
|
||||||
|
userProjection = new UserProjection(readRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplication() throws Exception {
|
||||||
String userId = UUID.randomUUID()
|
String userId = UUID.randomUUID()
|
||||||
.toString();
|
.toString();
|
||||||
User user = null;
|
User user = null;
|
||||||
@ -47,11 +62,13 @@ public class Main {
|
|||||||
user = userAggregate.handleUpdateUserCommand(updateUserCommand);
|
user = userAggregate.handleUpdateUserCommand(updateUserCommand);
|
||||||
projector.project(user);
|
projector.project(user);
|
||||||
|
|
||||||
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(user.getUserid(), "NY");
|
ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(userId, "EMAIL");
|
||||||
System.out.println(userProjection.handle(addressByRegionQuery));
|
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||||
|
.collect(Collectors.toSet()), userProjection.handle(contactByTypeQuery));
|
||||||
|
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY");
|
||||||
|
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||||
|
.collect(Collectors.toSet()), userProjection.handle(addressByRegionQuery));
|
||||||
|
|
||||||
ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(user.getUserid(), "EMAIL");
|
|
||||||
System.out.println(userProjection.handle(contactByTypeQuery));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.baeldung.patterns.crud;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.patterns.crud.repository.UserRepository;
|
||||||
|
import com.baeldung.patterns.crud.service.UserService;
|
||||||
|
import com.baeldung.patterns.domain.Address;
|
||||||
|
import com.baeldung.patterns.domain.Contact;
|
||||||
|
|
||||||
|
public class ApplicationUnitTest {
|
||||||
|
|
||||||
|
private UserRepository repository;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
repository = new UserRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplication() throws Exception {
|
||||||
|
UserService service = new UserService(repository);
|
||||||
|
String userId = UUID.randomUUID()
|
||||||
|
.toString();
|
||||||
|
|
||||||
|
service.createUser(userId, "Tom", "Sawyer");
|
||||||
|
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com"), new Contact("PHONE", "700-000-0001"))
|
||||||
|
.collect(Collectors.toSet()),
|
||||||
|
Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001"), new Address("Housten", "TX", "77001"))
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001"))
|
||||||
|
.collect(Collectors.toSet()),
|
||||||
|
Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001"))
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
|
||||||
|
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||||
|
.collect(Collectors.toSet()), service.getContactByType(userId, "EMAIL"));
|
||||||
|
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||||
|
.collect(Collectors.toSet()), service.getAddressByRegion(userId, "NY"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.patterns.es;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.patterns.domain.Address;
|
||||||
|
import com.baeldung.patterns.domain.Contact;
|
||||||
|
import com.baeldung.patterns.es.repository.EventStore;
|
||||||
|
import com.baeldung.patterns.es.service.UserService;
|
||||||
|
|
||||||
|
public class ApplicationUnitTest {
|
||||||
|
|
||||||
|
private EventStore repository;
|
||||||
|
private UserService service;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
repository = new EventStore();
|
||||||
|
service = new UserService(repository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplication() throws Exception {
|
||||||
|
String userId = UUID.randomUUID()
|
||||||
|
.toString();
|
||||||
|
|
||||||
|
service.createUser(userId, "Tom", "Sawyer");
|
||||||
|
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com"), new Contact("PHONE", "700-000-0001"))
|
||||||
|
.collect(Collectors.toSet()),
|
||||||
|
Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001"), new Address("Housten", "TX", "77001"))
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001"))
|
||||||
|
.collect(Collectors.toSet()),
|
||||||
|
Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001"))
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
|
||||||
|
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||||
|
.collect(Collectors.toSet()), service.getContactByType(userId, "EMAIL"));
|
||||||
|
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||||
|
.collect(Collectors.toSet()), service.getAddressByRegion(userId, "NY"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,10 +1,15 @@
|
|||||||
package com.baeldung.patterns.escqrs;
|
package com.baeldung.patterns.escqrs;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.patterns.cqrs.commands.CreateUserCommand;
|
import com.baeldung.patterns.cqrs.commands.CreateUserCommand;
|
||||||
import com.baeldung.patterns.cqrs.commands.UpdateUserCommand;
|
import com.baeldung.patterns.cqrs.commands.UpdateUserCommand;
|
||||||
import com.baeldung.patterns.cqrs.projections.UserProjection;
|
import com.baeldung.patterns.cqrs.projections.UserProjection;
|
||||||
@ -18,16 +23,25 @@ import com.baeldung.patterns.es.repository.EventStore;
|
|||||||
import com.baeldung.patterns.escqrs.aggregates.UserAggregate;
|
import com.baeldung.patterns.escqrs.aggregates.UserAggregate;
|
||||||
import com.baeldung.patterns.escqrs.projectors.UserProjector;
|
import com.baeldung.patterns.escqrs.projectors.UserProjector;
|
||||||
|
|
||||||
public class Main {
|
public class ApplicationUnitTest {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private EventStore writeRepository;
|
||||||
|
private UserReadRepository readRepository;
|
||||||
|
private UserProjector projector;
|
||||||
|
private UserAggregate userAggregate;
|
||||||
|
private UserProjection userProjection;
|
||||||
|
|
||||||
EventStore writeRepository = new EventStore();
|
@Before
|
||||||
UserReadRepository readRepository = new UserReadRepository();
|
public void setUp() {
|
||||||
UserProjector projector = new UserProjector(readRepository);
|
writeRepository = new EventStore();
|
||||||
UserAggregate userAggregate = new UserAggregate(writeRepository);
|
readRepository = new UserReadRepository();
|
||||||
UserProjection userProjection = new UserProjection(readRepository);
|
projector = new UserProjector(readRepository);
|
||||||
|
userAggregate = new UserAggregate(writeRepository);
|
||||||
|
userProjection = new UserProjection(readRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplication() throws Exception {
|
||||||
String userId = UUID.randomUUID()
|
String userId = UUID.randomUUID()
|
||||||
.toString();
|
.toString();
|
||||||
List<Event> events = null;
|
List<Event> events = null;
|
||||||
@ -50,11 +64,12 @@ public class Main {
|
|||||||
events = userAggregate.handleUpdateUserCommand(updateUserCommand);
|
events = userAggregate.handleUpdateUserCommand(updateUserCommand);
|
||||||
projector.project(userId, events);
|
projector.project(userId, events);
|
||||||
|
|
||||||
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY");
|
|
||||||
System.out.println(userProjection.handle(addressByRegionQuery));
|
|
||||||
|
|
||||||
ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(userId, "EMAIL");
|
ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(userId, "EMAIL");
|
||||||
System.out.println(userProjection.handle(contactByTypeQuery));
|
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||||
|
.collect(Collectors.toSet()), userProjection.handle(contactByTypeQuery));
|
||||||
|
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY");
|
||||||
|
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||||
|
.collect(Collectors.toSet()), userProjection.handle(addressByRegionQuery));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user