-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
94 lines (82 loc) · 2.43 KB
/
app.js
File metadata and controls
94 lines (82 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const express=require('express');
const app=express();
const bodyParser=require('body-parser');
const Restraunt=require('./models/schema');
const connectDB=require('./connection/connection')
const hostname='127.0.0.1';
const PORT= process.env.PORT||2000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.post('/',async(req,res)=>{
try{
// const name=req.body.restrauntName;
// const desc=req.body.description;
// req.body.location.coordinates=req.body.location;
// const rating=req.body.rating;
// console.log(typeof(coord))
// console.log(coord)
console.log(req.body);
const newRest=await Restraunt.create(req.body);
// Restraunt.index({restrauntName:"restro1"},()=>{
// console.log("yes sir")
// })
newRest.save();
// Restraunt.index({location : { type: 'Point', coordinates: [ 79.3975885, 28.3881653 ] }})
res.json({
status:"SUCESS",
message:"Restraunt Registered Successfully"
})
}
catch(err){
console.log(err)
res.json({
status:"FAILED",
message:"Sorry ,Something Wrong Happened"
})
}
})
app.post('/get',async (req,res)=>{
const rest=await Restraunt.find();
// console.log("HELOOOO")
// const rest2=await Restraunt.find({
// location: {
// $near: {
// $geometry: {
// type: "Point" ,
// coordinates: [ req.body.lattitude,req.body.longitude ]
// },
// $maxDistance: 500,
// $minDistance:10
// }
// }
// })
// // console.log(rest);
const rest2= Restraunt.find([
{
$geoNear:{
near:{type:'Point',coordinates:[parseFloat(req.body.longitude),parseFloat(req.body.lattitude)]},
key:'location',
maxDistance:500,
spherical:true
}
}
])
var sum;
var count=0;
rest2.forEach(element => {
sum=sum+element.rating;
count=count+1;
});
var avg=sum/count;
res.json({Name:rest2.restrauntName,
Description:rest2.description,
Location:[rest2.location.coordinates[0] , rest2.location.coordinates[1]],
avgRating:avg,
Ratings:count
});
// res.send("DONE");
})
app.listen(PORT, async ()=>{
await connectDB(process.env.MONGODB_URL)
console.log(`server istening on port http://${hostname}:${PORT}`)
})