2023-04-24 14:44:52 +00:00
|
|
|
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
|
|
|
|
import { Controller } from '@nestjs/common';
|
2023-04-25 15:49:47 +00:00
|
|
|
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 {
|
2023-04-25 15:49:47 +00:00
|
|
|
constructor(
|
|
|
|
private readonly _commandBus: CommandBus,
|
|
|
|
@InjectMapper() private readonly _mapper: Mapper,
|
|
|
|
) {}
|
|
|
|
|
2023-04-24 14:44:52 +00:00
|
|
|
@RabbitSubscribe({
|
|
|
|
name: 'adCreated',
|
|
|
|
})
|
2023-04-25 15:49:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|