-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
234 lines (191 loc) · 5.87 KB
/
app.js
File metadata and controls
234 lines (191 loc) · 5.87 KB
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
var mysql = require('mysql')
var express = require('express')
var app = express()
var path = require('path')
var student = "Guest",admin =null
var bodyParser = require('body-parser')
app.listen(8080, function (err) {
if (err) throw err;
console.log("listening on port" + 8080)
});
app.use(express.urlencoded({ extended: true }));
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "goodgame",
database: "college_mag"
})
con.connect((error) => {
if (error) {
console.log(error);
}
console.log("connected to database");
})
app.set('views', path.join(__dirname + '/views'))
app.set('view engine', 'ejs')
app.use(express.static("assets"));
app.use(bodyParser.json())
var t1 = ""
var t2,a1,a2,txt1,txt2
app.get('/', function (req, res) {
con.query('select * from article where status = "app" order by art_no desc limit 2;',function(err, result, fields){
const obj = JSON.parse(JSON.stringify(result))
t1 = obj[0].title
a1 = obj[0].stu_username
txt1= obj[0].content
t2 = obj[1].title
a2 = obj[1].stu_username
txt2= obj[1].content
})
// console.log(t1)
res.render('home_1', {title1:t1, author1 : a1, title2: t2, author2 : a2})
res.render('home_1', {title1:t1, author1 : a1, title2: t2, author2 : a2})
res.end()
})
app.get('/logout', function (req, res){
student = "Guest"
admin = null
res.redirect('/')
})
app.get('/home', function (req, res) {
res.redirect('/')
})
app.get('/admin',function (req, res){
if(admin == null){
res.redirect('/')
}
else{
con.query('select * from article where status = "app" order by art_no desc limit 2;',function(err, result, fields){
const obj = JSON.parse(JSON.stringify(result))
t1 = obj[0].title
a1 = obj[0].stu_username
txt1= obj[0].content
t2 = obj[1].title
a2 = obj[1].stu_username
txt2= obj[1].content
})
res.render('admin', {title1:t1, author1 : a1, title2: t2, author2 : a2})
if (err) throw err;
}
})
app.get('/templet', function (req, res) {
// console.log(typeof(txt2))
res.render('templet', { title:t1, author: a1, data: txt1})
})
app.get('/temp',function (req, res){
res.render('templet', { title:t2, author: a2, data: txt2})
})
app.post('/admin_check', function (req, res) {
con.query("select pwd from admin where username = ?",[req.body.uname], function(err, result, fields){
// console.log(result.length)
if(result.length != 0){
if (result[0].pwd == req.body.pwd){
admin = req.body.uname
console.log("logged in")
res.redirect('/admin')
}
else{
res.redirect('/')
}
}
else{
res.redirect('/')
}
})
})
app.get('/student', function (req, res) {
if(student == "Guest"){
res.redirect('/')
}
else {
con.query('select * from article where status = "app" order by art_no desc limit 2;',function(err, result, fields){
const obj = JSON.parse(JSON.stringify(result))
t1 = obj[0].title
a1 = obj[0].stu_username
txt1= obj[0].content
t2 = obj[1].title
a2 = obj[1].stu_username
txt2= obj[1].content
})
res.render('student', {title1:t1, author1 : a1, title2: t2, author2 : a2})}
})
app.post('/student_check', function (req, res) {
// sql = `select pwd from student where username = ${req.body.username};`
con.query('select pwd from student where username = ?;', [req.body.uname], function(err, result, fields){
if(result.length != 0){
if (result[0].pwd == req.body.pwd){
student = req.body.uname
console.log("logged in")
res.redirect('/student')
}
else{
res.redirect('/')
}
}
else{
res.redirect('/')
}
})
})
app.get('/upload', function (req, res) {
if(student == "Guest"){
res.redirect('/')
}
else{
res.render('upload', { name: student })
}
})
app.post('/data', function(req, res){
con.query("insert into article values (0, ?, 'TBC',?,?,?);",[student,req.body.text,null,req.body.title] , function (err) {
if (err) throw err;
})
res.redirect('/student')
})
app.post('/reg', function (req, res) {
// console.log(req.body);
const {username,pwd,contact} = req.body
con.query('insert into student values (?, ?, ?);',[username,pwd,contact] , function (err) {
if (err) {
throw err;
res.redirect('/')
}
else{
student=req.body.username
res.redirect('/')
}
})
})
var atn
app.get('/approve', function (req, res){
if(admin == null){
res.redirect('/')
}
else{
con.query("select * from article where status = 'TBC' order by art_no asc;",function(err, result, fields){
console.log(result)
console.log(result[0])
console.log(result.length)
if(result.length == 0){
res.redirect('/admin')
}
else{
const obj = JSON.parse(JSON.stringify(result))
t1 = obj[0].title
a1 = obj[0].stu_username
txt1= obj[0].content
atn = obj[0].art_no
res.render('check',{title:t1,author:a1,data:txt1})
}
})
// console.log(atn)
}
})
app.post('/update', function (req, res){
if(req.body.status == "app"){
con.query("update article set status = 'app' where art_no = ?",[atn],function(err){})
}
else if(req.body.status == "dis"){
con.query("delete from article where art_no = ?", [atn],function(err){})
}
res.redirect('/approve')
})