Hi,
I’m trying to reach a simple product reservation scenario when a order is created.
When handling a ReserveProductCommand, I need to load the latest product information to check whether if the stock is enough to reserve.
`
Aggregate aggregate = repository.load(command.getProductId());
aggregate.execute(aggregateRoot->aggregateRoot.reserve(command.getOrderId(), command.getNumber()));
`
`
public void reserve(OrderId orderId, int amount){
if(stock>=amount) {
int oStock = stock;
stock = stock - amount;
apply(new ProductReservedEvent(orderId, id, stock));
LOGGER.info(“Product [{}] stock changes {} → {}”, id, oStock, stock);
}else
apply(new ProductNotEnoughEvent(orderId, id));
}
`
I use mongodb as the EventStore, and the event are as below
`
db.domainevents.find({“type”:“ProductAggregate”}).pretty()
{
“_id” : ObjectId(“58d4e5aa73bc0c0f745619ae”),
“aggregateIdentifier” : “1”,
“type” : “ProductAggregate”,
“sequenceNumber” : NumberLong(0),
“serializedPayload” : “{"id":"1","name":"ttt","price":1000,"stock":100}”,
“timestamp” : “2017-03-24T09:23:54.101Z”,
“payloadType” : “com.edi.learn.axon.common.events.ProductCreatedEvent”,
“payloadRevision” : null,
“serializedMetaData” : “{"traceId":"9a8ea7a6-8825-4dc2-8451-45e900e14f94","correlationId":"9a8ea7a6-8825-4dc2-8451-45e900e14f94"}”,
“eventIdentifier” : “d6c90144-7320-46f7-9c63-c5a3c5d63bcf”
}
{
“_id” : ObjectId(“58d4e5fe73bc0c0f745619b0”),
“aggregateIdentifier” : “1”,
“type” : “ProductAggregate”,
“sequenceNumber” : NumberLong(1),
“serializedPayload” : “{"orderId":{"identifier":"18e18a59-2313-4908-bff6-ce20f40ae2a1"},"productId":"1","amount":10}”,
“timestamp” : “2017-03-24T09:25:18.310Z”,
“payloadType” : “com.edi.learn.axon.common.events.ProductReservedEvent”,
“payloadRevision” : null,
“serializedMetaData” : “{"traceId":"c67e86d8-3964-496c-b6cd-1e2552a9dae8","correlationId":"c67e86d8-3964-496c-b6cd-1e2552a9dae8"}”,
“eventIdentifier” : “46c28d7b-dda2-43e7-981a-0e31871f5516”
}
{
“_id” : ObjectId(“58d4e63f73bc0c0f745619b3”),
“aggregateIdentifier” : “1”,
“type” : “ProductAggregate”,
“sequenceNumber” : NumberLong(2),
“serializedPayload” : “{"orderId":{"identifier":"1786d799-aa4b-472a-ba17-87416b52ad43"},"productId":"1","amount":10}”,
“timestamp” : “2017-03-24T09:26:23.334Z”,
“payloadType” : “com.edi.learn.axon.common.events.ProductReservedEvent”,
“payloadRevision” : null,
“serializedMetaData” : “{"traceId":"6e541c61-b367-4503-b9ee-fa120a3626cc","correlationId":"6e541c61-b367-4503-b9ee-fa120a3626cc"}”,
“eventIdentifier” : “0fec7b33-b38a-4d23-811e-6baf95cd27df”
}
`
The method runs correctly, but the aggregate state is not updated. It always return the original stock number.
Can anyone help?
Thanks,