Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.DS_Store
28 changes: 26 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,49 @@ const app = express();

const port = process.env.PORT || 3001;

const { getCountryTable } = require('./lib/byCountry');
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
const { getCompleteTable } = require('./lib/corona');

app.get('/', (req, res) => {
const format = req.query.format ? req.query.format : '';

if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
return res.json(result);
}).catch(error => res.send(error));
}

return getCompleteTable().then(result => {
return res.send(result);
}).catch(error => res.send(error));
});

app.get('/:country', (req, res) => {
let { country } = req.params;
const { country } = req.params;
const format = req.query.format ? req.query.format : '';

if (!country || country === 'all') {
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
return res.json(result);
}).catch(error => res.send(error));
}

return getCompleteTable().then(result => {
return res.send(result);
}).catch(error => res.send(error));
}

if (format.toLowerCase() === 'json') {
return getJSONDataForCountry(country).then(result => {
return res.json(result);
}).catch(error => res.send(error));
}

return getCountryTable(country).then(result => {
return res.send(result);
}).catch(error => res.send(error));
});


app.listen(port, () => console.log(`Running on ${port}`));
13 changes: 13 additions & 0 deletions lib/byCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ function extraStats(dataArr) {
);
}

exports.getJSONData = async () => {
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
return result.data;
}

exports.getJSONDataForCountry = async (country) => {
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
const { latest, confirmed, deaths, recovered } = result.data;
const countryData = getDataByState(confirmed, deaths, recovered, country)
.filter(obj => obj.country === country);
return countryData;
}

exports.getCountryTable = async (country) => {
const head = [
'',
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"corona": "./bin/index.js"
},
"scripts": {
"dev": "nodemon app.js",
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -25,5 +26,8 @@
"humanize-number": "0.0.2",
"lodash": "^4.17.15",
"moment": "^2.24.0"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}
Loading