-
Notifications
You must be signed in to change notification settings - Fork 1
3. First 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.
git checkout -b feature-hero-equip-item
Let's start with the core part of the feature.
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.
Now you want to modify this new property, let's use the DDD Hexagonal Generator to create a new command EquipItem.
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. Let's keep it simple for now, we won't check if the Hero or the Item exists. - 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. - Write a unit test for your command-handler
equip-item.spec.ts, you can take a look atheroes/core/application/commands/gain-xp/gain-xp.spec.tsfor inspiration. - Commit your changes: Second commit
Now you should change the infrastructure; we want the update made to the Hero entity to be persisted in the database.
- Add a equippedItem column in the Hero Typeorm entity
heroes/infrastructure/typeorm/hero.orm-entity.ts. - Create a migration for this change:
npx typeorm migration:generate -n alterHeroTableAddEquippedItemColumn - Update the mapper between the domain entity and the database entity:
heroes/infrastructure/typeorm/hero.orm-mapper.ts - Commit your changes: Third commit