From e241b0be864cb866f276a5e3d30ccac98b2c7eb8 Mon Sep 17 00:00:00 2001 From: Pierre Chabardes Date: Wed, 4 May 2022 11:48:51 +0200 Subject: [PATCH] feature(hero): add an equip item command --- .../equip-item/equip-item.command-handler.ts | 24 +++++++++++++++++ .../commands/equip-item/equip-item.command.ts | 12 +++++++++ .../commands/equip-item/equip-item.spec.ts | 26 +++++++++++++++++++ .../core/application/hero.application.ts | 2 ++ 4 files changed, 64 insertions(+) create mode 100644 src/heroes/core/application/commands/equip-item/equip-item.command-handler.ts create mode 100644 src/heroes/core/application/commands/equip-item/equip-item.command.ts create mode 100644 src/heroes/core/application/commands/equip-item/equip-item.spec.ts diff --git a/src/heroes/core/application/commands/equip-item/equip-item.command-handler.ts b/src/heroes/core/application/commands/equip-item/equip-item.command-handler.ts new file mode 100644 index 0000000..4b818ff --- /dev/null +++ b/src/heroes/core/application/commands/equip-item/equip-item.command-handler.ts @@ -0,0 +1,24 @@ +import { Inject, Logger } from '@nestjs/common'; +import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; +import { UpdatePort } from '../../../../../common/core/domain/base.ports'; +import { Hero } from '../../../domain/hero.entity'; +import { EquipItemCommand } from './equip-item.command'; + +@CommandHandler(EquipItemCommand) +export class EquipItemCommandHandler + implements ICommandHandler +{ + constructor( + @Inject(Hero) + private readonly heroPorts: UpdatePort, + ) {} + + private readonly logger = new Logger(EquipItemCommandHandler.name); + + public async execute({ payload }: EquipItemCommand): Promise { + this.logger.log(`> EquipItemCommand: ${JSON.stringify(payload)}`); + const { heroId, itemId } = payload; + + await this.heroPorts.update(heroId, { equippedItem: itemId }); + } +} diff --git a/src/heroes/core/application/commands/equip-item/equip-item.command.ts b/src/heroes/core/application/commands/equip-item/equip-item.command.ts new file mode 100644 index 0000000..452ce2b --- /dev/null +++ b/src/heroes/core/application/commands/equip-item/equip-item.command.ts @@ -0,0 +1,12 @@ +import { ICommand } from '@nestjs/cqrs'; +import { Item } from '../../../../../items/core/domain/item.entity'; +import { Hero } from '../../../domain/hero.entity'; + +export class EquipItemCommand implements ICommand { + constructor( + public readonly payload: { + heroId: Hero['id']; + itemId: Item['id']; + }, + ) {} +} diff --git a/src/heroes/core/application/commands/equip-item/equip-item.spec.ts b/src/heroes/core/application/commands/equip-item/equip-item.spec.ts new file mode 100644 index 0000000..b217f9d --- /dev/null +++ b/src/heroes/core/application/commands/equip-item/equip-item.spec.ts @@ -0,0 +1,26 @@ +import { ItemMockAdapter } from '../../../../../items/infrastructure/mock/item.mock-adapter'; +import { HeroMockAdapter } from '../../../../infrastructure/mock/hero.mock-adapter'; +import { EquipItemCommand } from './equip-item.command'; +import { EquipItemCommandHandler } from './equip-item.command-handler'; + +describe('equip item command', () => { + const heroMockAdapter = new HeroMockAdapter(); + const itemMockAdapter = new ItemMockAdapter(); + const equipItemCommandHandler = new EquipItemCommandHandler(heroMockAdapter); + + beforeEach(() => { + heroMockAdapter.reset(); + itemMockAdapter.reset(); + }); + + it('hero that equip an item should have an equipped item', async () => { + const { id: heroId } = await heroMockAdapter.create({}); + const { id: itemId } = await itemMockAdapter.create({}); + await equipItemCommandHandler.execute( + new EquipItemCommand({ heroId, itemId }), + ); + + const hero = await heroMockAdapter.getById(heroId); + expect(hero.equippedItem).toStrictEqual(itemId); + }); +}); diff --git a/src/heroes/core/application/hero.application.ts b/src/heroes/core/application/hero.application.ts index d478c28..a8595e9 100644 --- a/src/heroes/core/application/hero.application.ts +++ b/src/heroes/core/application/hero.application.ts @@ -1,5 +1,6 @@ import { CommandHandlerType, QueryHandlerType } from '@nestjs/cqrs'; import { CreateHeroCommandHandler } from './commands/create-hero/create-hero.command-handler'; +import { EquipItemCommandHandler } from './commands/equip-item/equip-item.command-handler'; import { GainXpCommandHandler } from './commands/gain-xp/gain-xp.command-handler'; import { HealHeroCommandHandler } from './commands/heal-hero/heal-hero.command-handler'; import { HurtHeroCommandHandler } from './commands/hurt-hero/hurt-hero.command-handler'; @@ -18,6 +19,7 @@ const HeroQueryHandler: QueryHandlerType[] = [ ]; const HeroCommandHandler: CommandHandlerType[] = [ CreateHeroCommandHandler, + EquipItemCommandHandler, GainXpCommandHandler, HealHeroCommandHandler, HurtHeroCommandHandler,