-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (156 loc) · 4.78 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
const express = require('express'); //import express om te gebruiken. Express var is nu de express funcite
const app = express(); //koppel de var express aan de var app zodat je met alleen app epxress kan oproepen
const bodyParser = require('body-parser'); //maakt het makkelijker om de values uit een formulier te halen
require('dotenv').config();
//CONST FOR DB
const mongo = require('mongodb');
const ObjectId = mongo.ObjectID;
let db = null;
const url = process.env.DB_LINK;
let userDB = null;
//CONST FOR SESSION
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const sessionId = 'sessionId';
const sessionSecret = process.env.SES_SECRET;
const store = new MongoDBStore({
uri: url,
collection: 'sessions',
});
mongo.MongoClient.connect(url, { useUnifiedTopology: true }, (err, client) => {
if (err) {
throw err;
}
db = client.db(process.env.DB_NAME);
userDB = db.collection('users');
matchDB = db.collection('match');
});
app.listen(1000, () => console.log('server is working'));
//------SESSION AANMAKEN\
app.use(
require('express-session')({
name: sessionId,
secret: process.env.SES_SECRET,
store: store,
resave: false, //tussentijds dingen opslaan terwijl hij niks heeft aangepast hoeft niet!!
saveUninitialized: false,
cookie: {
sameSite: true, //alleen cookies pakken die van jouw eigen website komen
secure: false, //dit moet op true als je met https werkt
},
})
);
app.use('/', express.static('static/style'));
app.use(bodyParser.urlencoded({ extended: true })); //ontvang data uit het formulier en gebruik data in de code
app.use(express.static(__dirname + '/static')); //zorgt dat ik mijn css en images bestanden kan linken aan mijn .ejs pagia's
app.set('view engine', 'ejs');
app.set('views', 'views');
//----------------THIS IS WHERE THE MAGIC BEGINS (logic)-----------------------
//POST FORM
app.get('/', (req, res) => {
res.render('form');
});
app.post('/', (req, res) => {
const data = {
name: req.body.firstname,
age: req.body.age,
city: req.body.city,
description: req.body.description,
likes: [],
megalikes: [],
number: 0,
};
//check if data is inserted
if (data) {
//create sesssion id
req.session.sessionId = data;
// console.log('session id =');
// console.log(req.session.sessionId);
//insert data to database
userDB.insertOne(data);
console.log('added ' + data.name + ' to database');
console.log(req.session);
res.redirect('/home');
} else {
console.log('Data did not load, try again!');
}
});
//GET HOMEPAGE
app.get('/home', async (req, res) => {
if (req.session.sessionId._id) {
//render all id's in database exept for your own
const users = await userDB
.find({ _id: { $ne: ObjectId(req.session.sessionId._id) } })
.toArray();
const index = await userDB.findOne({
_id: ObjectId(req.session.sessionId._id),
});
res.render('home', { userData: users[index.number] });
}
});
//LIKE DISLIKE OR MEGALIKE THE USER-----------------
app.post('/like', async (req, res) => {
if (req.body.review === 'like') {
userDB.updateOne(
{
_id: ObjectId(req.session.sessionId._id),
},
{
$inc: { number: 1 },
$push: {
likes: req.body.id,
},
}
);
userDB.updateMany({}, { $set: { 'likes.$[]': ObjectId(req.body.id) } });
req.session.sessionId.likes.push(ObjectId(req.body.id));
res.redirect('home');
} else if (req.body.review === 'dislike') {
userDB.updateOne(
{
_id: ObjectId(req.session.sessionId._id),
},
{
$inc: { number: 1 },
}
);
res.redirect('home');
} else if (req.body.review === 'megalike') {
userDB.updateOne(
{
_id: ObjectId(req.session.sessionId._id),
},
{
$inc: { number: 1 },
$push: {
megalikes: req.body.id,
},
}
);
userDB.updateMany({}, { $set: { 'likes.$[]': ObjectId(req.body.id) } });
req.session.sessionId.megalikes.push(ObjectId(req.body.id));
res.redirect('home');
}
if(req.body.delete){
console.log("delteAccount")
const id = req.session.sessionId._id;
// remove user
userDB.deleteOne({_id: ObjectId(id)});
res.redirect('/')
req.session.destroy();
}
});
app.get('/likes', async (req, res) => {
if (req.session.sessionId.likes) {
console.log(req.session.sessionId.likes);
const usersIds = await userDB
.find({ _id: { $in: req.session.sessionId.likes } })
.toArray();
console.log(usersIds);
res.render('liked', { users: usersIds });
}
});
// ------------------------------
app.use(function (req, res, next) {
res.status(404).send('sorry, dit heb ik niet gevonden...');
}); //custom 404 error