41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
const router = require('express').Router();
|
|
|
|
const Roads = require('../models/roadsModel');
|
|
const Cities = require('../models/citiesModel');
|
|
|
|
const searchPath = require('../../helpers/dijkstra_algo');
|
|
|
|
router.get('', async (req, res) => {
|
|
const { start_city_id, end_city_id } = req.body;
|
|
|
|
try {
|
|
const cities = await Cities.getCities();
|
|
const roads = await Roads.getRoads();
|
|
|
|
let { path, distance } = searchPath(cities, roads, start_city_id, end_city_id);
|
|
const formatedPath = formatPath(path, cities)
|
|
|
|
let response = { path: formatedPath, 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;
|