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,
|
|
|
|
);
|
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);
|
2023-04-25 15:49:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|