Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
# sourcesage-dev-test
# qna-frontend-complete

For Data Science roles:
Create any application on iOS or android that showcase your knowledge of machine learning/data analytics.
Make use of price data of commodities by dropping an email to jian.sim@sourcesage.co
Live test site on http://104.199.175.158/

For Software Engineering roles:
Create a Q & A application that has the following functionality.
1. a user can ask a question
2. a user can answer a question
Real-time is preferred.
1. Fire up several browsers
2. Add Questions
3. Add Answers
4. Content should update on all browsers in real-time

You can write this application in whatever languages you prefer.
For backend developers: We like Python, Erlang (but we really don't mind if you write it in other languages)
For iOS and Android developers: Do you even have another choice?

You should fork this repository and put your work there.
This exercise should take no longer than 4 hours. However, you are not timed
and you can send us a pull request whenever you finish. (If you have something
live going on, even better!)
Improvements:
- Store questions and answers on a relational database
- Code refactoring (for both angular and nodejs)
89 changes: 89 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var NodeCache = require('node-cache');
var cache = new NodeCache({ checkPeriod: 120 });
var _ = require('lodash');
var uuid = require('node-uuid');

/**
* Common middleware.
*/
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

// TODO: change to a map with uuid as key for faster lookup.
var questions = [];

cache.set('questions', questions); // Using memory cache to store questions/answers.

/**
* Routes.
*/
app.get('/', function(req, res) {
return res.sendFile(__dirname + '/public/index.html');
});

app.get('/api/questions', function(req, res) {
return res.json(cache.get('questions'));
});

app.post('/api/questions', function(req, res) {
var question = req.body;
var questions = cache.get('questions');

question = new QuestionModel(question.questionString);

questions.push(question);

cache.set('questions', questions, function() {
io.emit('newQuestionAdded', question);
return res.json(question);
});
});

app.post('/api/questions/:questionId/answers', function(req, res) {
var questions = cache.get('questions');
var qid = _.findIndex(questions, function(question) {
return question.id == req.params.questionId;
});
var question = questions[qid];
var answer = new AnswerModel(req.body.answerString, question.id);

question.answers.push(answer);

cache.set('questions', questions, function() {
io.emit('newAnswerAdded', { answer: answer, question: question });
res.json(answer);
});
});

/**
* Models.
*/
function QuestionModel(questionString) {
return {
id: uuid.v4(),
timestamp: Date.now(),
questionString: questionString || '',
answers: []
}
}

function AnswerModel(answerString, questionParentId) {
return {
id: uuid.v4(),
timestamp: Date.now(),
answerString: answerString || '',
questionParentId: questionParentId
}
}



var server = app.listen(80);

var io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log('got a connection');
});
1 change: 1 addition & 0 deletions node_modules/.bin/uuid

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

Loading