Skip to content

Instantly share code, notes, and snippets.

@aatifbandey
Created December 7, 2020 07:51
Show Gist options
  • Save aatifbandey/fd9b88cbc9fce4447fc8fc4c7b08159d to your computer and use it in GitHub Desktop.
Save aatifbandey/fd9b88cbc9fce4447fc8fc4c7b08159d to your computer and use it in GitHub Desktop.
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