-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
274 lines (243 loc) · 7.1 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
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
264
265
266
267
268
269
270
271
272
273
274
//jshint esversion: 9
//Node Module Configurations
const port = process.env.PORT || 5000;
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const lodash = require('lodash');
const cookieParser = require('cookie-parser');
const {
MongoClient
} = require('mongodb');
app.use(cookieParser());
app.use('/public', express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
// Server Start
app.listen(port, () => {
console.log("server started at port 5000");
});
//Additional Function Definitions
// MongoDB initializer function
const uri = "mongodb+srv://nitraipur:[email protected]/?retryWrites=true&w=majority";
const client = new MongoClient(uri);
const db = client.db("Mentors");
async function run() {
try {
await client.connect();
await client.db("Mentors").command({
ping: 1
});
console.log("Connected successfully to server");
} finally {
// await client.close();
}
}
run().catch(console.dir);
// GET paths
// General GET paths
// HOMEPAGE
app.get('/', (req, res) => {
res.render('home');
});
// Error Page
// app.use(function(req, res, next) {
// res.status(404).render('error');
// });
// Mentor Specific GET Paths
// Create Mentor Account
app.get("/mentor", (req, res) => {
res.render('mentor');
});
//Finish Mentor Profile i.e. Adding more details
app.get("/expertise", (req, res) => {
res.render('expertise', {
allskills: []
});
});
// Mentor Login Page
app.get('/login', (req, res) => {
res.render('login');
});
//Mentor Dashboard
app.get('/dashboard', async (req, res) => {
let query = req.cookies.id;
console.log(query);
let mentorData = await db.collection("Mentor").find({
email: query
}).toArray();
console.log(mentorData);
res.render('dashboard', {
params: mentorData
});
});
// Mentee Specific GET paths
app.get("/search", (req, res) => {
res.render('search');
});
// Post Functions
//Mentor Specific POST Functions
//Create New Mentor Account
app.post("/mentor", async function(req, res) {
const data = {
name: req.body.name,
email: req.body.email,
password: req.body.password,
validated: false
};
// Duplicate Check
var lookup = await db.collection("Mentor").countDocuments({
email: req.body.email
}, limit = 1);
if (lookup == 0) {
const result = await db.collection("Mentor").insertOne(data);
console.log("Hi");
res.cookie('id', req.body.email);
console.log("Create Account Successful");
return res.redirect('/expertise');
} else {
console.log("failed");
return setTimeout(() => {
res.redirect("/mentor");
}, 1000);
}
});
// Finish Mentor Profile i.e. add details POST function
app.post('/submitSkills', async (req, res) => {
let query = req.cookies.id;
res.clearCookie('id');
let skillArray = Object.values(req.body);
for (var i = 0; i < 3; i++) {
skillArray.pop();
}
skillArray.forEach((element, index, array) => {
array[index] = (lodash.camelCase(element)).toLowerCase();
});
let project1 = req.body.project1;
let project2 = req.body.project2;
let project3 = req.body.project3;
let cursor = await db.collection("Mentor").updateOne({
email: query
}, {
$set: {
skills: skillArray,
project1: project1,
project2: project2,
project3: project3
}
});
//On success
res.render('alertRegistered', {
url: "mentor",
myMessage: "You have been registered successfully, wait for your verification!"
});
});
//Mentor Login POST function
app.post('/login', async function(req, res) {
var lookup = await db.collection("Mentor").countDocuments({
email: req.body.email,
password: req.body.password
}, limit = 1);
if (lookup == 0) {
res.render('alertRegistered', {
url: "login",
myMessage: "Invalid email/password please try again!"
});
} else {
res.cookie('id', req.body.email);
console.log("login success cookie set");
res.redirect('/dashboard');
}
});
//Mentor Dashboard Post Function
app.post('/dashboard', async (req, res) => {
let skillArray = Object.values(req.body);
for (var i = 0; i < 3; i++) {
skillArray.pop();
}
console.log(skillArray);
for (let i = 0; i<skillArray.length; i++){
skillArray[i] = lodash.camelCase(skillArray[i]).toLowerCase();
}
console.log(skillArray);
let query = req.cookies.id;
let mentorData = await db.collection('Mentor').find({
email: query
}).toArray();
mentorData = mentorData[0];
console.log(mentorData[0]);
mentorData.project1 = req.body.project1;
mentorData.project2 = req.body.project2;
mentorData.project3 = req.body.project3;
await db.collection('Mentor').updateOne({
email: query
}, {
$set: {
skills: skillArray,
project1: mentorData.project1,
project2: mentorData.project2,
project3: mentorData.project3
}
});
res.render('alertRegistered', {myMessage: "Data Altered Successfully!", url: "dashboard"});
});
// Mentee Specific post functions
// Search Mentor by Name or skills
app.post('/search', async (req, res) => {
var search = req.body;
// console.log(search);
var mentors = await db.collection("Mentor").find({
skills: req.body.skill
}).toArray();
// console.log(mentors); //logging the mathing mentors
res.render('display', {
mentors: mentors
});
});
//I dont know what this is
// app.post('/registered', async function(req, res) {
// let query = req.cookies['id'];
// res.clearCookie('id');
// let skills = (req.body.skills).toLowerCase();
// //converting string to array for easy lookup
//
// res.render('success');
// });
// app.get("/route/:variable", (req, res)=>{
// let recieved_name = req.params.variable;
// });
// Discarded search method
// app.post("/search", async function(req, res) {
// let searchItem = lodash.startCase(req.body.search);
// //Search the name of the mentor or search for skills
// let sch = await db.collection("Mentor").countDocuments({
// name: searchItem
// }, limit = 1);
// if (sch != 0) {
// //The user searched for name
// let srch = await db.collection("Mentor").find({
// name: searchItem
// }).toArray();
// console.log("This is not supporsed to happen" + srch);
// res.render('searchResult', srch);
// } else {
// //The user searched for skills
// //find the the matching mentors and add them into the array;
// srch = await db.collection("Mentor").find().toArray();
// console.log(srch);
// var display = [];
// srch.forEach((mentor) => {
// if (mentor.skills != undefined) {
// for (var i = 0; i < mentor.skills.length; i++) {
// if (mentor.skills[i] == searchItem) {
// display.push(mentor);
// }
// }
// }
// });
// console.log(display);
// res.render('searchResult', display);
// }
// });