Skip to content

3. First feature

Chabardes edited this page May 4, 2022 · 45 revisions

A new feature

When a hero slays a dragon, he gets a new item in his inventory, but you may have noticed that, for now, he cannot do anything with it. Let's change that ! Here's your ticket: As a Hero, I can equip an item and it improves my attack. Try to do it on your own but you can check the commits if you need help.

Create a new branch

git checkout -b feature-hero-equip-item

Modify the hero entity

In heroes/core/domain/hero.entity.ts, add an equippedItem property to the Hero entity, and modify the getHeroAttackValue function to take it in account (keep it simple for now). Commit your first changes: First commit.

Create a new command

Now you want to modify this new property, let's use the DDD Hexagonal Generator to create a new command EquipItem.

image

3 files should have been created, let's get rid of the equip-item.port.ts for now.

  • You will likely need the id of the Hero equipping the item and the id of the item, add them in the payload of the command in equip-item.command.ts.
  • In equip-item.command-handler.ts, change the port being injected in the constructor to @Inject(Hero) private readonly heroPorts: UpdatePort<Hero>. This means that you will use a port of the entity Hero to update it. There are similar ports to Get, Create, or Delete an entity.
  • In the execute method of the handler, use the payload to make the correct update.
  • In heroes/core/application/hero.applications, don't forget to add your new application.
  • Commit your changes: Second commit

Clone this wiki locally