introduce the database and controller connections for all inbounds
This commit is contained in:
61
src/inbound/inbound.service.ts
Normal file
61
src/inbound/inbound.service.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
import { Inbound, InboundDocument } from './models/inbound.model';
|
||||
|
||||
@Injectable()
|
||||
export class InboundService {
|
||||
constructor(
|
||||
@InjectModel('inbound')
|
||||
private readonly inboundModel: Model<InboundDocument>,
|
||||
) {}
|
||||
|
||||
async callsignKnown(callsign: string): Promise<boolean> {
|
||||
return this.inboundModel
|
||||
.findOne({ callsign })
|
||||
.then((inbound) => inbound !== null);
|
||||
}
|
||||
|
||||
async add(inbound: Inbound): Promise<boolean> {
|
||||
return this.callsignKnown(inbound.callsign).then(async (known) => {
|
||||
if (known) return false;
|
||||
|
||||
// modify the inbound data
|
||||
inbound.plan = undefined;
|
||||
inbound.controllerData.reportedTime = new Date().toUTCString();
|
||||
|
||||
await this.inboundModel.create(inbound);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
async remove(callsign: string): Promise<boolean> {
|
||||
return this.callsignKnown(callsign).then(async (known) => {
|
||||
if (!known) return false;
|
||||
await this.inboundModel.deleteOne({ callsign });
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
async update(inbound: Inbound): Promise<boolean> {
|
||||
return this.callsignKnown(inbound.callsign).then(async (known) => {
|
||||
if (!known) return false;
|
||||
|
||||
// modify the inbound data
|
||||
inbound.plan = undefined;
|
||||
inbound.controllerData.reportedTime = new Date().toUTCString();
|
||||
|
||||
await this.inboundModel.findOneAndUpdate(
|
||||
{ callsign: inbound.callsign },
|
||||
inbound,
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
async airportInbounds(icao: string): Promise<Inbound[]> {
|
||||
return this.inboundModel.find({ destination: icao });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user