-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
257 lines (226 loc) · 7.19 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
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
const mongoose=require('mongoose');
const {MongoClient}=require('mongodb');
const express=require('express');
const app=express();
const url=require('url');
app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.set('view engine','ejs');
//app.use(express.static(__dirname + '/public'));
app.use(express.static('public'));
//app.use('/public', express.static('public'));
const y=require('./path.js'); //accessing the mongo url
var {d,a}=require('./model.js'); // accessing the schema for username
var sub=require('./submodel.js'); // accessing the schema for college,subject,semester
var ds=require('./alldoubt.js'); // accessing the schema for questions related to a particular subject
const { all } = require('express/lib/application');
const { updateOne } = require('./submodel.js');
let port=process.env.PORT || 8000;
mongoose.connect(y,{
useNewUrlParser:true,
useUnifiedTopology:true
}).then(client=>{
app.listen(port,()=>{
console.log("Server and database connected");
});
}).catch(err=>{
console.error(err);
})
// home page
app.get('/',(req,res)=>{
res.render("index.ejs");
})
// login page
app.get('/login.ejs',(req,res)=>{
res.render("login.ejs");
})
// create-account page
app.get('/create.ejs',(req,res)=>{
res.render("create.ejs");
})
var usn; //Username
// check username is present in the database
app.get('/login/:id',(req,res)=>{
let id=req.params.id;
a.find({name:{$regex:id,$options:"i"}}).then((r)=>{
if((r.length==0)){
res.render("error.ejs"); // go to error page if no account found
}
else{
usn=id;
res.render("main.ejs",{name:usn}); // go to college,semester,branch selection page if the username is found
}
}).catch(err=>console.error(err));
})
// if error in login go the create-account page
app.get('/create-account',(req,res)=>{
res.render("create.ejs");
});
// check whether that particular username is present in the database
app.get('/check/:id',(req,res)=>{
let id=req.params.id;
a.findOne({name:{$regex:id,$options:'i'}}).then((result)=>{
res.status(200).json(result);
}).catch(err=>console.error(err));
})
// create account
app.post('/add',(req,res)=>{
a.create(req.body).then((result)=>{
res.redirect("/login.ejs");
//console.log(result);
}).catch(err=>console.error(err));
});
// add subject option
app.post('/add/subject',(req,res)=>{
sub.create(req.body).then(result=>res.json(result)).catch(err=>console.error(err));
})
// delete subject option
app.delete('/delete/subject/:id',(req,res)=>{
let i=req.params.id;
sub.findByIdAndDelete({_id:mongoose.Types.ObjectId(i)}).then(res=>res.send("Data deleted")).catch(err=>console.error(err));
});
var qy;
// choose subject from a list of subjects
app.get('/choose/subject',(req,res)=>{
qy=url.parse(req.url,true).query;
sub.find({college:qy.college,branch:qy.branch,semester:qy.semester}).then((result)=>{
if(result.length!==0){
res.render("subject.ejs",{name:usn,subs:result.map(r=>r.subjects)});
}
else{
res.render("nfound.ejs");
}
}).catch(err=>console.error(err));
})
/*app.post('/subject',(req,res)=>{
let result=subject.find({college:req.body.college,branch:req.body.branch,semester:req.body.semester});
if(result){
res.render("subject.ejs",{subjects:result.subject.map(r=>r.question),college:req.body.college,branch:req.body.branch,semester:req.body.semester});
//{result}
}
else{
res.render("no.ejs");
}
})*/
// add doubt
app.post('/doubt/add/ques',(req,res)=>{
ds.create(req.body).then((result)=>res.json(result)).catch(err=>console.error(err));
})
var s;
// get all the questions being asked in a particular subject
app.get('/doubt/ques/:id',(req,res)=>{
s=req.params.id;
//console.log(s);
ds.find({subject:s}).then((result)=>{
if(result.length!==0){
//console.log("usn = "+usn);
res.render("doubt.ejs",{questions:result.map(r=>r.questions),answers:result.map(r=>r.answers),qid:result.map(r=>r._id),subj:s,Username:usn});
}
}).catch(err=>console.error(err));
});
// add own answer to a question in the doubts page
app.put("/put/answer/:id",(req,res)=>{
const ansdb=require('./userd.js');
const quesid=req.params.id;
const query={_id:mongoose.Types.ObjectId(quesid)};
const queryans={name:usn};
if(req.body.answer){
const updocument={
$push:{
answers:{
$each:[req.body.answer],
position:-1
}
}
};
ds.updateOne(query,updocument).then((result)=>{
res.json(result);
ansdb.find(queryans).then(result=>{
// console.log("result");
//console.log(result);
if(result.length!=0){
ansdb.updateOne(queryans,updocument).then((result)=>console.log("added")).catch(err=>console.error(err));
}
else{
const reqbody={
name:usn,
answers:[req.body.answer]
}
//console.log("reqbody");
//console.log(reqbody);
ansdb.create(reqbody);
}
}).catch(err=>console.error(err));
}).catch(err=>console.error(err));
}
});
app.get('/go/answer/page',(req,res)=>{
res.render("answer.ejs",{subjj:s});
})
app.get('/answer.ejs',(req,res)=>{
res.render("answer.ejs",{subjj:s});
})
app.put('/added/ans/del/:id',(req,res)=>{
const quesid=req.params.id;
const query={_id:mongoose.Types.ObjectId(quesid)};
const delans={
$pull:{
answers:req.body.answer
}
};
ds.updateOne(query,delans).then((result)=>res.send("Deleted")).catch(err=>console.error(err));
})
app.post("/ask/ques",(req,res)=>{
const queryans={name:usn};
const qb=require('./userd.js');
if(req.body.questions){
const updocument={
$push:{
questions:{
$each:[req.body.questions],
position:-1
}
}
}
ds.create(req.body).then((result)=>{
res.json(result);
qb.find(queryans).then(result=>{
if(result.length!=0){
qb.updateOne(queryans,updocument).then((result)=>console.log("added")).catch(err=>console.error(err));
}
else{
const reqbody={
name:usn,
questions:[req.body.questions]
}
qb.create(reqbody);
}
})
}).catch(err=>console.error(err));
};
})
app.get("/activity.ejs",(req,res)=>{
res.render("activity.ejs");
})
app.get(`/view/activity/:id`,(req,res)=>{
const id=req.params.id;
const ann=require('./userd.js');
ann.find({name:id}).then((result)=>{
if(result.length!=0){
res.render("activity.ejs",{questions:result[0].questions,answers:result[0].answers,sen:""})
}
else{
res.render("noactivity.ejs",{sen:"No such activity found"})
}
})
})
app.get("/noactivity.ejs",(req,res)=>{
res.render("noactivity.ejs");
})
app.get('/doubt/ans/:id',(req,res)=>{
let qi=req.params.id;
ds.find({"dques._id":mongoose.Types.ObjectId(qi)}).then((result)=>{res.render("answer.ejs",{answers:result[0].dques.map(r=>r.answers)})}).catch(err=>console.error(err))
})
app.put('/add/answer',(req,res)=>{
})
/*c*/