2023-04-25 15:49:47 +00:00
|
|
|
import { CommandHandler } from '@nestjs/cqrs';
|
|
|
|
import { CreateAdCommand } from '../../commands/create-ad.command';
|
|
|
|
import { Ad } from '../entities/ad';
|
|
|
|
import { AdRepository } from '../../adapters/secondaries/ad.repository';
|
2023-04-26 10:10:22 +00:00
|
|
|
import { InjectMapper } from '@automapper/nestjs';
|
|
|
|
import { Mapper } from '@automapper/core';
|
|
|
|
import { CreateAdRequest } from '../dtos/create-ad.request';
|
2023-04-25 15:49:47 +00:00
|
|
|
|
|
|
|
@CommandHandler(CreateAdCommand)
|
|
|
|
export class CreateAdUseCase {
|
2023-04-26 10:10:22 +00:00
|
|
|
constructor(
|
|
|
|
@InjectMapper() private readonly mapper: Mapper,
|
|
|
|
private readonly adRepository: AdRepository,
|
|
|
|
) {}
|
2023-04-25 15:49:47 +00:00
|
|
|
|
|
|
|
async execute(command: CreateAdCommand): Promise<Ad> {
|
|
|
|
try {
|
2023-04-26 10:10:22 +00:00
|
|
|
const adToCreate: Ad = this.mapper.map(
|
|
|
|
command.createAdRequest,
|
|
|
|
CreateAdRequest,
|
|
|
|
Ad,
|
|
|
|
);
|
|
|
|
return adToCreate;
|
|
|
|
// return await this.adRepository.createAd(adToCreate);
|
2023-04-25 15:49:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|