-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
191 lines (152 loc) · 4.08 KB
/
server.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var express = require('express.io');
var server = express().http().io();
// For parameters
server.use(express.urlencoded());
// Import models
var localModel = require("./model/local");
var ratingModel = require("./model/rating");
// TODO Create funtions in vars, routing , auth, cath erros, error response codes.
/**
* POST LOCAL INTO DATABASE
*/
server.post("/locals", function(req,res){
console.log("Adding local");
// Parameters
var name = req.body.name
var city = req.body.city
var stars = req.body.stars
var company = req.body.company
var imageUrl = req.body.imageUrl
var lattitude = req.body.lattitude
var longittude = req.body.longittude
//|| typeof date == 'undefined'
if ( typeof name == 'undefined' ){
res.status(400).send("Missing some parameters");
return
}
// Create local
var local = new localModel({
name : name,
city : city,
stars : stars,
company: company,
imageUrl: imageUrl,
geo : [longittude,lattitude]
});
// Save the local in database
local.save(function(error){
if(error){
console.log(error);
res.status(500).send("Error when saving into database");
}
res.status(200).send(local);
});
});
/**
* GET A LIST OF HOTELS
*/
server.get('/locals', function(req, res) {
localModel.find({}, function (error, hotels) {
if(error){
console.log(error);
res.status(404).send("Local not found");
}
res.send(hotels);
});
});
/**
* SEARCH HOTEL BY NEAR OF POINT IN KM
*/
server.get('/locals/near', function(req,res){
var lattitude = req.param('lattitude')
var longittude = req.param('longittude')
var distance = req.param('distance')
console.log(lattitude)
console.log(longittude)
var locals = new localModel({geo: [longittude,lattitude]});
locals.findNear(distance,function(error,results) {
if(error) {
console.log(error);
res.status(404).send("Local not found");
}
res.send(results);
});
});
/**
* Update local
*/
server.put("/locals/:id", function(req,res){
// Parameters
var id = req.param('id');
var name = req.body.name
var city = req.body.city
var stars = req.body.stars
var company = req.body.company
var imageUrl = req.body.imageUrl
var lattitude = req.body.lattitude
var longittude = req.body.longittude
localModel.findById(req.param('id'),function(error,result){
result.name = name;
result.city = city;
result.stars = stars;
result.company = company;
result.imageUrl = imageUrl;
result.latitude = lattitude;
result.longitude = longittude;
// Save the local in database
result.save(function(error) {
if(error) {
console.log(error);
res.status(500).send("Error when save in database");
}
res.status(200).send(result);
});
});
});
/**
* Get local by id
*/
server.get("/locals/:id",function(req,res){
var id = req.param('id');
localModel.find({_id:req.param('id')},function(error,result){
if(error){
console.log(error);
res.status(404).send("Local not found");
}
res.send(result);
});
});
/**
* Get local ratings
*/
server.get('/locals/:id/comments', function(req,res){
ratingModel.find({local:req.param('id')},function(error,result){
if(error){
console.log(error);
res.status(404).send("Local not found");
}
res.send(result);
})
});
/**
* Add rating to local
*/
server.post('/locals/:id/comments', function(req,res){
var rating = req.param('rating')
var comment = req.param('comment')
var rating = new ratingModel({
rating: rating,
comment: comment,
local: req.param('id')
});
// save rating
rating.save(function(error) {
if(error) {
console.log(error);
res.status(500).send("Error saving comment")
}
res.send(rating);
});
});
console.log("Server started");
server.listen(9999);