diff --git a/package.json b/package.json index f1f7963..9880ff8 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "start": "ts-node src/index.ts" + "start": "ts-node src/app.ts" }, "repository": { "type": "git", @@ -15,6 +15,7 @@ "devDependencies": { "@types/express": "^5.0.0", "http-status-codes": "^2.3.0", + "ts-node": "^10.9.2", "typescript": "^5.8.2" }, "dependencies": { diff --git a/src/app.ts b/src/app.ts index f401f6b..7e94bea 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,10 @@ -import express from 'express'; +import express, {json} from 'express'; +import flightRouter from "./routes/flight-router"; const app = express(); +app.use(json()) +app.use('/api/flights', flightRouter) + app.listen(3000, () => { console.log("Started Server on Port 3000") diff --git a/src/database/data.ts b/src/database/data.ts index d1984d2..5019eb5 100644 --- a/src/database/data.ts +++ b/src/database/data.ts @@ -1,5 +1,5 @@ -import {Database,open} from "sqlite"; -import sqlite3 from "sqlite"; +import {Database, open} from "sqlite"; +import sqlite3 from "sqlite3"; export class DB { @@ -9,6 +9,7 @@ export class DB { driver: sqlite3.Database }); await connection.exec('PRAGMA foreign_keys = ON;'); + await this.createTables(connection); return connection; } @@ -16,7 +17,6 @@ export class DB { await db.exec("begin transaction;"); } - static async commitTransaction(db: Database) { await db.exec("commit;") } @@ -24,4 +24,65 @@ export class DB { static async rollbackTransaction(db: Database) { await db.exec("rollback;") } + + private static async createTables(db: Database){ + await this.beginTransaction(db); + try{ + await db.exec(`create table if not exists Airport ( + icao TEXT NOT NULL, + name TEXT NOT NULL, + country TEXT NOT NULL, + runwayLength TEXT NOT NULL, + CONSTRAINT pk_airport PRIMARY KEY (icao) + )`); + + await db.exec(`create table if not exists Plane + ( + tailNo Text Not Null, + model Text not null, + manufacturer text not null, + capacity int not null, + constraint pk_plane primary key (tailNo) + );`) + + await db.exec(`create table if not exists Flight +( + flightNo TEXT not null , + departure text not null, + arrival text, + operates Text not null, + departs text not null, + arrives text not null, + constraint pk_plane primary key (flightNo), + constraint fk_arrives foreign key (arrives) references Airport (icao), + constraint fk_departs foreign key (arrives) references Airport (icao) +);`); + await db.exec(` create table if not exists Passenger +( + ssn TEXT not null, + name TEXT not null, + dateOfBirth text not null, + isFemale boolean, + + constraint pk_passenger primary key (ssn) +); +`); + await db.exec(`create table if not exists Reservation + ( + ticketNo INTEGER not null primary key autoincrement, + seat TEXT not null, + flight TEXT not null, + passenger TEXT not null, + constraint fk_flight foreign key (flight) references Flight (flightNo), + constraint fk_passenger foreign key (passenger) references Passenger (ssn) + );`); + await this.commitTransaction(db); + } + + catch (error){ + console.error(error); + await this.rollbackTransaction(db) + } + + } } \ No newline at end of file diff --git a/src/models/Airport.ts b/src/models/Airport.ts new file mode 100644 index 0000000..33202f7 --- /dev/null +++ b/src/models/Airport.ts @@ -0,0 +1,14 @@ +export class Airport{ + public icao : string; + public name : string; + public country: string; + public runwayLength: number; + + + constructor(icao: string, name: string, country: string, runwayLength: number) { + this.icao = icao; + this.name = name; + this.country = country; + this.runwayLength = runwayLength; + } +} \ No newline at end of file diff --git a/src/models/Flight.ts b/src/models/Flight.ts new file mode 100644 index 0000000..a7e4f81 --- /dev/null +++ b/src/models/Flight.ts @@ -0,0 +1,22 @@ +import {Plane} from "./Plane"; +import {Airport} from "./Airport"; + +export class Flight { + public flightNo : string; + public departure : Date; + public departs : Airport; + public arrives : Airport; + public arrival : Date | null; + public operates : Plane; + + + constructor(flightNo: string, operates: Plane, departs: Airport, arrives : Airport, + departure: Date, arrival: Date | null = null) { + this.flightNo = flightNo; + this.departure = departure; + this.departs = departs; + this.arrives = arrives; + this.arrival = arrival; + this.operates = operates; + } +} \ No newline at end of file diff --git a/src/models/Passenger.ts b/src/models/Passenger.ts new file mode 100644 index 0000000..86c3a29 --- /dev/null +++ b/src/models/Passenger.ts @@ -0,0 +1,14 @@ +export class Passenger{ + public ssn: string + public name: string + public dateOfBirth: Date + public isFemale: boolean | null + + + constructor(ssn: string, name: string, dateOfBirth: Date, isFemale: boolean | null = null) { + this.ssn = ssn; + this.name = name; + this.dateOfBirth = dateOfBirth; + this.isFemale = isFemale; + } +} \ No newline at end of file diff --git a/src/models/Plane.ts b/src/models/Plane.ts new file mode 100644 index 0000000..79023bc --- /dev/null +++ b/src/models/Plane.ts @@ -0,0 +1,14 @@ +export class Plane{ + public tailNo : string; + public model : string; + public manufacturer : string; + public capacity : number; + + + constructor(tailNo: string, model: string, manufacturer: string, capacity: number) { + this.tailNo = tailNo; + this.model = model; + this.manufacturer = manufacturer; + this.capacity = capacity; + } +} \ No newline at end of file diff --git a/src/models/Reservation.ts b/src/models/Reservation.ts new file mode 100644 index 0000000..924afc2 --- /dev/null +++ b/src/models/Reservation.ts @@ -0,0 +1,15 @@ +import {Flight} from "./Flight"; + +export class Reservation{ + public ticketNo : number = -1; + public seat : string; + public flight : Flight; + public passenger : Reservation; + + + constructor(seat: string, flight: Flight, passenger: Reservation) { + this.seat = seat; + this.flight = flight; + this.passenger = passenger; + } +} \ No newline at end of file diff --git a/src/routes/flight-router.ts b/src/routes/flight-router.ts new file mode 100644 index 0000000..fe39895 --- /dev/null +++ b/src/routes/flight-router.ts @@ -0,0 +1,14 @@ + +import express, {Request, Response} from "express"; +import {Unit} from "../database/Unit"; + +const router = express.Router(); + + + +router.get('/', async (req : Request , res:Response) : Promise => { + await Unit.create(true); + res.sendStatus(200); +}) + +export default router; \ No newline at end of file