-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
170 lines (136 loc) · 3.84 KB
/
app.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
var express = require('express'),
fs = require('fs'),
_ = require('underscore'),
ns = require('express-namespace'),
request = require('request'),
db,
conf,
app = express.createServer(
express.logger(),
express.bodyParser(),
express.static(__dirname + '/public'),
express.errorHandler({ dumpExceptions: true, showStack: true })
// express.cookieParser(),
// express.session({ secret: 'blurbzzz'}),
);
//load fixtures
fs.readFile(__dirname + '/fixtures.json', function (err, buffer){
db = JSON.parse(buffer.toString());
//embed user records in item objects
_.each(db.items, function (item) {
var user = _.detect(db.users, function (user) {
return user.id == item.id;
}) || {};
item.user = user;
});
if (err){
throw err;
}
});
//load config variables
fs.readFile(__dirname + '/conf.json', function (err, buffer){
conf = JSON.parse(buffer.toString());
});
function nextId(collection){
return _.max(_.pluck(db[collection], 'id')) + 1;
}
app.get('/login', function(req, res){
res.render(__dirname + '/views/login.ejs');
});
app.post('/login', function(req, res){
res.send('Logged in');
});
app.namespace('/api', function () {
app.get('/count', function(req, res){
res.send('Hello World, we have '+db.users.length + " users and " + db.items.length + " items in our fake db" );
});
// return (paginated) list of all markers
app.get('/search', function(req, res){
// req.get
// Fetch x markers from db
// Write JSON string to res
var data = ('type' in req.query) ?
_.select(db.items, function (item) {
return item.type === req.query.type;
}) : db.items;
res.contentType("application/json");
res.send(data);
});
app.post('/message', function (req, res) {
var data = {
uname: conf.txtlocalUserid,
pword: conf.txtlocalPassword,
selectednums: '447941192398',
from: 'TouchyPeely',
message: req.body.message,
info: 1,
test: 0
};
var body = '';
_.each(data, function (value, attribute) {
var val = attribute === 'message' ? encodeURIComponent(value) : value;
body += attribute + "=" + val + '&';
});
request({
uri: 'http://www.txtlocal.com/sendsmspost.php?'+body,
method: 'GET',
headers: {
'ContentType': 'application/x-www-form-urlencoded'
},
body: ""
}, function (err, res2, data) {
res.send(data);
});
});
// Save marker to
app.post('/items', function(req, res){
// Authenticate user
// Validate the data
// Create a new item model with data
// return JSON representation of model
var description = req.body.description,
user = _.detect(db.users, function (user) {
return user.id == req.body.user;
}),
type = req.body.type,
createdAt = Date.now(),
lat = req.body.lat,
lng = req.body.lng,
newItem = {
id: nextId('items'),
description: description,
user: user,
type: type,
created_at: createdAt,
lat: lat,
lng: lng,
available: true
};
db.items.push(newItem);
res.contentType("application/json");
res.send(newItem);
});
// Update marker to
app.put('/items/:id', function(req, res){
// Authenticate user
// Check item exists
// Validate the data
// Update item model with data
// return JSON representation of model
});
// Delete marker
app.del('/items/:id', function(req, res){
// Authenticate user
// Check item exists
// Delete item
// Return 204
var item = _.detect(db.items, function (item, index) {
return item.id == req.params.id;
});
if (item) {
db.items.splice(parseInt(item.id, 10),1);
}
res.send('ok');
});
});
app.listen(3000);