generated from nestjs/typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(hero): add an equip item command
- Loading branch information
Pierre Chabardes
committed
May 31, 2022
1 parent
bcfe4d1
commit e241b0b
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/heroes/core/application/commands/equip-item/equip-item.command-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/heroes/core/application/commands/equip-item/equip-item.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/heroes/core/application/commands/equip-item/equip-item.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters