Created
December 7, 2020 07:51
-
-
Save aatifbandey/fd9b88cbc9fce4447fc8fc4c7b08159d to your computer and use it in GitHub Desktop.
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 Router = require('koa-router'); | |
const DataSet = require('./models/report'); | |
const saveData = async ctx => { | |
const body = ctx.request.body; | |
if (!body) { | |
return { | |
status: 400, | |
message: 'You must provide data!', | |
}; | |
} | |
const data = new DataSet(body); | |
if (!data) { | |
return { | |
status: 400, | |
message: 'Bad data', | |
}; | |
} | |
const result = await data | |
.save() | |
.then(() => { | |
return { | |
id: data._id, | |
status: 200, | |
message: `Data inserted! at ${new Date().toISOString()}`, | |
}; | |
}) | |
.catch(error => { | |
console.log('Fail', error); | |
return { | |
id: data._id, | |
status: 400, | |
message: 'Data not inserted!', | |
}; | |
}); | |
return result; | |
}; | |
router.post('/api/save/data', async (ctx /* next */) => { | |
const { status, message } = await saveData(ctx); | |
console.log('Response', status, message); | |
ctx.body = { | |
status, | |
message, | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment