Skip to content

Commit b872b26

Browse files
chore(voting-app): update ejs (#479)
* chore(voting-app): update ejs * save file before commiting * fix: pie charts, I think --------- Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com>
1 parent 320e04b commit b872b26

File tree

9 files changed

+404
-205
lines changed

9 files changed

+404
-205
lines changed

apps/voting-app/app.js

Lines changed: 134 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,127 +3,160 @@ var express = require('express');
33
var bodyParser = require('body-parser');
44
var ObjectID = require('mongodb').ObjectID;
55

6-
require('dotenv').config()
6+
require('dotenv').config();
77

88
var app = express();
99

1010
app.use(bodyParser.json());
11-
app.use(bodyParser.urlencoded({extended:false}));
11+
app.use(bodyParser.urlencoded({ extended: false }));
1212
app.use('/public', express.static('public'));
1313
app.set('view engine', 'ejs');
1414
app.set('views', './views');
1515

16-
mongo.connect(process.env.MONGO_URI, { useUnifiedTopology: true }, function(err, client) {
17-
// var db = client.db('voting-app-v0');
18-
var db = client.db();
19-
console.log('Successfully connected to MongoDB');
20-
21-
app.locals.user = null;
22-
app.locals.usersPolls = null;
23-
app.locals.allPolls = null;
24-
app.locals.singlePoll = null;
25-
var users = db.collection('users');
26-
var polls = db.collection('polls');
27-
28-
function login(req, res) {
29-
users.findOne({ 'username': req.body.username, 'password': req.body.password },{ _id: 0 }, function (err, findUser) {
30-
if (findUser) {
31-
app.locals.user = findUser.username;
32-
polls.find().toArray(function(err, findAllPolls) {
33-
app.locals.allPolls = findAllPolls;
34-
return mypolls(req, res);
16+
mongo.connect(
17+
process.env.MONGO_URI,
18+
{ useUnifiedTopology: true },
19+
function (err, client) {
20+
if (err) {
21+
console.error(err);
22+
throw new Error('Unable to connect to MongoDB');
23+
}
24+
// var db = client.db('voting-app-v0');
25+
var db = client.db();
26+
console.log('Successfully connected to MongoDB');
27+
28+
app.locals.user = null;
29+
app.locals.usersPolls = null;
30+
app.locals.allPolls = null;
31+
app.locals.singlePoll = null;
32+
var users = db.collection('users');
33+
var polls = db.collection('polls');
34+
35+
function login(req, res) {
36+
users.findOne(
37+
{ username: req.body.username, password: req.body.password },
38+
{ _id: 0 },
39+
function (err, findUser) {
40+
if (findUser) {
41+
app.locals.user = findUser.username;
42+
polls.find().toArray(function (err, findAllPolls) {
43+
app.locals.allPolls = findAllPolls;
44+
return mypolls(req, res);
45+
});
46+
} else {
47+
return allpolls(req, res);
48+
}
49+
}
50+
);
51+
}
52+
53+
function mypolls(req, res) {
54+
polls
55+
.find({ creator: app.locals.user }, { creator: 0 })
56+
.toArray(function (err, findUsersPolls) {
57+
app.locals.usersPolls = findUsersPolls;
58+
return res.render('mypolls');
3559
});
36-
} else {
37-
return allpolls(req, res);
38-
}
39-
});
40-
}
60+
}
61+
62+
function allpolls(req, res) {
63+
polls.find().toArray(function (err, findAllPolls) {
64+
app.locals.allPolls = findAllPolls;
65+
return res.render('index');
66+
});
67+
}
4168

42-
function mypolls(req, res) {
43-
polls.find({ 'creator': app.locals.user },{ creator: 0 }).toArray(function(err, findUsersPolls) {
44-
app.locals.usersPolls = findUsersPolls;
45-
return res.render('mypolls');
69+
app.get('/', allpolls);
70+
71+
app.get('/signup', function (req, res) {
72+
return res.render('signup');
4673
});
47-
}
4874

49-
function allpolls(req, res) {
50-
polls.find().toArray(function(err, findAllPolls) {
51-
app.locals.allPolls = findAllPolls;
52-
return res.render('index');
75+
app.post('/signup', function (req, res) {
76+
users.findOne({ username: req.body.username }, function (err, signup) {
77+
if (!signup) {
78+
users.insert({
79+
username: req.body.username,
80+
password: req.body.password
81+
});
82+
return login(req, res);
83+
} else {
84+
return login(req, res);
85+
}
86+
});
5387
});
54-
}
5588

56-
app.get('/', allpolls)
57-
58-
app.get('/signup', function(req, res){
59-
return res.render('signup');
60-
});
61-
62-
app.post('/signup', function(req, res){
63-
users.findOne({ 'username': req.body.username }, function (err, signup) {
64-
if (!signup) {
65-
users.insert({'username': req.body.username, 'password': req.body.password });
66-
return login(req, res);
89+
app.get('/login', function (req, res) {
90+
return res.render('login');
91+
});
92+
93+
app.post('/login', login);
94+
95+
app.get('/mypolls', mypolls);
96+
97+
app.get('/newpoll', function (req, res) {
98+
return res.render('newpoll');
99+
});
100+
101+
app.post('/newpoll', function (req, res) {
102+
var optionArray = req.body.newoptions.split(',');
103+
var options = {};
104+
105+
for (var i = 0; i < optionArray.length; i++) {
106+
options[optionArray[i]] = 0;
107+
}
108+
109+
polls.insert(
110+
{
111+
creator: app.locals.user,
112+
question: req.body.newquestion,
113+
options: options
114+
},
115+
function () {
116+
return mypolls(req, res);
117+
}
118+
);
119+
});
120+
121+
app.post('/vote/:id', function (req, res) {
122+
if (req.body.voteOptions === 'Add option') {
123+
polls.update(
124+
{ _id: ObjectID(req.params.id) },
125+
{ $inc: { ['options.' + req.body.newOption]: 1 } },
126+
{ upsert: true }
127+
);
128+
return allpolls(req, res);
67129
} else {
68-
return login(req, res);
69-
}
130+
polls.update(
131+
{ _id: ObjectID(req.params.id) },
132+
{ $inc: { ['options.' + req.body.voteOptions]: 1 } },
133+
{ upsert: true }
134+
);
135+
return allpolls(req, res);
136+
}
70137
});
71-
});
72-
73-
app.get('/login', function(req, res){
74-
return res.render('login');
75-
});
76-
77-
app.post('/login', login)
78-
79-
app.get('/mypolls', mypolls);
80-
81-
app.get('/newpoll', function(req, res){
82-
return res.render('newpoll');
83-
});
84-
85-
app.post('/newpoll', function(req, res){
86-
var optionArray = req.body.newoptions.split(',');
87-
var options = {};
88-
89-
for (var i=0; i<optionArray.length; i++) {
90-
options[optionArray[i]] = 0;
91-
}
92-
93-
polls.insert({ 'creator':app.locals.user, 'question': req.body.newquestion, 'options': options }, function() {
94-
return mypolls(req, res);
138+
139+
app.get('/delete/:id', function (req, res) {
140+
polls.remove({ _id: ObjectID(req.params.id) }, function () {
141+
return mypolls(req, res);
142+
});
95143
});
96-
});
97144

98-
app.post('/vote/:id', function(req, res){
99-
if (req.body.voteOptions === "Add option") {
100-
polls.update({ _id : ObjectID(req.params.id)}, { $inc: { ["options."+req.body.newOption] : 1 } }, { upsert: true });
101-
return allpolls(req, res);
102-
} else {
103-
polls.update({ _id : ObjectID(req.params.id)}, { $inc: { ["options."+req.body.voteOptions] : 1 } }, { upsert: true });
104-
return allpolls(req, res);
105-
}
106-
});
107-
108-
app.get('/delete/:id', function(req, res){
109-
polls.remove({ _id : ObjectID(req.params.id)}, function() {
110-
return mypolls(req, res);
145+
app.get('/share/:id', function (req, res) {
146+
polls
147+
.find({ _id: ObjectID(req.params.id) })
148+
.toArray(function (err, findSinglePoll) {
149+
app.locals.singlePoll = findSinglePoll[0];
150+
return res.render('singlepoll');
151+
});
111152
});
112-
});
113-
114-
app.get('/share/:id', function(req, res){
115-
polls.find({ '_id' : ObjectID(req.params.id)}).toArray(function(err, findSinglePoll) {
116-
app.locals.singlePoll = findSinglePoll[0];
117-
return res.render('singlepoll');
153+
154+
app.get('/logout', function (req, res) {
155+
app.locals.user = null;
156+
return allpolls(req, res);
118157
});
119-
});
120-
121-
app.get('/logout', function(req, res){
122-
app.locals.user = null;
123-
return allpolls(req, res);
124-
});
125-
126-
});
158+
}
159+
);
127160

128161
const portNum = process.env.PORT || 3000;
129162

0 commit comments

Comments
 (0)