matcher/src/modules/ad/domain/usecases/create-ad.usecase.ts

40 lines
1.5 KiB
TypeScript
Raw Normal View History

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';
@CommandHandler(CreateAdCommand)
export class CreateAdUseCase {
2023-04-26 10:10:22 +00:00
constructor(
@InjectMapper() private readonly mapper: Mapper,
private readonly adRepository: AdRepository,
) {}
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,
);
2023-05-11 15:47:55 +00:00
adToCreate.driverDistance = command.geography.driverRoute?.distance;
adToCreate.driverDuration = command.geography.driverRoute?.duration;
adToCreate.passengerDistance = command.geography.passengerRoute?.distance;
adToCreate.passengerDuration = command.geography.passengerRoute?.duration;
adToCreate.fwdAzimuth = command.geography.driverRoute
? command.geography.driverRoute.fwdAzimuth
: command.geography.passengerRoute.fwdAzimuth;
adToCreate.backAzimuth = command.geography.driverRoute
? command.geography.driverRoute.backAzimuth
: command.geography.passengerRoute.backAzimuth;
2023-04-26 10:10:22 +00:00
return adToCreate;
// return await this.adRepository.createAd(adToCreate);
} catch (error) {
throw error;
}
}
}