-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
172 lines (159 loc) · 4.73 KB
/
ajax.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
// -----------------------------------------------------------------------------
// Name: /handlers/ajax.js
// Author: Adam Barreiro Costa
// Description: Sets all the AJAX handlers
// -----------------------------------------------------------------------------
var modelGroup = require('../models/group.js');
var modelStudent = require('../models/student.js');
var modelLevel = require('../models/level.js');
/**
* Responds to the AJAX petition which asks for all the groups in the database.
*/
function ajaxGetGroups() {
app.get('/getGroups', function (req, res) {
modelGroup.getAllGroups(function(groups) {
res.send(groups);
});
});
}
exports.ajaxGetGroups = ajaxGetGroups;
/**
* Responds to the AJAX petition which asks for all the students, whether they
* have a group assigned or not.
*/
function ajaxGetStudents() {
app.get('/getStudentsWithGroup', function (req, res) {
modelStudent.getAllStudents(true, function(students) {
res.send(students);
});
});
app.get('/getStudentsWithNoGroup', function (req, res) {
modelStudent.getAllStudents(false, function(students) {
res.send(students);
});
});
app.get('/getAllStudents', function (req, res) {
modelStudent.getAllStudents(null, function(students) {
res.send(students);
});
});
}
exports.ajaxGetStudents = ajaxGetStudents;
/**
* Responds to the AJAX petition which asks for the complete data of a specific
* student.
*/
function ajaxGetStudent() {
app.post('/getStudent', function (req, res) {
modelStudent.searchStudent(unescape(req.body.email), function(student) {
res.send(student);
});
});
}
exports.ajaxGetStudent = ajaxGetStudent;
/**
* Responds to the AJAX petition which asks for the list of levels created.
*/
function ajaxGetLevelList() {
app.post('/getLevelList', function (req, res) {
modelLevel.getLevelSequence(function(levelList) {
res.send(levelList);
});
});
}
exports.ajaxGetLevelList = ajaxGetLevelList;
/**
* Responds to the AJAX petition which asks for the creation of a new level.
*/
function ajaxLevelCreate() {
app.post('/levelCreate', function (req, res) {
modelLevel.addLevel(function(level) {
res.send(level);
});
});
}
exports.ajaxLevelCreate = ajaxLevelCreate;
/**
* Responds to the AJAX petition which asks for the modification of a level.
*/
function ajaxLevelUpdate() {
app.post('/levelUpdate', function (req, res) {
modelLevel.updateLevel(req.body.number, JSON.parse(req.body.map), function(ok) {
res.send(ok);
});
});
}
exports.ajaxLevelUpdate = ajaxLevelUpdate;
/**
* Responds to the AJAX petition which asks for the load of a level.
*/
function ajaxLevelLoad() {
app.post('/levelLoad', function (req, res) {
modelLevel.getLevel(req.body.number, function(level) {
res.send(level);
});
});
}
exports.ajaxLevelLoad = ajaxLevelLoad;
/**
* Responds to the AJAX petition which asks for the removal of a level.
*/
function ajaxLevelDelete() {
app.post('/levelDelete', function (req, res) {
modelLevel.deleteLevel(req.body.number, function(ok) {
res.send(ok);
});
});
}
exports.ajaxLevelDelete = ajaxLevelDelete;
/**
* Responds to the AJAX petition which asks for moving a level.
*/
function ajaxLevelMove() {
app.post('/levelMove', function (req, res) {
modelLevel.moveLevel(req.body.from, req.body.to, function(ok) {
res.send(ok);
});
});
}
exports.ajaxLevelMove = ajaxLevelMove;
/**
* Responds to the AJAX petition which asks for saving a game.
*/
function ajaxSaveGame() {
app.post('/saveGame', function (req, res) {
modelStudent.setLevelStudent(req.body.student, req.body.level, function(ok) {
res.send(ok);
});
});
}
exports.ajaxSaveGame = ajaxSaveGame;
/**
* Responds to the AJAX petition which asks for saving a game.
*/
function ajaxLoadGame() {
app.post('/loadGame', function (req, res) {
modelStudent.getLevelStudent(req.body.email, function(data) {
res.send(data);
});
});
}
exports.ajaxLoadGame = ajaxLoadGame;
/**
* Responds to the AJAX petition which asks for the questions file.
*/
function ajaxGetQuestions() {
app.post('/getQuestionsFile', function (req, res) {
res.sendfile('public/assets/questions.txt');
});
}
exports.ajaxGetQuestions = ajaxGetQuestions;
/**
* Responds to the AJAX petition which asks for the timeouts file.
*/
function ajaxGetTimeouts() {
app.post('/getTimeoutsFile', function (req, res) {
res.sendfile('public/assets/timeouts.txt');
});
}
exports.ajaxGetTimeouts = ajaxGetTimeouts;