82 linhas
2.2 KiB
TypeScript
82 linhas
2.2 KiB
TypeScript
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<AirportDocument>,
|
|
) {}
|
|
|
|
private async airportExists(icao: string): Promise<boolean> {
|
|
return this.airportModel
|
|
.find({ icao })
|
|
.then((response) => response && response.length !== 0);
|
|
}
|
|
|
|
async allAirports(): Promise<AirportOverviewDto[]> {
|
|
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<Airport> {
|
|
return this.airportModel.findOne({ icao }).then((response) => {
|
|
if (!response) return null;
|
|
return response;
|
|
});
|
|
}
|
|
|
|
async registerAirport(airport: Airport): Promise<boolean> {
|
|
return this.airportExists(airport.icao).then(async (exists) => {
|
|
if (!exists) {
|
|
await this.airportModel.create(airport);
|
|
}
|
|
|
|
return !exists;
|
|
});
|
|
}
|
|
|
|
async updateAirport(airport: Airport): Promise<boolean> {
|
|
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<void> {
|
|
this.airportModel.deleteOne({ icao });
|
|
}
|
|
|
|
async activateRunways(runways: ActiveRunwaysDto): Promise<boolean> {
|
|
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;
|
|
});
|
|
}
|
|
}
|