wip
This commit is contained in:
parent
96577e119f
commit
95310651d8
|
@ -24,14 +24,21 @@ export class AdMessagerController {
|
||||||
const parsedMessage: CreateAdRequest = JSON.parse(message);
|
const parsedMessage: CreateAdRequest = JSON.parse(message);
|
||||||
console.log(parsedMessage);
|
console.log(parsedMessage);
|
||||||
// create a real instance of CreateAdRequest from parsed message
|
// create a real instance of CreateAdRequest from parsed message
|
||||||
const createAdRequest: CreateAdRequest = this.mapper.map(
|
// const createAdRequest: CreateAdRequest = this.mapper.map(
|
||||||
parsedMessage,
|
// parsedMessage,
|
||||||
CreateAdRequest,
|
// CreateAdRequest,
|
||||||
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);
|
console.log(createAdRequest);
|
||||||
// validate instance
|
// validate instance
|
||||||
await validateOrReject(createAdRequest);
|
await validateOrReject(createAdRequest.waypoints[0]);
|
||||||
const ad: Ad = await this.commandBus.execute(
|
const ad: Ad = await this.commandBus.execute(
|
||||||
new CreateAdCommand(createAdRequest),
|
new CreateAdCommand(createAdRequest),
|
||||||
);
|
);
|
||||||
|
|
|
@ -8,10 +8,12 @@ import {
|
||||||
IsNumber,
|
IsNumber,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
|
ValidateNested,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { PointType } from '../../../geography/domain/types/point-type.enum';
|
import { PointType } from '../../../geography/domain/types/point-type.enum';
|
||||||
import { Frequency } from '../types/frequency.enum';
|
import { Frequency } from '../types/frequency.enum';
|
||||||
import { Coordinates } from '../../../geography/domain/types/coordinates.type';
|
import { Coordinates } from '../../../geography/domain/types/coordinates.type';
|
||||||
|
import { Type } from 'class-transformer';
|
||||||
|
|
||||||
export class CreateAdRequest {
|
export class CreateAdRequest {
|
||||||
@IsString()
|
@IsString()
|
||||||
|
@ -112,7 +114,9 @@ export class CreateAdRequest {
|
||||||
destinationType: PointType;
|
destinationType: PointType;
|
||||||
|
|
||||||
@IsArray()
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
@ArrayMinSize(2)
|
@ArrayMinSize(2)
|
||||||
|
@Type(() => Coordinates)
|
||||||
@AutoMap(() => [Coordinates])
|
@AutoMap(() => [Coordinates])
|
||||||
waypoints: Coordinates[];
|
waypoints: Coordinates[];
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { Injectable } from '@nestjs/common';
|
||||||
import { Ad } from '../domain/entities/ad';
|
import { Ad } from '../domain/entities/ad';
|
||||||
import { AdPresenter } from '../adapters/primaries/ad.presenter';
|
import { AdPresenter } from '../adapters/primaries/ad.presenter';
|
||||||
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
|
import { CreateAdRequest } from '../domain/dtos/create-ad.request';
|
||||||
|
import { Coordinates } from 'src/modules/geography/domain/types/coordinates.type';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AdProfile extends AutomapperProfile {
|
export class AdProfile extends AutomapperProfile {
|
||||||
|
@ -14,7 +15,25 @@ export class AdProfile extends AutomapperProfile {
|
||||||
override get profile() {
|
override get profile() {
|
||||||
return (mapper: any) => {
|
return (mapper: any) => {
|
||||||
createMap(mapper, Ad, AdPresenter);
|
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(
|
createMap(
|
||||||
mapper,
|
mapper,
|
||||||
CreateAdRequest,
|
CreateAdRequest,
|
||||||
|
|
|
@ -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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
import { AutoMap } from '@automapper/classes';
|
import { AutoMap } from '@automapper/classes';
|
||||||
import { IsNumber, Max, Min } from 'class-validator';
|
import { IsLatitude, IsLongitude, IsNumber } from 'class-validator';
|
||||||
|
|
||||||
export class Coordinates {
|
export class Coordinates {
|
||||||
constructor(lon: number, lat: number) {
|
constructor(lon: number, lat: number) {
|
||||||
|
@ -8,14 +8,12 @@ export class Coordinates {
|
||||||
}
|
}
|
||||||
|
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
@Min(-180)
|
@IsLongitude()
|
||||||
@Max(180)
|
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
lon: number;
|
lon: number;
|
||||||
|
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
@Min(-90)
|
@IsLatitude()
|
||||||
@Max(90)
|
|
||||||
@AutoMap()
|
@AutoMap()
|
||||||
lat: number;
|
lat: number;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue