Skip to content

Commit e98f4e5

Browse files
committed
Changes made togetTravelTime Method
1 parent f9e6fa3 commit e98f4e5

File tree

3 files changed

+138
-9
lines changed

3 files changed

+138
-9
lines changed

src/db/mongoose.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const mongoose = require('mongoose')
22

3-
mongoose.connect('mongodb://127.0.0.1:27017/dine-out-api', {
3+
mongoose.connect(process.env.MongoDBURL, {
44
useNewUrlParser: true,
55
useCreateIndex: true
66
}, (error) => {

src/googleAPI/distanceMatrix.js

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,102 @@ const getTravelTime = function (user, restaurant) {
4343

4444
}
4545

46-
module.exports = getTravelTime
46+
47+
48+
const getTravelTimeRecursive = async function(user, restaurantsArray, callback) {
49+
50+
function iterate(index) {
51+
if (index == restaurantsArray.length) {
52+
return callback()
53+
}
54+
55+
const userLat = user.address[0].lat
56+
const userLng = user.address[0].lng
57+
58+
const restLat = restaurantsArray[index].address.location.lat
59+
const restLng = restaurantsArray[index].address.location.lng
60+
61+
var options = {
62+
uri: "https://maps.googleapis.com/maps/api/distancematrix/json",
63+
qs: {
64+
origins: `${userLat},${userLng}`,
65+
destinations: `${restLat},${restLng}`,
66+
key: process.env.googleAPIKey
67+
},
68+
json: true
69+
}
70+
71+
rp(options).then((body) => {
72+
73+
console.log(body.rows[0].elements[0])
74+
restaurantsArray[index].distanceMatrix.distance = body.rows[0].elements[0].distance.text
75+
restaurantsArray[index].distanceMatrix.duration = body.rows[0].elements[0].duration.text
76+
77+
iterate(index + 1)
78+
79+
}).catch((err) => {
80+
console.log(err)
81+
82+
callback(err)
83+
})
84+
85+
}
86+
87+
iterate(0)
88+
}
89+
90+
const getTravelTimeParralel = function(user, restaurantsArray, callback) {
91+
92+
// let completed = 0, hasErrors = false
93+
94+
// function done(err) {
95+
// if(err) {
96+
// hasErrors = true;
97+
// return callback(err);
98+
// }
99+
100+
// if(++completed === links.length && !hasErrors) {
101+
// return callback();
102+
// }
103+
// }
104+
105+
restaurantsArray.forEach(restaurant => {
106+
const userLat = user.address[0].lat
107+
const userLng = user.address[0].lng
108+
109+
const restLat = restaurant.address.location.lat
110+
const restLng = restaurant.address.location.lng
111+
112+
console.log({userLat, userLng})
113+
console.log({restLat, restLng})
114+
115+
var options = {
116+
uri: "https://maps.googleapis.com/maps/api/distancematrix/json",
117+
qs: {
118+
origins: `${userLat},${userLng}`,
119+
destinations: `${restLat},${restLng}`,
120+
key: process.env.googleAPIKey
121+
},
122+
json: true
123+
}
124+
125+
rp(options).then((body) => {
126+
127+
console.log(body.rows[0].elements[0])
128+
restaurant.distanceMatrix.distance = body.rows[0].elements[0].distance.text
129+
restaurant.distanceMatrix.duration = body.rows[0].elements[0].duration.text
130+
131+
callback()
132+
133+
}).catch((err) => {
134+
console.log(err)
135+
136+
callback(err)
137+
138+
})
139+
});
140+
}
141+
142+
module.exports = { getTravelTime,
143+
getTravelTimeRecursive,
144+
getTravelTimeParralel }

src/routers/restaurant.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const express = require('express')
22
const Restaurant = require('../model/restaurant')
33
const userauth = require('../middleware/auth')
44
const router = new express.Router()
5-
const getTravelTime = require('../googleAPI/distanceMatrix')
5+
const { getTravelTimeParralel } = require('../googleAPI/distanceMatrix')
66

77
router.post('/restaurant', async (req, res) => {
88
const restaurant = new Restaurant( req.body )
@@ -63,17 +63,48 @@ router.get('/restaurants/location', userauth, async (req, res) => {
6363
if (!restaurants) {
6464
throw new Error( {error: "No results Found"})
6565
}
66+
67+
//This code was for parallel implementation using getTravelTimeParallel... much faster than sequential implementation
68+
69+
let completed = 0, hasErrors = false
6670

67-
let modifiedRestaurants = []
71+
function done(err) {
72+
if(err) {
73+
hasErrors = true;
74+
throw new Error(err);
75+
}
6876

69-
for (let i = 0; i < restaurants.length; i++) {
70-
const newRest = await getTravelTime(req.user, restaurants[i])
71-
modifiedRestaurants.push(newRest)
77+
if(++completed === restaurants.length && !hasErrors) {
78+
res.send(restaurants)
79+
}
7280
}
81+
7382

83+
getTravelTimeParralel(req.user, restaurants, done)
84+
85+
//This code was for sequential non-recursive implementation using getTravelTime
86+
87+
// let modifiedRestaurants = []
88+
89+
// for (let i = 0; i < restaurants.length; i++) {
90+
// const newRest = await getTravelTime(req.user, restaurants[i])
91+
// modifiedRestaurants.push(newRest)
92+
// }
93+
94+
// console.log(modifiedRestaurants)
95+
// res.send(modifiedRestaurants)
96+
97+
98+
//This code was for sequential recursive implementation using getTravelTimeRecursive
99+
100+
// getTravelTimeRecursive(req.user, restaurants, (err) => {
101+
// if (err) {
102+
// throw new Error(err)
103+
// }
104+
105+
// res.send(restaurants)
106+
// })
74107

75-
console.log(modifiedRestaurants)
76-
res.send(modifiedRestaurants)
77108
} catch (e) {
78109
res.status(400).send(e)
79110
}

0 commit comments

Comments
 (0)