Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrangreat committed Nov 6, 2017
1 parent a519108 commit 9fb8f92
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ typings/
/db

# Code Editor Configuration file
.vscode
.vscode

# CSV test file
assessmentItem.csv
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const bunyanMiddleware = require('bunyan-middleware');
const ping = require('./ping');
const question = require('./question');
const history = require('./history');
const bulkUpload = require('./bulk_upload');

const app = express();
const logger = bunyan.createLogger({ name: 'Question-Authoring-API' });
Expand All @@ -31,5 +32,6 @@ app.use(bunyanMiddleware({
app.use('/ping', ping);
app.use('/history', history);
app.use('/question', question);
app.use('/bulkupload', bulkUpload);

module.exports = app;
42 changes: 42 additions & 0 deletions bulk_upload/csv.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const questionController = require('../question/question.controller');
const config = require('../config');
const fs = require('fs');
const csv = require('fast-csv');
const _ = require('lodash');

const readAndPublish = async () => {
const stream = fs.createReadStream(config.fileUploadPath);
let publishedQuestion = '';
await csv
.fromStream(stream, { objectMode: true, headers: true })
.validate(data =>
data.concept && data.content && data.expectedOutcome && data.player && data.evaluator)
.on('data-invalid', () => {
console.log('Error: Primary fields are missing!!!');
})
.on('data', async (data) => {
let assessmentItem = data;
let primaryObject = {};
primaryObject = _.pick(assessmentItem, ['concept', 'content', 'expectedOutcome', 'player', 'evaluator']);
_.forOwnRight(assessmentItem, (value, key) => {
if (key === 'concept' || key === 'content' || key === 'expectedOutcome' || key === 'player' || key === 'evaluator') {
_.unset(assessmentItem, key);
}
});
const secondaryObject = assessmentItem;
primaryObject.question = secondaryObject;
assessmentItem = primaryObject;
const initialisedQuestion = await questionController.initQuestion();
assessmentItem.id = initialisedQuestion.id;
publishedQuestion = await questionController.publishQuestion(assessmentItem);
return publishedQuestion;
})
.on('error', (err) => {
console.log(`Error is ${err}`);
});
};

module.exports = {
readAndPublish,
};

11 changes: 11 additions & 0 deletions bulk_upload/csv.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const csvController = require('./csv.controller');

const router = express.Router();

router.get('/csvUpload', (req, res) => {
const publishedQuestion = csvController.readAndPublish();
res.json(publishedQuestion);
});

module.exports = router;
1 change: 1 addition & 0 deletions bulk_upload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./csv.router');
1 change: 1 addition & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const config = {
PORT: process.env.PORT || 4000,
MONGODB_URL: process.env.MONGODB_URL || 'localhost:29017/assessment_item',
fileUploadPath: process.env.FILE_UPLOAD_PATH || 'assessmentItem.csv',
};

module.exports = config;
97 changes: 95 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"bunyan-middleware": "^0.8.0",
"eslint": "^4.9.0",
"express": "^4.16.2",
"fast-csv": "^2.4.1",
"json-diff-rfc6902": "^1.3.3",
"lodash": "^4.17.4",
"mongoose": "^4.12.4",
"simple-neo4j-wrapper": "^1.0.5",
"sinon": "^4.0.1"
Expand Down

0 comments on commit 9fb8f92

Please sign in to comment.