define the controller and service for the airport management

This commit is contained in:
Sven Czarnian
2022-10-23 12:53:31 +02:00
부모 440477d741
커밋 a21eddda93
22개의 변경된 파일796개의 추가작업 그리고 1개의 파일을 삭제

파일 보기

@@ -0,0 +1,64 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
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 airportsList(): Promise<string[]> {
return this.airportModel.find({}).then((response) => {
const icaoCodes: string[] = [];
response.forEach((airport) => icaoCodes.push(airport.icao));
return icaoCodes;
});
}
async airport(icao: string): Promise<Airport> {
return this.airportModel.find({ icao }).then((response) => {
if (!response || response.length !== 1) return null;
return response[0];
});
}
async registerAirport(airport: Airport): Promise<boolean> {
this.airportExists(airport.icao).then(async (exists) => {
if (!exists) {
await this.airportModel.create(airport);
}
return !exists;
});
return false;
}
async updateAirport(airport: Airport): Promise<boolean> {
this.airportExists(airport.icao).then(async (exists) => {
if (exists) {
await this.airportModel.findOneAndUpdate(
{ icao: airport.icao },
airport,
);
}
return exists;
});
return false;
}
async deleteAirport(icao: string): Promise<void> {
this.airportModel.deleteOne({ icao });
}
}