matcher/src/modules/ad/adapters/primaries/ad-messager.controller.ts

33 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-04-24 14:44:52 +00:00
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
import { Controller } from '@nestjs/common';
import { Ad } from '../../domain/entities/ad';
import { InjectMapper } from '@automapper/nestjs';
import { Mapper } from '@automapper/core';
import { CommandBus } from '@nestjs/cqrs';
import { CreateAdCommand } from '../../commands/create-ad.command';
import { AdPresenter } from './ad.presenter';
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
2023-04-24 14:44:52 +00:00
@Controller()
export class AdMessagerController {
constructor(
private readonly _commandBus: CommandBus,
@InjectMapper() private readonly _mapper: Mapper,
) {}
2023-04-24 14:44:52 +00:00
@RabbitSubscribe({
name: 'adCreated',
})
async adCreatedHandler(message: string): Promise<AdPresenter> {
try {
const createAdRequest: CreateAdRequest = JSON.parse(message);
const ad: Ad = await this._commandBus.execute(
new CreateAdCommand(createAdRequest),
);
return this._mapper.map(ad, Ad, AdPresenter);
} catch (e) {
console.log('error', e);
}
2023-04-24 14:44:52 +00:00
}
}