Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
263 lines (202 loc) · 6.89 KB
/
Copy pathserver.js
File metadata and controls
263 lines (202 loc) · 6.89 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const express = require('express');
const fs = require('fs');
const sqlite = require('sql.js');
const uuid = require( 'uuid' );
const app = express();
const bodyParser = require( 'body-parser');
// Require the driver.
var pg = require('pg');
var connectionString = process.env.DATABASE_URL || 'postgresql://root@localhost:26257?sslmode=disable';
var client = new pg.Client(connectionString);
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(allowCrossDomain);
app.use( bodyParser());
app.set('port', (process.env.PORT || 3001));
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
const COLUMNS = [
'carbohydrate_g',
'protein_g',
'fa_sat_g',
'fa_mono_g',
'fa_poly_g',
'kcal',
'description',
];
var config = {
user: 'maxroach',
host: 'localhost',
database: 'healthi',
port: 26257
};
//FOOD
//GET food item suggestions from foodTable
app.get('/foodSuggestions', function(req, res){
try{
console.log( 'call food suggestions!!!!' );
// Get a Postgres client from the connection pool
pg.connect( config, function(err, client, done) {
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({ success: false, data: err});
}
const foodItems = [];
//CHANGE THE BELOW QUERY
var query = client.query("SELECT name FROM healthi.foodItem;" );
query.on('row', function(row) {
foodItems.push( row );
});
query.on('end', function() {
console.log( 'foodItems:', JSON.stringify( foodItems ) );
res.send(foodItems);
});
query.on('error', function(err) {
console.log(err);
res.status(500).json({ success: false, data: err});
done();
});
});
} catch (ex) {
console.log( ex );
}
});
//USER LOGIN
app.post('/userPref', function(req, res){
try{
//ANNA PUT YOUR STUFF HERE
} catch (ex) {
}
});
//USER
//posting each preference for food on SIGNUP for USER
//save things as JSON object, which we can stringify and SAVE in table, then when retrieved
//it can be turned back into a json object
app.post('/userPref', function(req, res){
try{
// Grab data from http request
var data = {faveCuisine: req.body.faveCuisine,
dietRestriction: req.body.dietRestriction,
dietGoals: req.body.dietGoals};
// Get a Postgres client from the connection pool
pg.connect(connectionString, function(err, client, done) {
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({ success: false, data: err});
}
// Insert food preference for user
client.query("INSERT INTO healthi.user.foodPref(name, balance) VALUES($1, $2);", [data.faveCuisine,
data.dietRestriction,
data.dietGoals], function (err, result) {
done();
res.send();
if (err) {
return console.error('error happened during query', err)
}
});
});
} catch (ex) {
callback(ex);
}
});
app.get('/api/food', (req, res) => {
const param = req.query.q;
if (!param) {
res.json({
error: 'Missing required parameter `q`',
});
return;
}
// WARNING: Not for production use! The following statement
// is not protected against SQL injections.
const r = db.exec(`
select ${COLUMNS.join(', ')} from entries
where description like '%${param}%'
limit 100
`);
if (r[0]) {
res.json(
r[0].values.map((entry) => {
const e = {};
COLUMNS.forEach((c, idx) => {
// combine fat columns
if (c.match(/^fa_/)) {
e.fat_g = e.fat_g || 0.0;
e.fat_g = (
parseFloat(e.fat_g, 10) + parseFloat(entry[idx], 10)
).toFixed(2);
} else {
e[c] = entry[idx];
}
});
return e;
}),
);
} else {
res.json([]);
}
});
app.post( '/logIn', function( req, res ){
const username = req.body.username;
const password = req.body.password;
pg.connect( config, function( err, client, done ){
var query = client.query( `SELECT * FROM healthi.user where username='${username}';` );
query.on( 'error', ( err )=>{
return res.status( 500 ).json( { success: false, statusText: 'application error' } );
});
let user;
query.on( 'row', ( row )=>{
user = row;
} );
query.on( 'end', ()=>{
if( !user ){
return res.status( 500 ).json( { success: false, statusText: 'user does not exist' } );
}
console.log( user.password );
console.log( password );
if( user.password === password ){
return res.status(200).json({ success: true, statusText: 'successfully logged in', user: user } );
} else {
return res.status(500).json({ success: false, statusText: 'password incorrect'} );
}
} )
})
});
app.post( '/sign-up', function( req, res ){
const id = uuid.v4();
const username = req.body.username;
const password = req.body.password;
pg.connect( config, function( err, client, done ){
var query = client.query( `INSERT INTO healthi.user VALUES ('${id}','${username}','${password}', null, null, null, null);`);
query.on( 'error',( err )=>{
console.log( err );
if( err.code === '23505' )
return res.status(500).json( { success: false, statusText: 'username taken' } );
else
return res.status(500).json( { success: false, statusText: 'application error'})
} );
let retVal;
query.on('row', (row)=>{
retVal = row;
});
query.on('end',()=>{
res.status( 200 ).json({ success: true, user: retVal ? retVal : { id, username, password } });
})
});
}
/* console.log( JSON.stringify( req.body ));
return res.status(400).json({success: true, data: 'hello'}).send();*/
);
app.listen(app.get('port'), () => {
console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});