-
-
Save iamads/3509c40026b165d3a1aade3aa33cdc64 to your computer and use it in GitHub Desktop.
Assembly task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bodyParser = require('body-parser'); | |
const express = require('express') | |
const port = 3800; | |
const museumData = require('./data.json'); | |
const _ = require('lodash'); | |
const app = express(); | |
app.get("/", (req, res) => { | |
res.send("Hello World!"); | |
}); | |
const monthMap = { | |
'01': 'Jan', | |
'02': 'Feb', | |
'03': 'Mar', | |
'04': 'Apr', | |
'05': 'May', | |
'06': 'June', | |
'07': 'Jul', | |
'08': 'Aug', | |
'09': 'Sep', | |
'10': 'Oct', | |
'11': 'Nov', | |
'12': 'Dec' | |
}; | |
app.get("/api/visitors", (req, res) => { | |
const date = req.query.date || null; | |
const ignore = req.query.ignore || null; | |
if (_.isNull(date) ) { // TODO add check if date is valid | |
res.status(400).send({ error: "Missing Date"}); | |
} | |
const year = new Date(parseInt(date)).getFullYear(); | |
const month = new Date(parseInt(date)).getMonth(); | |
const formattedDate = `${year}-${month<10 ? `0${month}` : month}-01T00:00:00.000` | |
const data = museumData.find((data) => data["month"]===formattedDate); | |
console.log(data); | |
let max = -Infinity; | |
let min = +Infinity; | |
let maxMus = ''; | |
let minMus = ''; | |
// let ignoreCount; | |
// let validIgnore = false; | |
// console.log(data); | |
// if (!_.isNull(ignore)) { | |
// if (data[ignore]) { | |
// validIgnore = true; | |
// ignoreCount = data[ignore]; | |
// delete data[ignore] | |
// } | |
// } | |
let total = 0 | |
const keys = Object.keys(data); | |
for (let i = 0; i < keys.length ; i++) { | |
if (keys[i] !== 'month' && keys[i] !== ignore) { | |
if (parseInt(data[keys[i]]) > max) { | |
max = parseInt(data[keys[i]]); | |
maxMus = keys[i]; | |
} | |
if (parseInt(data[keys[i]]) < min) { | |
min = parseInt(data[keys[i]]); | |
minMus = keys[i]; | |
} | |
total += parseInt(data[keys[i]]); | |
console.log('total', total, 'data', data[keys[i]], keys[i]); | |
} | |
} | |
// const result = { maxVistor: max, maxMuseum: maxMus, minVisitor: min, minMuseum: minMus, totalVisitors: total}; | |
const result = { | |
year, | |
month: monthMap[month<10 ? `0${month}` : ''+month], | |
highest: {museum: maxMus, visitors: max}, | |
lowest: { museum: minMus, visitors: min }, | |
total | |
}; | |
if (!_.isNull(ignore)) { | |
if (data[ignore]) { | |
result["ignored"] = { museum: ignore, visitor: data[ignore]}; | |
} | |
} | |
res.status(200).send({attendance: result}); | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment