-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
162 lines (136 loc) · 4.49 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const mySql = require('mysql');
const app = express();
app.set('views', path.join(__dirname, 'public'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use('/', express.static(__dirname + '/'));
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
const PORT = 8000;
const index = fs.readFileSync('public/index.html');
const registerEvent = fs.readFileSync('public/registerEvent.html');
function log(logMessage) {
console.log(logMessage);
}
var connection = mySql.createConnection(
{
host: 'localhost',
user: 'root',
password: '',
database: 'quest',
multipleStatements: true,
dateStrings: 'date'
}
);
connection.connect(function (error) {
if (error) {
log('Database Connection failed');
} else {
log('Connected to Database');
}
});
app.get('/event/:eventCode', (req, res) => {
var selectionQuery = "SELECT * FROM events WHERE eventId = '"+ req.params.eventCode +"'";
connection.query(selectionQuery, (err, result) => {
if(!err){
res.render('event.html', {events: result});
}else{
log(err);
throw err;
}
});
});
app.get('/manageEvent', (req, res) => {
var selectionQuery = "SELECT * FROM events order by eventDate";
connection.query(selectionQuery, (err, result) => {
if (err) {
log(err);
throw err;
}
else{
res.render('manageEvent.html', {contests: result});
}
});
});
var candidateRegistration = (req, res) => {
var fullName = req.body.fullName;
var contactNumber = req.body.mobileNumber;
var registeringContest = req.body.contestNames;
var insertQuery = "INSERT INTO candidates (candidateId, candidateName, candidateContact) VALUES ('', '"+fullName+"', '"+contactNumber+"'); ";
var getCandidateIdQuery = "SELECT candidateId FROM candidates WHERE candidateName = '"+ fullName +"'";
connection.query(insertQuery, (err, result) => {
if(!err){
connection.query(getCandidateIdQuery, (err2, result2) => {
if(!err2){
var updateRegistersQuery = "INSERT INTO registers (eventCode, candidateId) VALUES ('"+ registeringContest +"', '"+ result2[0].candidateId +"')";
connection.query(updateRegistersQuery, (err3, result3) => {
if(!err3){
log(result2[0].candidateId + "Registered for event succesfully");
}
});
}
});
}
if (err) {
log("Error while registering for contest");
log(err);
}
else{
log("Candidate registered succesfully");
}
});
return res.redirect('/registerCandidate');
};
var eventRegistration = (req, res) => {
var eventName = req.body.eventName;
var eventId = req.body.eventId;
var eventDate = req.body.eventDate;
var eventTime = req.body.eventTime;
var insertQuery = "INSERT INTO events (eventId, eventName, eventDate, eventTime) VALUES ('"+eventId+"', '"+eventName+"', '"+eventDate+"', '"+eventTime+"'); ";
connection.query(insertQuery, (err, result) => {
if (err) {
log("Error while registering for event");
log(err);
throw err;
}
else{
log("Event registered succesfully");
}
return res.redirect('/registerEvent');
});
};
app.post('/candidateRegistration', candidateRegistration);
app.get("/registerCandidate", (req, res) => {
var selectionQuery = 'SELECT * FROM events';
connection.query(selectionQuery, (err, result) => {
if (err) {
log(err);
throw err;
}
else{
res.render('registerCandidate.html', {contests: result});
}
});
});
app.post('/eventRegistration', eventRegistration);
app.get('/registerEvent', (req,res)=>{
res.write(registerEvent);
res.end();
});
app.get("/", (req, res) => {
var selectionQuery = 'SELECT * FROM fest';
connection.query(selectionQuery, (err, result) => {
if(!err){
res.render('index.html', {fest: result});
}else{
log(err);
throw err;
}
});
});
app.listen(PORT, () => {
console.log('Application running at http://localhost:' + PORT);
});