2023-03-29 21:15:33 +01:00

190 lines
3.8 KiB
JavaScript

var express = require('express');
var app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
/*
Flight:
"name"
"take-off time"
"priority"
"status"
//*/
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function Flight( name ) {
this.id = uuidv4();
this.name = name;
this.status = "unknown";
this.takeoffTime = -1;
}
Flight.prototype.getData = function() {
return {
"id" : this.id,
"name" : this.name,
"status" : this.status,
"takeoffTime" : this.takeoffTime
};
}
Flight.prototype.setStatus = function( status ) {
if ( [ "unknown", "checkedin", "readytotaxi", "clearedtotaxi", "halted", "terminated" ].indexOf( status ) < 0 ) {
return "Invalid status";
}
this.status = status;
return true;
}
Flight.prototype.setTakeoffTime = function( takeoffTime ) {
if ( takeoffTime === "" || takeoffTime === -1 ) {
this.takeoffTime = -1;
}
if ( isNaN( takeoffTime ) ) {
return "Invalid takeoff time"
}
this.takeoffTime = parseInt( takeoffTime );
return true;
}
function ATCDataHandler( data ) {
this.data = data;
}
ATCDataHandler.prototype.addFlight = function( flight ) {
if ( flight instanceof Flight === false ) {
throw new Error( "Given flight is not an instance of Flight" );
}
this.data.flights[ flight.id ] = flight;
}
ATCDataHandler.prototype.deleteFlight = function( flightId ) {
delete this.data.flights[ flightId ];
}
ATCDataHandler.prototype.getFlight = function( flightId ) {
return this.data.flights[ flightId ] || false;
}
ATCDataHandler.prototype.getFlights = function() {
return this.data.flights;
}
const dataHandler = new ATCDataHandler( {
"flights": {}
} );
/**************************************************************************************************************/
// Endpoints
/**************************************************************************************************************/
app.get( "/flight", ( req, res ) => {
res.json( dataHandler.getFlights() );
});
app.patch( "/flight/:flightId", ( req, res ) => {
const flightId = req.params.flightId;
const flight = dataHandler.getFlight( flightId );
if ( !flight ) {
res.status( 400 ).send( `Unrecognised flight ID (given: "${req.params.flightId}")` );
}
if ( req.body.status ) {
const statusChangeSuccess = flight.setStatus( req.body.status );
if ( statusChangeSuccess !== true ) {
res.status( 400 ).send( statusChangeSuccess );
}
}
if ( req.body.hasOwnProperty( "takeoffTime" ) ) {
const takeoffChangeSuccess = flight.setTakeoffTime( req.body.takeoffTime );
if ( takeoffChangeSuccess !== true ) {
res.status( 400 ).send( takeoffChangeSuccess );
}
}
res.json( flight.getData() );
});
app.post( "/flight", ( req, res ) => {
if ( !req.body.name ) {
res.status( 400 ).send( "Invalid/missing flight name" );
}
const flight = new Flight( req.body.name );
dataHandler.addFlight( flight );
res.status( 201 );
res.json( flight.getData() );
});
app.delete( "/flight/:flightId", ( req, res ) => {
const flight = dataHandler.getFlight( req.params.flightId );
if ( !flight ) {
res.status( 400 ).send( `Unrecognised flight ID (given: "${req.params.flightId}")` );
}
dataHandler.deleteFlight( req.params.flightId );
res.status( 204 ).send( "" );
});
module.exports = app;