-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Created seperate converter for coroutes
- Loading branch information
Showing
6 changed files
with
104 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'; | ||
import { plainToClass } from 'class-transformer'; | ||
import { validateOrReject } from 'class-validator'; | ||
import { Airport } from './dto/airport.dto'; | ||
import { CoRouteDto } from './dto/coroute.dto'; | ||
import { Fix } from './dto/fix.dto'; | ||
import { General } from './dto/general.dto'; | ||
import { Navlog } from './dto/navlog.dto'; | ||
|
||
@Injectable() | ||
export class CoRouteConverter { | ||
private readonly logger = new Logger(CoRouteConverter.name) | ||
|
||
convertJsonToDto(parsedObject: any): CoRouteDto { | ||
try { | ||
const tempCoRouteDto = new CoRouteDto(); | ||
const tempNavlog = new Navlog(); | ||
tempCoRouteDto.destination = plainToClass(Airport, parsedObject.OFP.destination); | ||
tempCoRouteDto.origin = plainToClass(Airport, parsedObject.OFP.origin); | ||
tempCoRouteDto.general = plainToClass(General, parsedObject.OFP.general); | ||
parsedObject.OFP.navlog.fix.forEach((item: any) => tempNavlog.fix.push(plainToClass(Fix, item))); | ||
tempCoRouteDto.navlog = tempNavlog; | ||
|
||
return tempCoRouteDto; | ||
} catch (errors) { | ||
const message = 'Failed to instantiate DTO'; | ||
this.logger.warn(message, errors); | ||
throw new HttpException(message, HttpStatus.UNPROCESSABLE_ENTITY); | ||
} | ||
} | ||
|
||
async validateCoRoute(coRoute: CoRouteDto, rteNumber: String = 'PLACEHOLDER') { | ||
try { | ||
await validateOrReject(coRoute, { whitelist: true }); | ||
} catch (errors) { | ||
const message = `${rteNumber} failed validation`; | ||
this.logger.warn(`${rteNumber} failed validation`, errors); | ||
throw new HttpException(message, HttpStatus.UNPROCESSABLE_ENTITY); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CoRouteConverter } from './coroute.converter'; | ||
import { UtilitiesModule } from '../utilities/utilities.module'; | ||
import { CoRouteController } from './coroute.controller'; | ||
import { CoRouteService } from './coroute.service'; | ||
|
||
@Module({ | ||
controllers: [CoRouteController], | ||
providers: [CoRouteService], | ||
providers: [CoRouteService, CoRouteConverter], | ||
imports: [UtilitiesModule], | ||
}) | ||
export class CoRouteModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters