yday's

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.3 kB
1
Indexable
Never
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 Promise.all(
                stations.map((station) => this.updateStations(station)),
            );
            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(name: string): Promise<Station> {
        const existingStation = await this.stationRepo.findOne({
            where: {
                stationName: name,
            },
        });
        if (existingStation) {
            return existingStation;
        }
        return this.stationRepo.save({ stationName: name });
    }

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

}