import { AppDataSource } from '../../data-source'
import { NextFunction, Request, Response } from "express"
import { Station } from "../Station/Model"
import { Trainline } from './Model';
import { StationController } from "../Station/Controller";
export class TrainlineController {
readonly manager = AppDataSource.manager;
readonly trainlineRepo = AppDataSource.getRepository(Trainline);
readonly stationRepo = AppDataSource.getRepository(Station);
async addLine(request: Request, response: Response, next: NextFunction) {
try {
const { stations, name } = request.body;
const stationsToAdd = await this.updateStations(stations);
const trainline = this.trainlineRepo.create({ name: name, stations: stationsToAdd });
return this.trainlineRepo.save(trainline);
}
catch (err) {
return response.status(400).json({ error: 'Invalid request' });
}
}
private async updateStations(stationsToAdd: string[]): Promise<Station[]> {
const stations = []
await Promise.all(stationsToAdd.map(async (station) => {
let existingStation = await this.stationRepo.findOne({
where: {
stationName: station,
},
});
if (!existingStation) {
existingStation = this.stationRepo.create({ stationName: station, parents: [], children: [] });
await this.stationRepo.save(existingStation);
}
stations.push(existingStation)
}));
console.log(stations);
for (let i = 0; i < stations.length; i++) {
let existingStation = stations[i];
if (existingStation) {
if (i != 0) {
//concat previous station in line
existingStation.parents = existingStation.parents.concat(stations[i - 1]);
}
await this.stationRepo.save(existingStation);
}
}
return stations
}
async route(request: Request, response: Response, next: NextFunction) {
try {
const origin = request.query.origin as string;
const destination = request.query.destination as string;
const max = await this.stationRepo.count({})
const Q = [origin]
//bfs
let currentStation = await this.stationRepo.findOneBy({ stationName: origin });
while (Q.length >= 0) { // Q is a queue of paths
// (a path is an array of stations as strings taken from origin)
let curr = Q.shift() // curr is the path at top of q, q is shifted
if (curr[-1] == destination) { // if the top of curr is our destination, return
return response.json({ route: curr }) // bfs guarantees shortest path in unweighted undirected cyclic graph
}
let currentStation = await this.stationRepo.findOneBy({ stationName: curr[-1] })
// add all neighbours of curr to q
const rawData = await this.stationRepo.query(`SELECT * FROM trainline_stations_station`)
console.log(rawData)
}
} catch {
return response.status(400).json({ error: 'Invalid request' });
}
}
async remove(request: Request, response: Response, next: NextFunction) {
const name = request.params.name
let trainlineToRemove = await this.trainlineRepo.findOneBy({
name: name,
});
if (!trainlineToRemove) {
return "this trainline not exist"
}
await Trainline.remove(trainlineToRemove)
return "Trainline has been removed"
}
async removeall(request: Request, response: Response, next: NextFunction) {
return Trainline.delete({})
}
async all(request: Request, response: Response, next: NextFunction) {
return Trainline.find()
}
}