-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
349 lines (312 loc) · 10.6 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
const express = require('express');
const sqlite3 = require('sqlite3');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
// Connect to the SQLite database
const db = new sqlite3.Database('student_perf.db')
// Middleware for parsing JSON requests
app.use(express.json());
// Schools API endpoint
app.get('/api/schools', (req, res) => {
db.all('SELECT DISTINCT school FROM Student', (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ schools: rows.map(row => row.school) });
});
});
// API for all students
app.get('/api/students', (req, res) => {
db.all('SELECT * FROM Student', (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ students: rows });
});
});
// API for students by school
app.get('/api/students/by-school/:school', (req, res) => {
const { school } = req.params;
db.all('SELECT * FROM Student WHERE school = ?', [school], (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ students: rows });
});
});
// API for students by gender
app.get('/api/students/by-gender/:gender', (req, res) => {
const { gender } = req.params;
db.all('SELECT * FROM Student WHERE sex = ?', [gender], (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ students: rows });
});
});
// API for performance data for a specific student.
app.get('/api/performance/:studentId', (req, res) => {
const { studentId } = req.params;
// Fetch performance data for the specific student
db.get('SELECT G1, G2, G3 FROM Student WHERE id = ?', [studentId], (err, row) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
// Respond with the performance data for the specific student
res.json({ performanceData: row });
});
});
// Get performance data for all students in a specific school.
app.get('/api/schools/:id/performance', (req, res) => {
const { id } = req.params;
// Fetch performance data for all students in the specific school
db.all('SELECT G1, G2, G3 FROM Student WHERE school = ?', [id], (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
// Respond with the performance data for all students in the specific school
res.json({ performanceData: rows });
});
});
//API for Impact of Parental Education Level
app.get('/api/performance-by-parental-education', (req, res) => {
// Calculate average scores based on parental education level
db.all('SELECT Medu, Fedu, AVG(G1) AS avgG1, AVG(G2) AS avgG2, AVG(G3) AS avgG3 FROM Student GROUP BY Medu, Fedu', (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ impactByParentalEducation: rows });
});
});
// API for Visualization of Score Distributions
app.get('/api/score-distributions', (req, res) => {
// Fetch scores for each subject
db.all('SELECT G1, G2, G3 FROM Student', (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ scoreDistributions: rows });
});
});
// Create a new student
app.post('/api/students', (req, res) => {
const newStudent = req.body;
// Validate that the required information is present in the request body
if (!newStudent.school || !newStudent.sex || !newStudent.age) {
res.status(400).json({ error: 'Required information (school, sex, age) is missing for creating a new student' });
return;
}
// Inserting the new student into the database
db.run(
'INSERT INTO Student (school, sex, age, address, famsize, Pstatus, Medu, Fedu, Mjob, Fjob, reason, guardian, traveltime, studytime, failures, schoolsup, famsup, paid, activities, nursery, higher, internet, romantic, famrel, freetime, goout, Dalc, Walc, health, absences, G1, G2, G3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
newStudent.school,
newStudent.sex,
newStudent.age,
newStudent.address,
newStudent.famsize,
newStudent.Pstatus,
newStudent.Medu,
newStudent.Fedu,
newStudent.Mjob,
newStudent.Fjob,
newStudent.reason,
newStudent.guardian,
newStudent.traveltime,
newStudent.studytime,
newStudent.failures,
newStudent.schoolsup,
newStudent.famsup,
newStudent.paid,
newStudent.activities,
newStudent.nursery,
newStudent.higher,
newStudent.internet,
newStudent.romantic,
newStudent.famrel,
newStudent.freetime,
newStudent.goout,
newStudent.Dalc,
newStudent.Walc,
newStudent.health,
newStudent.absences,
newStudent.G1,
newStudent.G2,
newStudent.G3
],
function (err) {
if (err) {
res.status(500).json({ error: err.message });
return;
}
// Respond with the information of the newly created student
res.json({
success: true,
message: 'Student created successfully',
student: {
id: this.lastID,
...newStudent
}
});
}
);
});
// Update details of a specific student
app.put('/api/students/:id', (req, res) => {
const studentId = req.params.id;
const updatedStudent = req.body;
// Validate that the required information is present in the request body
if (!updatedStudent.school || !updatedStudent.sex || !updatedStudent.age) {
res.status(400).json({ error: 'Required information (school, sex, age) is missing for updating the student' });
return;
}
// Update the details of the specific student in the database
db.run(
'UPDATE Student SET school = ?, sex = ?, age = ?, address = ?, famsize = ?, Pstatus = ?, Medu = ?, Fedu = ?, Mjob = ?, Fjob = ?, reason = ?, guardian = ?, traveltime = ?, studytime = ?, failures = ?, schoolsup = ?, famsup = ?, paid = ?, activities = ?, nursery = ?, higher = ?, internet = ?, romantic = ?, famrel = ?, freetime = ?, goout = ?, Dalc = ?, Walc = ?, health = ?, absences = ?, G1 = ?, G2 = ?, G3 = ? WHERE id = ?',
[
updatedStudent.school,
updatedStudent.sex,
updatedStudent.age,
updatedStudent.address,
updatedStudent.famsize,
updatedStudent.Pstatus,
updatedStudent.Medu,
updatedStudent.Fedu,
updatedStudent.Mjob,
updatedStudent.Fjob,
updatedStudent.reason,
updatedStudent.guardian,
updatedStudent.traveltime,
updatedStudent.studytime,
updatedStudent.failures,
updatedStudent.schoolsup,
updatedStudent.famsup,
updatedStudent.paid,
updatedStudent.activities,
updatedStudent.nursery,
updatedStudent.higher,
updatedStudent.internet,
updatedStudent.romantic,
updatedStudent.famrel,
updatedStudent.freetime,
updatedStudent.goout,
updatedStudent.Dalc,
updatedStudent.Walc,
updatedStudent.health,
updatedStudent.absences,
updatedStudent.G1,
updatedStudent.G2,
updatedStudent.G3,
studentId
],
function (err) {
if (err) {
res.status(500).json({ error: err.message });
return;
}
// Check if any rows were affected
if (this.changes === 0) {
res.status(404).json({ error: 'Student not found' });
return;
}
// Respond with the information of the updated student
res.json({
success: true,
message: 'Student details updated successfully',
student: {
id: studentId,
...updatedStudent // Include other fields if needed
}
});
}
);
});
// Delete a specific student
app.delete('/api/students/:id', (req, res) => {
const studentId = req.params.id;
// Delete the specific student from the database
db.run('DELETE FROM Student WHERE id = ?', [studentId], function (err) {
if (err) {
res.status(500).json({ error: err.message });
return;
}
// Check if any rows were affected
if (this.changes === 0) {
res.status(404).json({ error: 'Student not found' });
return;
}
// Respond with a success message
res.json({
success: true,
message: 'Student deleted successfully',
studentId: studentId
});
});
});
// API for students by multiple criteria
app.post('/api/students/filter', (req, res) => {
const { criteria } = req.body;
// Define an SQL query string based on the provided criteria
let sql = 'SELECT * FROM Student WHERE 1=1';
const params = [];
// Check if criteria are provided and append them to the query
if (criteria) {
if (criteria.school) {
sql += ' AND school = ?';
params.push(criteria.school);
}
if (criteria.gender) {
sql += ' AND sex = ?';
params.push(criteria.gender);
}
// Execute the query
db.all(sql, params, (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ filteredStudents: rows });
});
} else {
res.status(400).json({ error: 'Criteria not provided' });
}
});
// API for Average Scores
app.get('/api/average-scores', (req, res) => {
db.get('SELECT AVG(G1) AS avgG1, AVG(G2) AS avgG2, AVG(G3) AS avgG3 FROM Student', (err, row) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ averageScores: row });
});
});
// API for performance by gender
app.get('/api/performance-by-gender', (req, res) => {
db.all('SELECT sex, AVG(G1) AS avgG1, AVG(G2) AS avgG2, AVG(G3) AS avgG3 FROM Student GROUP BY sex', (err, rows) => {
if (err) {
res.status(500).json({ error: err.message });
return;
}
res.json({ performanceByGender: rows });
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});