implemented task 4
This commit is contained in:
parent
f0d41f5711
commit
d905f7c8ae
@ -11,4 +11,11 @@ Content-Type: application/json
|
|||||||
GET http://localhost:3000/api/passengers/
|
GET http://localhost:3000/api/passengers/
|
||||||
|
|
||||||
### get one
|
### get one
|
||||||
GET http://localhost:3000/api/passengers/123456789
|
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
|
||||||
@ -1,11 +1,14 @@
|
|||||||
import express, {json} from 'express';
|
import express, {json} from 'express';
|
||||||
import flightRouter from "./routes/flight.router";
|
import flightRouter from "./routes/flight.router";
|
||||||
import passengerRouter from "./routes/passenger.router";
|
import passengerRouter from "./routes/passenger.router";
|
||||||
|
import airportRouter from "./routes/airport.router";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(json())
|
app.use(json())
|
||||||
app.use('/api/flights', flightRouter)
|
app.use('/api/flights', flightRouter)
|
||||||
app.use('/api/passengers', passengerRouter)
|
app.use('/api/passengers', passengerRouter)
|
||||||
|
app.use('/api/airports', airportRouter);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.listen(3000, () => {
|
app.listen(3000, () => {
|
||||||
|
|||||||
46
src/routes/airport.router.ts
Normal file
46
src/routes/airport.router.ts
Normal file
@ -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<null, null,null, {icaoCodes : boolean | undefined}>, res: Response) : Promise<void> => {
|
||||||
|
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<void> => {
|
||||||
|
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;
|
||||||
24
src/services/airport.service.ts
Normal file
24
src/services/airport.service.ts
Normal file
@ -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<AirportModel | null>{
|
||||||
|
const sql = `select icao, name, country, runwayLength from Airport where icao = ?1;`
|
||||||
|
const params = {
|
||||||
|
1: icao
|
||||||
|
}
|
||||||
|
return await this.get<AirportModel>(sql, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getAll() : Promise<AirportModel[]>{
|
||||||
|
const sql = `select icao, name, country, runwayLength from Airport;`
|
||||||
|
return await this.all<AirportModel>(sql);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getIcaoCodes() : Promise<string[]>{
|
||||||
|
const sql = `select icao from Airport;`
|
||||||
|
return await this.all<string>(sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user