-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestapi.js
174 lines (137 loc) · 4.34 KB
/
testapi.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 unirest = require("unirest");
var mysql = require('mysql');
//endpoint values are countries, statistics , countries : as per rapidapi
function EndPointSel(endpoint = "statistics", queryvalue = "india") {
const restep = "https://covid-193.p.rapidapi.com/" + endpoint;
var req = unirest("GET", restep);
var storedresp;
// req.query({
// "country": queryvalue
// //querykey , queryvalue
// //"country": "india"
// });
req.headers({
"x-rapidapi-host": "covid-193.p.rapidapi.com",
"x-rapidapi-key": "36191329b7mshcbbefaca44e0129p11f24djsn5d13d22f2b25"
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
console.log("after resolving!!! ready to return")
//storedresp = res
connecttodb(res);
//storedresp = JSON.stringify(storedresp)
//storedresp = JSON.parse(storedresp)
//console.log(res.body.response)
//return storedresp;
//return res.body.response;
});
//console.log(storedresp)
//return storedresp
}
function main() {
var dojo = EndPointSel();
//console.log(dojo);
//setTimeout(() => { "lets wait for dojo" }, 3000)
//console.log(dojo);
}
main();
function connecttodb(res) {
var connection = mysql.createConnection({
host: '35.223.207.83',
user: 'root',
password: 'npmgak@2020#',
database: "coviddb"
});
connection.connect(function (err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log("Connected to DB!")
console.log(res.body.response)
//return connection;
});
//CreateTable(connection);
//InsertData(connection,res.body.response);
updateData(connection,res.body.response);
FetchData(connection);
connection.end();
}
function CreateTable(connection){
var sql = "CREATE TABLE table1_covid_19 (id INT AUTO_INCREMENT PRIMARY KEY, name_Country VARCHAR(255), cases_new VARCHAR(10), cases_active int(20), cases_recovered int(20), cases_total int(20), deaths_new VARCHAR(10), deaths_total int (20))";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
}
function FetchData(connection){
var sql = "SELECT * from table1_covid_19";
connection.query(sql,function(error,result,fields){
console.log("select query result as follows! ");
console.log(fields);
})
}
function InsertData(connection,resp){
var sql = "INSERT INTO table1_covid_19 (id,name_Country,cases_new,cases_active,cases_recovered,cases_total,deaths_new,deaths_total) VALUES ?";
var values = [];
var j = 0;
for(i =1 ; i<resp.length;i++){
if(resp[i].country != "ALL")
{
values[j] = [i, resp[i].country,resp[i].cases.new,resp[i].cases.active,resp[i].cases.recovered,resp[i].cases.total,resp[i].deaths.new,resp[i].deaths.total];
j++;
}
}
connection.query(sql , [values], function (err, result) {
if (err)
{
console.log("error occured " + err.stack);
//throw err;
}
console.log("Number of records inserted: " + result.affectedRows);
});
// console.log("Insert sucessful.");
// });
}
function updateData(connection,resp){
var sql = "REPLACE INTO table1_covid_19 (name_Country,cases_new,cases_active,cases_recovered,cases_total,deaths_new,deaths_total) VALUES ?";
var values = [];
var j = 0;
for(i =1 ; i<resp.length;i++){
if(resp[i].country != "ALL")
{
values[j] = [resp[i].country,resp[i].cases.new,resp[i].cases.active,resp[i].cases.recovered,resp[i].cases.total,resp[i].deaths.new,resp[i].deaths.total];
j++;
}
}
connection.query(sql , [values], function (err, result) {
if (err)
{
console.log("error occured " + err.stack);
//throw err;
}
console.log("Number of records updated: " + result.affectedRows);
});
}
module.exports = {
EndPointSel,
connecttodb
};
/*liveCO.query("CREATE DATABASE coviddb", function (err, result) {
if (err) throw err;
console.log("Database created");
});*/
//console.log('connected as id ' + connection.threadId);
// execution point
// function main()
// {
// let response = EndPointSel();
// console.log(response);
// let liveCO = connecttodb();
// console.log(liveCO);
// // how / should do i capture resp / connection object here?
// //this is the best example to learn the async nature of JS
// // welcome to JS :)
// //liveCO.end();
// }
// main();