Skip to content

Commit 1a66386

Browse files
vanroguclaude
andcommitted
Replace getEventById with EventQuery for event retrieval (#41)
* Replace getEventById usage with query-based event lookup in examples The eventmodeling library no longer calls getEventById from the eventstore library. The two example apps now query all AccountOpened events and filter by event ID instead. https://claude.ai/code/session_01LPcFaA1bRJrcZjP4Rh3Mwe * fixed examples by adding bounded context start --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 70c794c commit 1a66386

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

sliceworkz-eventmodeling-examples/src/main/java/org/sliceworkz/eventmodeling/examples/banking/BankingClosingTheBooksExample.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
import org.sliceworkz.eventstore.EventStoreFactory;
4242
import org.sliceworkz.eventstore.events.Event;
4343
import org.sliceworkz.eventstore.events.EventReference;
44+
import org.sliceworkz.eventstore.events.Tags;
4445
import org.sliceworkz.eventstore.infra.inmem.InMemoryEventStorage;
46+
import org.sliceworkz.eventstore.query.EventQuery;
47+
import org.sliceworkz.eventstore.query.EventTypesFilter;
4548
import org.sliceworkz.eventstore.spi.EventStorage;
4649
import org.sliceworkz.eventstore.stream.EventStream;
4750
import org.sliceworkz.eventstore.stream.EventStreamId;
@@ -100,6 +103,8 @@ public static void main(String[] args) {
100103
.done();
101104

102105
ClosingTheBooksBoundedContext bc = builder.build(ClosingTheBooksBoundedContext.class);
106+
107+
bc.start();
103108

104109
EventStream<BankingEvent> eventStream = eventStore.getEventStream(
105110
EventStreamId.forContext("banking-ctb").withPurpose("domain"),
@@ -117,9 +122,11 @@ public static void main(String[] args) {
117122
Optional<EventReference> ref = bc.execute(new OpenBankAccountCommand(customerId, january));
118123

119124
// Retrieve the AccountOpened event to get the generated accountId
120-
AccountOpened accountOpened = eventStream.getEventById(ref.get().id())
125+
AccountOpened accountOpened = eventStream.query(EventQuery.forEvents(EventTypesFilter.of(AccountOpened.class), Tags.none()))
126+
.filter(e -> e.reference().id().equals(ref.get().id()))
121127
.map(Event::data)
122128
.map(e -> (AccountOpened) e)
129+
.findFirst()
123130
.get();
124131
DomainConceptId accountId = accountOpened.accountId();
125132

sliceworkz-eventmodeling-examples/src/main/java/org/sliceworkz/eventmodeling/examples/banking/BankingExample.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
import org.sliceworkz.eventstore.EventStoreFactory;
3636
import org.sliceworkz.eventstore.events.Event;
3737
import org.sliceworkz.eventstore.events.EventReference;
38+
import org.sliceworkz.eventstore.events.Tags;
3839
import org.sliceworkz.eventstore.infra.inmem.InMemoryEventStorage;
3940
import org.sliceworkz.eventstore.query.EventQuery;
41+
import org.sliceworkz.eventstore.query.EventTypesFilter;
4042
import org.sliceworkz.eventstore.spi.EventStorage;
4143
import org.sliceworkz.eventstore.stream.EventStream;
4244
import org.sliceworkz.eventstore.stream.EventStreamEventuallyConsistentAppendListener;
@@ -63,6 +65,8 @@ public static void main ( String[] args ) {
6365
.done()
6466
.build(BankingBoundedContext.class);
6567

68+
bc.start();
69+
6670
EventStream<BankingDomainEvent> eventStream = eventStore.getEventStream(EventStreamId.forContext("banking").withPurpose("domain"), BankingDomainEvent.class);
6771

6872
/*
@@ -95,7 +99,12 @@ public EventReference eventsAppended(EventReference atLeastUntil) {
9599
Optional<EventReference> ref = bc.execute(new OpenAccountCommand(DomainConceptId.create()));
96100

97101
// Go fetch the AccountOpened Event that should have been raised by the OpenAccountCommmand
98-
AccountOpened ao = eventStream.getEventById(ref.get().id()).map(Event::data).map(e->(AccountOpened)e).get();
102+
AccountOpened ao = eventStream.query(EventQuery.forEvents(EventTypesFilter.of(AccountOpened.class), Tags.none()))
103+
.filter(e -> e.reference().id().equals(ref.get().id()))
104+
.map(Event::data)
105+
.map(e -> (AccountOpened) e)
106+
.findFirst()
107+
.get();
99108

100109
// Render a live model with the details of the Account
101110
AccountDetailsReadModel rm = bc.read(AccountDetailsReadModel.class, ao.accountId());

0 commit comments

Comments
 (0)