-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataEntry.js
More file actions
134 lines (118 loc) · 4.61 KB
/
dataEntry.js
File metadata and controls
134 lines (118 loc) · 4.61 KB
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
var express = require('express');
var http = require('http');
var url = require('url');
var bodyParser = require('body-parser');
var app = express();
var mysql = require('mysql');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Authorization, Content-Type');
next();
});
app.listen(4004);
console.log('Listening at 4004');
DATABASE_HOST = 'localhost';
DATABASE_NODE = 'scholarship';
DATABASE_USERNAME = 'root';
DATABASE_PASSWORD = 'cheese';
var connectionNode = mysql.createConnection({
host: DATABASE_HOST,
user: DATABASE_USERNAME,
password: DATABASE_PASSWORD,
database: DATABASE_NODE
});
app.post('/saveData', function(request, response) {
console.log('entered request')
var dataType=request.body.type;
if(dataType == 'state'){
console.log('i shoudlnt be here')
var statesData =request.body.data;
for(var i=0;i<statesData.length;i++){
(function(i) {
connectionNode.query('INSERT INTO State (state_id, state_name) VALUES(?,?);',[statesData[i].id, statesData[i].name],function(err, rows){
if(err) {
console.log(err);
}
});
})(i);
}
console.log(statesData.length+' states have been updated');
response.json('success');
}else if(dataType == 'district'){
var districtsData =request.body.data;
for(var i=0;i<districtsData.length;i++){
(function(i) {
connectionNode.query('INSERT INTO District (district_id, state_id, district_name) VALUES(?,?,?);',[districtsData[i].districtId, districtsData[i].stateId,districtsData[i].districtName],function(err, rows){
if(err) {
console.log(err);
}
if(i==districtsData.length-1){
console.log(districtsData.length+' districts have been updated');
response.json('success');
}
});
})(i);
}
}else if(dataType == 'mandal'){
var mandalsData =request.body.data;
for(var i=0;i<mandalsData.length;i++){
(function(i) {
connectionNode.query('INSERT INTO mandal (mandal_id, district_id, mandal_name) VALUES(?,?,?);',[mandalsData[i].mandal_id, mandalsData[i].district_id,mandalsData[i].mandal_name],function(err, rows){
if(err) {
console.log(err);
}
if(i==mandalsData.length-1){
console.log(mandalsData.length+' mandals have been updated');
response.json('success');
}
});
})(i);
}
}else if(dataType == 'studyLevel'){
var studyLevelData = request.body.data;
for(var i=0;i<studyLevelData.length;i++){
(function(i) {
connectionNode.query('INSERT INTO Study_level (level_id, level_name) VALUES(?,?);',[studyLevelData[i].id, studyLevelData[i].name],function(err, rows){
if(err) {
console.log(err);
}
if(i==studyLevelData.length-1){
console.log(studyLevelData.length+' mandals have been updated');
response.json('success');
}
});
})(i);
}
}
});
app.post('/saveInstitutes',function(request,response){
var institutesData = request.body.data;
connectionNode.query('INSERT INTO institution (institution_id, mandal_id, institution_name) VALUES(?,?,?);',[institutesData.instututeId, institutesData.mandalId,institutesData.mandalName],function(err, rows){
if(err) {
console.log(err);
}
response.json('success')
});
});
app.get('/getDistricts',function(request,response){
connectionNode.query('Select * from District',[],function(err, rows){
if(err) {
console.log(err);
}
else{
response.json(rows)
}
});
});
app.get('/getMandals',function(request,response){
connectionNode.query('SELECT mandal.mandal_id, mandal.district_id, District.state_id FROM scholarship.mandal,scholarship.District where District.district_id = mandal.district_id;',[],function(err, rows){
if(err) {
console.log(err);
}
else{
response.json(rows)
}
});
});