Sorting self-referencing aggregate roots

Viewed 29

I'm using Domain-Driven Design and CQRS on my project and I have a self-referencing aggregate root to model a hierarchical structure:

Category
  CategoryId $id
  CategoryName $name
  ?CategoryId $parentId
  CategoryPosition $position

Now I want to create a feature to sort the category tree. I receive the following structure on an API controller:

[
  {
    "id": "ffb03f1d-dade-4f45-80d5-6e28fea17e70",
    "children": [
      {
        "id": "bdb82eb6-59ea-4645-ae70-d03b5e01c4b6",
        "children": [
          {
            "id": "0ad9c916-eb64-4b0a-b2d0-6864b81b304f",
            "children": []
          }
        ]
      },
      {
        "id": "afa58caf-a0b4-4787-b784-ed3344af2b2b",
        "children": [
          {
            "id": "6f334ab9-47b3-4f7b-8f87-f9c36db8afe5",
            "children": []
          }
        ]
      }
    ]
  },
  {
    "id": "7d3b6110-c282-42b3-b649-efc9394fa5f0",
    "children": []
  }
]

Currently I'm recursively traversing the new tree in infrastructure handling a MoveCategory command to change parentId and position for each category in the tree.

I cannot do it with a single command, because each command should modify a single Category.
Also I'm checking that tree depth is not over 4 levels, which is a domain rule.

The question is, how can I traverse the new tree and do changes on domain layer?

0 Answers
Related