-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (140 loc) · 4.05 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
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
var express = require('express');
var mysql = require('mysql');
var bodyParser = require('body-parser');
var app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var connection = mysql.createConnection({
host: 'localhost',
user:'hbstudent',
password:'hbstudent',
database:'studentData'
});
app.use(bodyParser.json())
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
connection.connect(function(error){
if(error){
console.log('error');
} else {
console.log('Connected');
}
})
app.get('/', function(req, res, next){
connection.query(`select student.sid, student.sname, course.cname, course.cid
from student, course, enrollment
where student.sid = enrollment.sid
and course.cid = enrollment.cid;`,
function(error,rows,fields){
if(error){
console.log('error in query');
} else {
res.send(rows);
}
})
});
app.post('/', function(req, res, next){
let sname = req.body.name;
let math = req.body.math;
let data = req.body.dataStructure;
let id = 0;
console.log(sname);
console.log(math);
console.log(data);
connection.query(`insert into student values (null, '${sname}');`,
function(error,rows,fields){
if(error){
console.log('error in query!!!!');
} else {
//console.log(rows.insertId);
res.send(rows);
id = rows.insertId;
if(math){
connection.query(`insert into enrollment values (${id}, 1);`,
function(error,rows,fields){
if(error){
console.log('error to insert enrollment values');
} else {
console.log('insert math success!');
}
});
}
if (data){
connection.query(`insert into enrollment values (${id}, 2);`,
function(error,rows,fields){
if(error){
console.log('error in query');
} else {
console.log('insert data structure success!');
}
});
}
}
})
});
app.put('/:sid/:cid', function(req, res, next){
console.log('inside');
let sid = req.params.sid;
let cid = req.params.cid;
let sname = req.body.name;
let newCourseName = req.body.course;
if(newCourseName === 'math'){
newCId = 1;
} else {
newCId = 2;
}
connection.query(`update enrollment set cid= ${newCId} where cid = ${cid} and sid =${sid};`,
function(error,rows,fields){
if(error){
console.log('error in query!!!!');
} else {
console.log('student new name :' + sname);
console.log('student id :' + sid);
connection.query(`update student set sname= '${sname}' where sid = ${sid};`,
function(error,rows,fields){
if(error){
console.log('error in query!!!!');
} else {
res.send(rows);
console.log('update 2 success');
}
})
}
})
});
app.delete('/:id/:cid', function(req, res, next){
let sid = req.params.id;
let cid = req.params.cid;
connection.query(`delete from enrollment where sid = ${sid} and cid = ${cid};`,
function(error,rows,fields){
if(error){
console.log('error in query');
} else {
res.send(rows);
let count = 0;
connection.query(`select count(sid) as count from enrollment where sid = ${sid};`,
function(error,rows,fields){
if(error){
console.log('error in query');
} else {
count = rows[0].count;
if(count === 1){
connection.query(`delete from student where student.sid = ${sid}`,
function(error,rows,fields){
if(error){
console.log(' There is another reocrd for such student!');
} else {
console.log('delete student success!');
}
})
}
}
})
}
})
});
app.listen(1337);