This commit is contained in:
sbriat 2023-04-27 17:52:43 +02:00
parent 96577e119f
commit 95310651d8
5 changed files with 57 additions and 12 deletions

View File

@ -24,14 +24,21 @@ export class AdMessagerController {
const parsedMessage: CreateAdRequest = JSON.parse(message);
console.log(parsedMessage);
// create a real instance of CreateAdRequest from parsed message
const createAdRequest: CreateAdRequest = this.mapper.map(
parsedMessage,
CreateAdRequest,
CreateAdRequest,
);
// const createAdRequest: CreateAdRequest = this.mapper.map(
// parsedMessage,
// CreateAdRequest,
// CreateAdRequest,
// );
const createAdRequest = new CreateAdRequest();
createAdRequest.originType = parsedMessage.originType;
createAdRequest.destinationType = parsedMessage.destinationType;
createAdRequest.waypoints = parsedMessage.waypoints.map((waypoint) => ({
lon: waypoint.lon,
lat: waypoint.lat,
}));
console.log(createAdRequest);
// validate instance
await validateOrReject(createAdRequest);
await validateOrReject(createAdRequest.waypoints[0]);
const ad: Ad = await this.commandBus.execute(
new CreateAdCommand(createAdRequest),
);

View File

@ -8,10 +8,12 @@ import {
IsNumber,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
import { PointType } from '../../../geography/domain/types/point-type.enum';
import { Frequency } from '../types/frequency.enum';
import { Coordinates } from '../../../geography/domain/types/coordinates.type';
import { Type } from 'class-transformer';
export class CreateAdRequest {
@IsString()
@ -112,7 +114,9 @@ export class CreateAdRequest {
destinationType: PointType;
@IsArray()
@ValidateNested({ each: true })
@ArrayMinSize(2)
@Type(() => Coordinates)
@AutoMap(() => [Coordinates])
waypoints: Coordinates[];

View File

@ -4,6 +4,7 @@ import { Injectable } from '@nestjs/common';
import { Ad } from '../domain/entities/ad';
import { AdPresenter } from '../adapters/primaries/ad.presenter';
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
import { Coordinates } from 'src/modules/geography/domain/types/coordinates.type';
@Injectable()
export class AdProfile extends AutomapperProfile {
@ -14,7 +15,25 @@ export class AdProfile extends AutomapperProfile {
override get profile() {
return (mapper: any) => {
createMap(mapper, Ad, AdPresenter);
createMap(mapper, CreateAdRequest, CreateAdRequest);
createMap(
mapper,
CreateAdRequest,
CreateAdRequest,
forMember(
(dest) => dest.waypoints,
mapFrom(
(source) =>
source.waypoints.map(
(waypoint) =>
new Coordinates(
waypoint.lon ?? undefined,
waypoint.lat ?? undefined,
),
),
// .filter((waypoint) => waypoint),
),
),
);
createMap(
mapper,
CreateAdRequest,

View File

@ -0,0 +1,17 @@
import { createMap, Mapper } from '@automapper/core';
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common';
import { Coordinates } from '../../geography/domain/types/coordinates.type';
@Injectable()
export class CoordinatesProfile extends AutomapperProfile {
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}
override get profile() {
return (mapper: any) => {
createMap(mapper, Coordinates, Coordinates);
};
}
}

View File

@ -1,5 +1,5 @@
import { AutoMap } from '@automapper/classes';
import { IsNumber, Max, Min } from 'class-validator';
import { IsLatitude, IsLongitude, IsNumber } from 'class-validator';
export class Coordinates {
constructor(lon: number, lat: number) {
@ -8,14 +8,12 @@ export class Coordinates {
}
@IsNumber()
@Min(-180)
@Max(180)
@IsLongitude()
@AutoMap()
lon: number;
@IsNumber()
@Min(-90)
@Max(90)
@IsLatitude()
@AutoMap()
lat: number;
}