-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
221 lines (162 loc) · 5.53 KB
/
index.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
192
var express = require('express');
var app = express();
var pg = require('pg');
var bodyParser = require('body-parser');
var session = require('express-session')
var {Pool} = require('pg') // that gets the Pool attribute from the full pg module
//Pool.defaults.ssl = true;
var gmaps = require('@google/maps').createClient({
key: 'my key'
});
app.use(session({
secret: 'Achiever',
resave: false,
saveUninitialized: true
}))
if (process.env.DATABASE_URL != '')
{
var connectionString = process.env.DATABASE_URL;
}
// else {
// var connectionString = 'postgres://postgres@localhost/gonning';
// }
const pool = new Pool({connectionString});
/** bodyParser.urlencoded(options)
* Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST)
* and exposes the resulting object (containing the keys and values) on req.body
*/
app.use(bodyParser.urlencoded({
extended: true
}));
/**bodyParser.json(options)
* Parses the text as JSON and exposes the resulting object on req.body.
*/
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('pages/index');
});
app.post('/checkUser', function(request, response){
console.log('USING POST METHOD');
handleRequest(request, response);
});
//for clicking on it
app.get('/checkUser', function(request, response){
console.log('USING GET METHOD');
handleRequest(request, response);
});
app.get('/create', function(request, response){
console.log('USING GET METHOD for create');
response.render('pages/create-goal');
});
function handleRequest(request, response) {
if (request.body.username != null && request.body.password != null) {
request.session.user = request.body.username;
request.session.pass = request.body.password;
console.log(request.body.username, request.body.password);
console.log(request.session.user, request.session.pass);
}
if (request.session.user != "" && request.session.pass != "") {
const pass = request.session.pass;
const username = request.session.user;
pool.query('SELECT * FROM "users" WHERE user_name = $1 and password = $2', [username, pass], (err, res) => {
if (err) {
console.log("User does not exist");
request.session.destroy();
throw err;
} else {
if (res.rows[0].user_name != username || res.rows[0].password != pass) {
request.session.destroy();
response.render('pages/index');
}
var user = res.rows[0];
request.session.uid = res.rows[0].id;
console.log("Is this the right id? " + request.session.uid);
console.log(user);
showGoals(response, user);
}
})
} else {
response.render('pages/index');
}
}
function showGoals(response, user) {
pool.query('SELECT * FROM "goals" WHERE user_id = $1', [user.id], (err, res) => {
if (err) {
throw err
} else {
var goals = [];
for (i = 0; i < res.rows.length; i++) {
goals.push(res.rows[i]);
}
var params = {user: user, goals: goals};
response.render('pages/goals', params);
}
})
}
app.get('/map', function(request, response){
var place = 'rexburg walmart';//'1600 Amphitheatre Parkway, Mountain View, CA';
gmaps.geocode({
address: place
}, function(err, response) {
if (!err) {
console.log("This works");
console.log(response.json.results);
//response.json.gmaps;
res.writeHead(200, {"Content-Type": "application/json"});
res.write('{ "name":"Jason Fitzgerald","class":"cs313" }');
console.log("This works");
}
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
//Insert Goals
app.post('/insertGoal', function(request, response){
console.log('USING POST METHOD To Insert Goal');
handleInsert(request, response);
});
function handleInsert(request, response) {
if (request.body.username != null && request.body.password != null) {
request.session.user = request.body.username;
request.session.pass = request.body.password;
console.log(request.body.username, request.body.password);
console.log(request.session.user, request.session.pass);
}
if (request.session.user != "" && request.session.pass != "") {
const pass = request.session.pass;
const username = request.session.user;
const user_id = request.session.uid;
const goalName = request.body.goal_name;
const description = request.body.description;
const category = request.body.goal_view;
const endDate = request.body.end_date;
const location = request.body.location;
console.log(user_id, goalName, description, endDate, category, location);
pool.query('INSERT INTO goals(user_id, goal_name, description, start_date, end_date, goal_count, total_count, category, location) VALUES($1, $2, $3, current_timestamp, $4, 0, 0, $5, $6)',
[user_id, goalName, description, endDate, category, location], (err, res) => {
if (err) {
console.log("did not insert goal");
request.session.destroy();
throw err;
} else {
response.render('pages/confirmation');
}
})
}
}
//Logout
app.get('/logout', handleLogout);
function handleLogout(request, response) {
// We should do better error checking here to make sure the parameters are present
if (request.session.user) {
console.log("destroying session");
request.session.destroy();
response.render('pages/index');
}
}