-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (110 loc) · 3.55 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
const express = require('express');
const bodyparser = require('body-parser');
const cors = require('cors');
// Express app
const app = express();
app.use(cors());
app.use(bodyparser.json());
// Connection from the database
const connection = require('./database/config');
// Schema Modules
const Users = require('./api/users/Users');
const Auth = require('./api/users/Auth');
const QuizViews = require('./api/quizdatabase/QuizViews');
const SubmitQuiz = require('./api/quizdatabase/SubmitQuiz');
// Route for getting all user details
app.post('/api/users', (req, res) => {
let que;
console.log(req.body);
// TODO: specify which database
if (req.body.type === 'usernames') {
Users.getAllUsernames(connection, req, res);
} else if (req.body.type === 'allstudents') {
Users.getAllStudents(connection, req, res);
} else if (req.body.type === 'allteachers') {
Users.getAllTeachers(connection, req, res);
} else {
res.sendStatus(412);
}
} );
// TODO : update and fix
// Route for creating databases
app.get('/api/users', (req, res) => {
console.log(req.body);
if(req.body.type == 'allstudents'){
Users.getAllStudents(connection, req, res);
}else if(req.body.type == 'allteachers'){
Users.getAllTeachers(connection, req, res);
}else{
res.json({"msg":"Welcome"});
}
} );
// Route for getting user password associated with username or email
app.post('/api/users/auth', (req, res) => {
if (req.body.type == 'getConfirmation') {
Auth.getConfirmedUser(connection, req, res);
} else if (req.body.type == 'signup') {
Auth.signUpUser(connection, req, res);
}
} );
// Route for sign up a new user
app.put('/api/users/auth', (req, res) => {
if(req.body.table === 'Student'){
Auth.signUpStudent(connection, req, res);
}else if(req.body.table === 'Teacher'){
Auth.signUpTeacher(connection, req, res);
}
} );
// Route for getting specific user
app.get('/api/users/:id', (req, res) => {
let que = `SELECT Username, Email FROM foxlearn.Student WHERE Std_ID = ${req.params.id} `;
connection.query(que, (error, results, fields) => {
if (error) {
res.sendStatus(400);
return console.error(error.message);
}
res.json(results);
});
} );
// Route for getting one question
app.get('/api/quiz', (req, res) => {
QuizViews.getOneQuestion(connection, req, res);
} );
// Route for getting complete quiz
app.post('/api/quiz', (req, res) => {
let id_que = `SELECT Ques_ID FROM foxlearn.Question;`;
let ids = [];
connection.query(id_que, (error, results, fields) => {
if (error) {
return console.error(error.message);
}
results.forEach( (item) => {
ids.push(item.Ques_ID);
});
let i = 0;
let randomIDs = [];
while(i < 5){
var item = ids[Math.floor(Math.random()*ids.length)];
if (randomIDs.includes(item)) {
continue;
} else {
randomIDs.push(item);
i++;
}
}
QuizViews.getCompleteQuiz(connection, randomIDs, res);
});
} );
// Route for creating a quiz
app.put('/api/quiz/create', (req, res) => {
SubmitQuiz.submitQuiz(connection, req, res);
});
// Route for selecting sub IDs
app.get('/api/quiz/IDs', (req, res) => {
QuizViews.getSubjectIDs(connection, req, res);
});
// connection.end();
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});