diff --git a/README.md b/README.md index 024e732..d498e2e 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,26 @@ Please refer to the provided document for the code challenge requirements. ## Available scripts - `npm start` - Start the application (Port 8081) + + +## Highlights of improvements + +- Instead of looping through data in the frontend side, have used that data in the backend part to fetch the data from API. So instead of multiple calls, we will get data in a single call. +- Data returned has all the values of the temperature and the beer now. +- Frontend is focused now more on rendering rather than on logical aspect. + +## What would you improve next if you had more time? + +- I could have refactored way more on the backend side. The status logic could have been added in backend side. +- I could have added end to end testing as well. +- Test cases not added in backend + +## Questions you would ask and your own answers and assumptions + +- Instead of using setInterval, web sockets could have been used? Probably + +## Explanations of decisions or the approach you took + +- In order to make frontend light, I decided to remove multiple calls. Instead I moved that logic to backend so that only one API is called at a time. + +## Any other notes you feel relevant to evaluate your test improvements diff --git a/index.js b/index.js index ad2f5ad..018b23c 100644 --- a/index.js +++ b/index.js @@ -5,14 +5,63 @@ const cors = require('cors'); const app = express(); const port = 8081; +const beerData = [ + { + id: '1', + name: 'Pilsner', + minimumTemperature: 4, + maximumTemperature: 6, + }, + { + id: '2', + name: 'IPA', + minimumTemperature: 5, + maximumTemperature: 6, + }, + { + id: '3', + name: 'Lager', + minimumTemperature: 4, + maximumTemperature: 7, + }, + { + id: '4', + name: 'Stout', + minimumTemperature: 6, + maximumTemperature: 8, + }, + { + id: '5', + name: 'Wheat beer', + minimumTemperature: 3, + maximumTemperature: 5, + }, + { + id: '6', + name: 'Pale Ale', + minimumTemperature: 4, + maximumTemperature: 6, + }, +]; + app.use(cors()); -app.get('/temperature/:id', (req, res) => { - fetch( - `https://hasydbj5c4gpa2oozfpjpc677a0hxuob.lambda-url.ap-southeast-2.on.aws/sensor/${req.params.id}` - ) - .then((response) => response.json()) - .then((response) => res.send(response)); +app.get('/temperature', async (req, res) => { + try { + const dataArray = []; + + for (const item of beerData) { + const response = await fetch(`https://hasydbj5c4gpa2oozfpjpc677a0hxuob.lambda-url.ap-southeast-2.on.aws/sensor/${item.id}`); + const data = await response.json(); + const newData ={...data, ...item}; + dataArray.push(newData); + } + + res.status(200).send(dataArray); + } catch (error) { + console.error(error); + res.status(500).send('Internal Server Error'); + } }); app.listen(port, () => {