const router = require('express').Router(); const Roads = require('../models/roadsModel'); const Cities = require('../models/citiesModel'); const findShortestPath = require('../../helpers/dijkstra_algo'); router.get('', async (req, res) => { const { start_city_id, end_city_id } = req.query; start = Math.floor(start_city_id); end = Math.floor(end_city_id); try { const cities = await Cities.getCities(); const roads = await Roads.getRoads(); let { path, distance } = findShortestPath(cities, roads, start, end); const shortestPath = formatPath(path, cities) let response = { path: shortestPath, distance } res.status(200).json(response); } catch (e) { res.status(500).json(e); } }) function formatPath(path, cities) { const complete_path = [] for (let path_city of path) { for (let city of cities) { if (city.id == path_city) { complete_path.push({ id: city.id, name: city.name }); } } } return complete_path } module.exports = router;