From d905f7c8ae1e4eadb631b4591657352dc5f4c4f0 Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Sun, 16 Mar 2025 13:33:07 +0100 Subject: [PATCH] implemented task 4 --- requests.http | 9 ++++++- src/app.ts | 3 +++ src/routes/airport.router.ts | 46 +++++++++++++++++++++++++++++++++ src/services/airport.service.ts | 24 +++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/routes/airport.router.ts create mode 100644 src/services/airport.service.ts diff --git a/requests.http b/requests.http index 289af28..3bea1c1 100644 --- a/requests.http +++ b/requests.http @@ -11,4 +11,11 @@ Content-Type: application/json GET http://localhost:3000/api/passengers/ ### get one -GET http://localhost:3000/api/passengers/123456789 \ No newline at end of file +GET http://localhost:3000/api/passengers/123456789 + + +### get all airports +GET http://localhost:3000/api/airports/?icaoCodes=true + +### get one airports +GET http://localhost:3000/api/airports/EDDF \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 30a27fc..03981f3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,11 +1,14 @@ import express, {json} from 'express'; import flightRouter from "./routes/flight.router"; import passengerRouter from "./routes/passenger.router"; +import airportRouter from "./routes/airport.router"; const app = express(); app.use(json()) app.use('/api/flights', flightRouter) app.use('/api/passengers', passengerRouter) +app.use('/api/airports', airportRouter); + app.listen(3000, () => { diff --git a/src/routes/airport.router.ts b/src/routes/airport.router.ts new file mode 100644 index 0000000..bfda735 --- /dev/null +++ b/src/routes/airport.router.ts @@ -0,0 +1,46 @@ +import express, {Request, Response} from "express"; +import {AirportService} from "../services/airport.service"; +import {StatusCodes} from "http-status-codes"; +import {AirportModel} from "../models/airport.model"; + +const router = express.Router() + +router.get('/', async (req : Request, res: Response) : Promise => { + const icaoCode = req.query.icaoCodes; + + if(icaoCode === undefined || !icaoCode) + { + const airports = await AirportService.getAll(); + + res.status(StatusCodes.OK).json(airports); + return; + } + + else { + const codes = await AirportService.getIcaoCodes(); + res.status(StatusCodes.OK).json(codes); + return ; + } +}); + +router.get('/:icaoCode', async (req : Request<{icaoCode : string}>, res: Response) : Promise => { + const icaoCode: string = req.params.icaoCode; + + if(icaoCode === undefined || icaoCode.length != 4){ + res.sendStatus(StatusCodes.BAD_REQUEST); + return ; + } + + const airport : AirportModel | null = await AirportService.getOne(icaoCode); + + if(airport === null) + { + res.sendStatus(StatusCodes.NO_CONTENT); + } + + res.status(200).json(airport); +}); + + + +export default router; \ No newline at end of file diff --git a/src/services/airport.service.ts b/src/services/airport.service.ts new file mode 100644 index 0000000..e071d60 --- /dev/null +++ b/src/services/airport.service.ts @@ -0,0 +1,24 @@ + +import {ServiceBase} from "./serviceBase"; +import {AirportModel} from "../models/airport.model"; + +export class AirportService extends ServiceBase { + static async getOne(icao : string) : Promise{ + const sql = `select icao, name, country, runwayLength from Airport where icao = ?1;` + const params = { + 1: icao + } + return await this.get(sql, params); + } + + static async getAll() : Promise{ + const sql = `select icao, name, country, runwayLength from Airport;` + return await this.all(sql); + + } + + static async getIcaoCodes() : Promise{ + const sql = `select icao from Airport;` + return await this.all(sql); + } +} \ No newline at end of file