dwz
dwz

Reputation: 385

How to read and filter entities-aggregates based on condition in Axon and after that change them

I am new with Axon and maybe I missed something, but need help to understand.

I have a simple food cart aggregate.

Here is example:

@Aggregate
class FoodCard {

    @AggregateIdentifier
    private lateinit var foodCardId: UUID
    private lateinit var selectedProduct: MutableMap<UUID, Int>

    constructor()

    @CommandHandler
    constructor(command: CreateFoodCartCommand) {
        AggregateLifecycle.apply(FoodCartCreateEvent(
            UUID.randomUUID()
        ))
    }

    @CommandHandler
    fun handle(command: SelectProductCommand) {
        AggregateLifecycle
            .apply(ProductSelectedEvent(foodCardId, command.productId, command.quantity))
    }

    @CommandHandler
    fun handle(command: DeleteFoodCartCommand) {
        AggregateLifecycle
            .apply(FoodCartDeleteEvent(foodCardId))
    }

    @CommandHandler
    fun handle(command: DeselectProductCommand) {

        val productId = command.productId
        if (!selectedProduct.containsKey(productId)) {
            throw ProductDeselectionException("ProductDeselectionException")
        }
        AggregateLifecycle
            .apply(ProductDeselectEvent(foodCardId, productId, command.quantity))
    }

    @EventSourcingHandler
    fun on(event: FoodCartCreateEvent) {
        foodCardId = event.foodCardId
        selectedProduct = mutableMapOf()
    }

    @EventSourcingHandler
    fun on(event: ProductSelectedEvent) {
        selectedProduct.merge(
                event.productId,
                event.quantity
        ) {a, b -> a + b}
    }
}

As ES I am using Axon Server. For FoodCard projector I am using JPA repository that connects to DB.

I want to get all foodcards that contain special product (concrete UUID) and change quantity to -1 for all of them.

I understood there are two types of actions -> read and write

So the question how to correctly implement this flow with Axon?

Thanks

Upvotes: 0

Views: 173

Answers (1)

Corrado Musumeci
Corrado Musumeci

Reputation: 306

from your explanation and code I feel that you will probably need to complete your implementation of DeselectProductCommand introducing an EventSourcingHandler for ProductDeselectEvent. If I understood correctly your "quantity" information is stored into the selectProduct Map. In this case, based on your code, I see that the information of the quantity that should be subtracted to your product is in the command.

You will also need a Query, such as FindAllFoodCardByProductId, that will retrieve the foodCardId aggregate identifier that contains a certain productId: this operation will be performed on your Projection through the jpa repository. As a reference you can have a look at the ref guide here https://docs.axoniq.io/reference-guide/implementing-domain-logic/query-handling on how to use QueryGateway into your controller and implement a QueryHandler into your Projection. Corrado.

Upvotes: 2

Related Questions