added the models and the cerate tabels

This commit is contained in:
Jonas Hinterdorfer 2025-03-13 19:00:46 +01:00
parent 7487dec93f
commit ec6b96aadc
9 changed files with 164 additions and 5 deletions

View File

@ -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": {

View File

@ -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")

View File

@ -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)
}
}
}

14
src/models/Airport.ts Normal file
View File

@ -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;
}
}

22
src/models/Flight.ts Normal file
View File

@ -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;
}
}

14
src/models/Passenger.ts Normal file
View File

@ -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;
}
}

14
src/models/Plane.ts Normal file
View File

@ -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;
}
}

15
src/models/Reservation.ts Normal file
View File

@ -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;
}
}

View File

@ -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<void> => {
await Unit.create(true);
res.sendStatus(200);
})
export default router;