-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
378 lines (312 loc) · 10.7 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
const exp = require('express');
const app = exp();
const http = require('http');
var parse = require('body-parser');
const path = require('path');
app.use(exp.static(path.join(__dirname, 'public')));
app.use(parse.urlencoded({ extended: false }));
app.use(parse.json());
app.set('view engine', 'ejs');
const { Console } = require('console');
const mysql=require('mysql2');
const session = require('express-session');
app.use(session({
secret: 'your_secret_key',
resave: false,
saveUninitialized: true
}));
const multer = require('multer');
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
const port = 3000;
//sql connection
var con = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "",
database: "report",
});
con.connect((error) => {
if (error) {
console.error('faield');
return;
}
console.log('connected');
});
//routersssssssssssssssssssssss
app.get('/', (req, res) => {
var logged=false;
res.render('pages/home',{logged});
});
app.get('/home',function(req,res)
{
var logged=true;
var name=req.session.username;
res.render('pages/home',{logged,name});
})
app.get('/signup',function(req,res)
{
res.render('pages/signup');
});
app.get('/login',function(req,res)
{ var error=false;
res.render('pages/login',{error});
});
app.get('/asignup',function(req,res)
{
res.render('pages/asignup');
});
app.get('/alogin',function(req,res)
{ var error=false;
res.render('pages/alogin',{error});
});
app.get('/about',function(req,res)
{ //var error=false;
var moni=req.session.autho;
res.render('pages/about',{moni});
});
app.get('/dash',function(req,res)
{ //var error=false;
var moni=req.session.autho;
console.log(moni);
res.render('pages/dash',{moni});
});
app.get('/profile',function(req,res)
{ var a=req.session.b;
res.render('pages/profile',{b:a});
});
app.get('/report',function(req,res)
{
res.render('pages/report');
})
app.get('/community',function(req,res)
{
var logged = true;
var name=req.session.username;
res.render("pages/community",{logged,name})
})
app.post('/signup',function(req,res)
{
const username = req.body.username;
const email = req.body.email;
const mobile = req.body.mobile;
const pass = req.body.password;
const pass2 = req.body.password2;
const sql = "select * from user where email=?";
con.query(sql, [email], function (err, result) {
if (result.length > 0) {
res.render('pages/signup', { error: "Already Exist" });
}
});
if (pass == pass2) {
const query = "INSERT INTO user(username,email,mobile,password)values('" + username + "','" + email + "','" + mobile + "','" + pass + "')";
con.query(query, function (err, result) {
if (err) {
throw err;
}
else {
var logged = false;
res.render('pages/login', { error: "Registered succesfully.....Login Now", logged });
console.log("regusterd");
console.log(mobile);
}
});
}
else {
res.render('pages/signup', { error: "mismatched password" });
}
});
app.post('/login', function (req, res) {
const mail = req.body.email;
const pass1 = req.body.password;
var sql = 'SELECT * FROM user WHERE email=? AND password=?';
con.query(sql, [mail, pass1], function (err, result) {
if (err) throw err;
if (result.length > 0) {
req.session.b = result[0];
var a=result[0];
var name = a.username;
var logged = true;
req.session.username = result[0].username;
req.session.mail=result[0].email;
req.session.autho = result[0].autho;
console.log(result[0].autho)
res.render('pages/home', { logged, name});
}
else {
var error = true;
res.render("pages/login", { error: "Invalid email or Password"});
}
});
});
app.post('/report', upload.fields([
{ name: 'img1', maxCount: 1 },
{ name: 'img2', maxCount: 1 }]), function (req, res) {
var email = req.session.username;
const title=req.body.title;
const category = req.body.category;
const description = req.body.description;
const ministry=req.body.department;
const webname = req.body.webname;
const weblink = req.body.weblink;
console.log(email, category,webname);
const img1 = req.files['img1'][0];
const img2 = req.files['img2'][0];
const sql = "insert into bugs (email,title,category,ministry,descr,webname,weblink,img,img2,date)values(?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)";
con.query(sql, [email, title, category,ministry, description,webname,weblink, img1 ? img1.buffer : null,img1 ? img2.buffer : null], function (err, result) {
if (err) {
console.log(err);
}
else {
console.log("sucess");
res.render('pages/report', { successMessage: 'Bug submmited successfuly'});
//res.render('pages/addpro', { successMessage: 'property add succesfully', name: email });
}
});
});
app.post('/asignup',function(req,res)
{ const id=req.body.govid;
const username = req.body.username;
const email = req.body.email;
const mobile = req.body.mobile;
const depart=req.body.department;
const pass = req.body.password;
const pass2 = req.body.password2;
const sql = "select * from authority where email=?";
con.query(sql, [email], function (err, result) {
if (result.length > 0) {
res.render('pages/signup', { error: "Already Exist" });
}
});
if (pass == pass2) {
const query = "INSERT INTO authority(id,username,email,mobile,password,department)values('" + id + "','" + username + "','" + email + "','" + mobile + "','" + pass + "','"+depart+"')";
con.query(query, function (err, result) {
if (err) {
console.log("fucked");
}
else {
var error= true;
res.render('pages/alogin', { error: "Registered succesfully.....Login Now" });
console.log("regusterd");
console.log(mobile);
}
});
}
else {
var error= true;
res.render('pages/asignup', { error: "mismatched password" });
}
});
app.post('/alogin', function (req, res) {
const mail = req.body.email;
const pass1 = req.body.password;
var sql = 'SELECT * FROM authority WHERE email=? AND password=?';
con.query(sql, [mail, pass1], function (err, result) {
if (err) throw err;
if (result.length > 0) {
req.session.b = result[0];
var a=result[0];
var name = a.username;
var logged = true;
req.session.username = result[0].username;
req.session.autho = result[0].autho;
req.session.mail=result[0].email;
var moni=true;
res.render('pages/home', { logged, name,moni});
}
else {
var error= true;
res.render("pages/alogin", { error: "Invalid email or Password"});
}
});
});
app.post('/show', function (req, res) {
const depart = req.body.department;
var name=req.session.name
if(depart=='all')
{
const sql = "select * from bugs";
con.query(sql, function (err, result) {
if (err) {
throw err;
}
else {
var show = true;
var logged = true;
res.render("pages/home", {bug: result, logged, show ,name});
}
});
}else{
const sql = "select * from bugs where ministry=?";
con.query(sql, [depart], function (err, result) {
if (err) {
throw err;
}
else {
var show = true;
var logged = true;
res.render("pages/home", {bug: result,logged, show,name });
}
});
}
});
app.get("/bugs/:bugname", function (req, res) {
const bugname = req.params.bugname;
//const bugname = decodeURIComponent(encodedBugname)
const sql = "select * from bugs where title=?";
con.query(sql, [bugname], function (err, result) {
if (err) {
throw err;
}
else {
const ss="select * from comm2 where title=?";
con.query(ss,[bugname],function(err,resu){
if(err)
{
throw err;
}
else{
res.render("pages/buginfo", {bug:result[0],comms:resu});
}
});
}
})
});
app.post('/comment',function(req,res){
const title=req.body.title;
const user=req.session.username;
var descp=req.body.commet;
const sql = "insert into comm2(title,username,comm)values(?,?,?)";
con.query(sql, [title,user,descp], function (err, result) {
if (err) {
throw err;
}
else {
const ss = "select * from bugs where title=?";
con.query(ss, [title], function (err, result) {
if (err) {
throw err;
}
else {
const ss="select * from comm2 where title=?";
con.query(ss,[title],function(err,resu){
if(err)
{
throw err;
}
else{
res.render("pages/buginfo", {bug:result[0],comms:resu});
}
});
}
})
}
})
})
app.get('/community', function(req, res) {
var name=req.session.username;
res.render('pages/community');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});