import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { ActiveRunwaysDto } from './dto/activerunways.dto'; import { AirportOverviewDto } from './dto/airportoverview.dto'; import { Airport, AirportDocument } from './models/airport.model'; @Injectable() export class AirportService { constructor( @InjectModel('airport') private readonly airportModel: Model, ) {} private async airportExists(icao: string): Promise { return this.airportModel .find({ icao }) .then((response) => response && response.length !== 0); } async allAirports(): Promise { return this.airportModel.find({}).then((response) => { const retval: AirportOverviewDto[] = []; response.forEach((airport) => retval.push({ icao: airport.icao, name: airport.name, flightInformationRegion: airport.flightInformationRegion, }), ); return retval; }); } async airport(icao: string): Promise { return this.airportModel.findOne({ icao }).then((response) => { if (!response) return null; return response; }); } async registerAirport(airport: Airport): Promise { return this.airportExists(airport.icao).then(async (exists) => { if (!exists) { await this.airportModel.create(airport); } return !exists; }); } async updateAirport(airport: Airport): Promise { return this.airportExists(airport.icao).then(async (exists) => { if (exists) { await this.airportModel.findOneAndUpdate( { icao: airport.icao }, airport, ); } return exists; }); } 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; }); } }