Skip to content

Commit

Permalink
feature(hero): add an equip item command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Chabardes committed May 31, 2022
1 parent bcfe4d1 commit e241b0b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<EquipItemCommand>
{
constructor(
@Inject(Hero)
private readonly heroPorts: UpdatePort<Hero>,
) {}

private readonly logger = new Logger(EquipItemCommandHandler.name);

public async execute({ payload }: EquipItemCommand): Promise<void> {
this.logger.log(`> EquipItemCommand: ${JSON.stringify(payload)}`);
const { heroId, itemId } = payload;

await this.heroPorts.update(heroId, { equippedItem: itemId });
}
}
Original file line number Diff line number Diff line change
@@ -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'];
},
) {}
}
26 changes: 26 additions & 0 deletions src/heroes/core/application/commands/equip-item/equip-item.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 2 additions & 0 deletions src/heroes/core/application/hero.application.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -18,6 +19,7 @@ const HeroQueryHandler: QueryHandlerType[] = [
];
const HeroCommandHandler: CommandHandlerType[] = [
CreateHeroCommandHandler,
EquipItemCommandHandler,
GainXpCommandHandler,
HealHeroCommandHandler,
HurtHeroCommandHandler,
Expand Down

0 comments on commit e241b0b

Please # to comment.