-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (88 loc) · 2.63 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
const express = require('express');
const { Client } = require('pg');
const path = require('path');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const bodyParser = require('body-parser');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:false}));
const client = new Client({
connectionString: process.env.DATABASE_URL || 'YOUR LOCAL SERVER'
});
client.connect();
var savedPw = "YOUR PASSWORD";
var pwToTest = '';
io.on('connection', (socket) => {
socket.on('password', (msg) => {
pwToTest = msg;
});
socket.on('status', (msg) => {
if (msg == 'in admin') {
//nothing here
}
});
socket.on('logout', (msg) => {
pwToTest = '';
});
socket.on('disconnect', () => {
//nothing here
});
});
app.use(express.static('public'));
app.get('/', (req, res) => {
var names;
client.query("SELECT * FROM Users WHERE Status='Approve'", (err, result) => {
if (err) throw err;
names = result.rows;
res.render('index', { data: names, success: true });
// client.end();
});
});
app.post('/', (req, res) => {
if (req.body.name != '' && req.body.title != '') {
var idToAdd = req.body.name.split(' ').join('');
// db.run("CREATE TABLE IF NOT EXISTS Users (ID TEXT, Name TEXT, Org TEXT, Title TEXT, Status TEXT)");
// client.connect();
client.query('INSERT INTO users(id,name,org,title,status) VALUES($1, $2, $3, $4, $5)', [idToAdd, req.body.name, req.body.org, req.body.title, 'TBD']);
res.render('thankyou', {data : {name: req.body.name, org: req.body.org}});
} else {
res.render ('index', {data: names, success: false});
}
});
app.get('/admin', (req, res) => {
res.render('auth');
});
app.post('/admin', (req, res) => {
if (pwToTest == savedPw) {
res.redirect('/authorized');
} else {
res.render('auth');
}
});
app.get('/authorized', (req, res) => {
if (pwToTest == savedPw) {
pwToTest = '';
var toView;
client.query("SELECT * FROM users WHERE status='TBD'", (err, result) => {
if (err) throw err;
toView = result.rows;
res.render('admin', {data : toView});
});
} else {
res.redirect('/admin');
}
});
app.post('/authorized', (req, res) => {
pwToTest = savedPw;
var newStatus = req.body.mod.split(' ')[0];
var toChange = req.body.mod.split(' ').slice(1).join(' ');
client.query("UPDATE users SET status = ($1) WHERE name = ($2)", [newStatus, toChange]);
res.redirect('/authorized');
});
app.get('/resources', (req, res) => {
res.render('resources');
});
http.listen(process.env.PORT || 5000, () => {
console.log("App listening on port!")
});