define the controller and service for the airport management
This commit is contained in:
64
src/airport/airport.service.ts
Normal file
64
src/airport/airport.service.ts
Normal file
@@ -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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user