From abe73cdd8b1476131554e40d0458e262289a4084 Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Tue, 25 Oct 2022 08:26:39 +0200 Subject: [PATCH] introduce an endpoint and service to update the active runway and arrival mode --- src/airport/airport.controller.ts | 36 ++++++++++++++++++++++++++++ src/airport/airport.service.ts | 14 +++++++++++ src/airport/dto/activerunways.dto.ts | 27 +++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/airport/dto/activerunways.dto.ts diff --git a/src/airport/airport.controller.ts b/src/airport/airport.controller.ts index 3c7059c..d5132c4 100644 --- a/src/airport/airport.controller.ts +++ b/src/airport/airport.controller.ts @@ -3,6 +3,7 @@ import { Controller, Get, Delete, + Patch, Post, Query, HttpException, @@ -27,6 +28,7 @@ import { WaypointDto } from '../generic/dto/waypoint.dto'; import { Coordinate } from '../generic/models/coordinate.model'; import { CoordinateDto } from '../generic/dto/coordinate.dto'; import { WaypointConverter } from '../generic/converters'; +import { ActiveRunwaysDto } from './dto/activerunways.dto'; @Controller('airport') export class AirportController { @@ -317,4 +319,38 @@ export class AirportController { await this.airportService.deleteAirport(icao); }); } + + @Patch('/activateRunways') + @ApiBody({ + description: 'The airport definition', + type: ActiveRunwaysDto, + }) + @ApiResponse({ + status: 200, + description: 'The active runways are set', + }) + @ApiResponse({ + status: 404, + description: 'The airport or the runways are not found', + }) + @ApiResponse({ + status: 422, + description: 'The arrival mode is invalid', + }) + async activeRunways( + @Body('runways') runways: ActiveRunwaysDto, + ): Promise { + if (runways.mode !== 'STAGGERED' && runways.mode !== 'IPA') { + throw new HttpException( + 'Invalid arrival mode', + HttpStatus.UNPROCESSABLE_ENTITY, + ); + } + + await this.airportService.activateRunways(runways).then((updated) => { + if (!updated) { + throw new HttpException('Airport not found', HttpStatus.NOT_FOUND); + } + }); + } } diff --git a/src/airport/airport.service.ts b/src/airport/airport.service.ts index 199e6ba..185d783 100644 --- a/src/airport/airport.service.ts +++ b/src/airport/airport.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; +import { ActiveRunwaysDto } from './dto/activerunways.dto'; import { Airport, AirportDocument } from './models/airport.model'; @Injectable() @@ -57,4 +58,17 @@ export class AirportService { async deleteAirport(icao: string): Promise { this.airportModel.deleteOne({ icao }); } + + async activateRunways(runways: ActiveRunwaysDto): Promise { + return this.airportExists(runways.icao).then(async (exists) => { + if (exists) { + await this.airportModel.findOneAndUpdate( + { icao: runways.icao }, + { activeRunways: runways.runways, arrivalMode: runways.mode }, + ); + } + + return exists; + }); + } } diff --git a/src/airport/dto/activerunways.dto.ts b/src/airport/dto/activerunways.dto.ts new file mode 100644 index 0000000..e68456e --- /dev/null +++ b/src/airport/dto/activerunways.dto.ts @@ -0,0 +1,27 @@ +import { IsNotEmpty, IsOptional } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; +import { ConstraintDto } from './constraint.dto'; +import { WaypointDto } from '../../generic/dto/waypoint.dto'; + +export class ActiveRunwaysDto { + @IsNotEmpty() + @ApiProperty({ + description: 'The ICAO code of the airport', + example: 'EDDB', + }) + icao: string; + + @IsNotEmpty() + @ApiProperty({ + description: 'The arrival mode (possible: STAGGERED, IPA)', + example: 'IPA', + }) + mode: string; + + @IsNotEmpty() + @ApiProperty({ + description: 'The active runways', + example: '["25L", "25R"]', + }) + runways: string[]; +}