Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : 4, // {int} Number of spaces to use for indentation
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // true: Require all defined variables be used
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line

// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : true, // true: Allow ES5 syntax (ex: getters and setters)
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
"notypeof" : false, // true: Tolerate invalid typeof operator values
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function

// Environments
"browser" : false, // Web Browser (window, document, etc)
"browserify" : false, // Browserify (node.js code in the browser)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jasmine" : false, // Jasmine
"jquery" : false, // jQuery
"mocha" : true, // Mocha
"mootools" : false, // MooTools
"node" : true, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"prototypejs" : false, // Prototype and Scriptaculous
"qunit" : false, // QUnit
"rhino" : false, // Rhino
"shelljs" : false, // ShellJS
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface

// Custom Globals
"globals" : {} // additional predefined global variables
}
4 changes: 2 additions & 2 deletions app/otherOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ module.exports = function () {
}
res.json(output);
};
return { getCategories: getCategories };
};
return {getCategories: getCategories};
};
77 changes: 42 additions & 35 deletions app/questionOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,52 @@ module.exports = function () {
sortObject.time = -1;
}

Question.find(query).sort(sortObject).skip(skip).limit(questionsPerPage).populate('asker', 'firstName _id').exec(function (err, questions) {
var output = Question.formatQuestionsList(questions);
res.json(output);
});
Question.find(query)
.sort(sortObject)
.skip(skip)
.limit(questionsPerPage)
.populate('asker', 'firstName _id')
.exec(function (err, questions) {
var output = Question.formatQuestionsList(questions);
res.json(output);
});
};

var getQuestionById = function (req, res) {
var id = req.params.id;
if (_.isEmpty(id)) {
return res.status(400).json({error: Constants.ERROR.MISSING.QUESTION_ID});
}
Question.findById(id).populate('answers').populate('asker').exec(function (err, item) {
if (err) {
return res.status(400).json({error: Constants.ERROR.QUESTION_BY_ID});
} else {
var currentOutput = {};
currentOutput.questionId = item._id;
currentOutput.name = item.name;
currentOutput.description = item.text;
currentOutput.askerName = item.asker.firstName;
currentOutput.askerId = item.asker._id;
currentOutput.askerEmail = item.asker.email;
currentOutput.category = item.category;
currentOutput.numAnswers = item.answers.length;
currentOutput.answers = [];
item.answers.forEach(function (answer) {
var currentAnswer = {};
currentAnswer.answererId = answer.answerer._id;
currentAnswer.answererName = answer.answererName;
currentAnswer.text = answer.text;
currentAnswer.answerId = answer._id;
currentAnswer.timeAnswered = answer.time;
currentOutput.answers.push(currentAnswer);
});
currentOutput.favourites = item.favourites;
currentOutput.numVotes = item.votes;
currentOutput.timeAsked = item.time;
res.json(currentOutput);
}
});
Question.findById(id)
.populate('answers').populate('asker').exec(function (err, item) {
if (err) {
return res.status(400).json({error: Constants.ERROR.QUESTION_BY_ID});
} else {
var currentOutput = {};
currentOutput.questionId = item._id;
currentOutput.name = item.name;
currentOutput.description = item.text;
currentOutput.askerName = item.asker.firstName;
currentOutput.askerId = item.asker._id;
currentOutput.askerEmail = item.asker.email;
currentOutput.category = item.category;
currentOutput.numAnswers = item.answers.length;
currentOutput.answers = [];
item.answers.forEach(function (answer) {
var currentAnswer = {};
currentAnswer.answererId = answer.answerer._id;
currentAnswer.answererName = answer.answererName;
currentAnswer.text = answer.text;
currentAnswer.answerId = answer._id;
currentAnswer.timeAnswered = answer.time;
currentOutput.answers.push(currentAnswer);
});
currentOutput.favourites = item.favourites;
currentOutput.numVotes = item.votes;
currentOutput.timeAsked = item.time;
res.json(currentOutput);
}
});
};

var deleteQuestionById = function (req, res) {
Expand All @@ -91,7 +97,7 @@ module.exports = function () {
res.status(400).json({error: Constants.ERROR.QUESTION_BY_ID});
} else {
async.each(doc.answers, function (answerId, done) {
Answer.remove({_id:answerId}, done);
Answer.remove({_id: answerId}, done);
}, function (err) {
doc.remove();
res.status(204).send();
Expand Down Expand Up @@ -145,5 +151,6 @@ module.exports = function () {
getQuestionSet: getQuestionSet,
getQuestionById: getQuestionById,
deleteQuestionById: deleteQuestionById,
postQuestion: postQuestion };
postQuestion: postQuestion
};
};
4 changes: 2 additions & 2 deletions app/userOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = function (passport) {
done();
}
});
}, function(err) {
}, function (err) {
if (err) return serverError(res);
return res.json(out);
});
Expand Down Expand Up @@ -97,7 +97,7 @@ module.exports = function (passport) {
done();
}
});
}, function(err) {
}, function (err) {
if (err) return serverError(res);
return res.json(out);
});
Expand Down
19 changes: 8 additions & 11 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
var mongoose = require('mongoose');

module.exports = function(connection_string){

mongoose.connect(connection_string);
var db = mongoose.connection;
db.on('error', function(){
console.log("database could not open");
module.exports = function (connection_string) {
mongoose.connect(connection_string);
var db = mongoose.connection;
db.on('error', function () {
console.log("database could not open");
});
db.once('open', function callback() {
console.log("database open");
});
db.once('open', function callback () {
console.log("database open");
});


};
4 changes: 2 additions & 2 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = function (passport) {

// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'email': email }, function (err, user) {
User.findOne({'email': email}, function (err, user) {
console.log("db query done");
// if there are any errors, return the error
if (err) {
Expand Down Expand Up @@ -90,7 +90,7 @@ module.exports = function (passport) {
email = email.toLowerCase();
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'email': email }, function (err, user) {
User.findOne({'email': email}, function (err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
Expand Down
60 changes: 30 additions & 30 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
exports.ERROR = {
MISSING: {
TOKEN: "Please provide a valid access token",
QUESTION_ID: "Please provide 'questionId' property.",
QUESTION_TITLE: "Please provide 'questionTitle' property.",
QUESTION_DESCRIPTION: "Please provide 'questionDescription' property.",
QUESTION_CATEGORY: "Please provide 'categoryIndex' property.",
ANSWER_BODY: "Please provide 'answerBody' property.",
EMAIL: "Please provide an email",
PASSWORD: "Please provide a password",
FIRST_NAME: "Please provide a firstName."
},
INVALID: {
TOKEN: "Unauthorized. Invalid token.",
EMAIL_OR_PASSWORD: "Invalid email or password",
HTTP_METHOD: "Invalid HTTP method or path, please refer to the API Documentation."
},
SAVE: {
ANSWER: "Could not save answer.",
QUESTION: "Could not save question.",
USER: "Could not save user."
},
QUESTION_BY_ID: "Could not find question. Please form your requests like the following: api/question/QUESTION_ID",
EMAIL_IN_USE: "The provided email is already in use",
FEATURE_NOT_IMPLEMENTED: "This feature is not yet implemented."
MISSING: {
TOKEN: "Please provide a valid access token",
QUESTION_ID: "Please provide 'questionId' property.",
QUESTION_TITLE: "Please provide 'questionTitle' property.",
QUESTION_DESCRIPTION: "Please provide 'questionDescription' property.",
QUESTION_CATEGORY: "Please provide 'categoryIndex' property.",
ANSWER_BODY: "Please provide 'answerBody' property.",
EMAIL: "Please provide an email",
PASSWORD: "Please provide a password",
FIRST_NAME: "Please provide a firstName."
},
INVALID: {
TOKEN: "Unauthorized. Invalid token.",
EMAIL_OR_PASSWORD: "Invalid email or password",
HTTP_METHOD: "Invalid HTTP method or path, please refer to the API Documentation."
},
SAVE: {
ANSWER: "Could not save answer.",
QUESTION: "Could not save question.",
USER: "Could not save user."
},
QUESTION_BY_ID: "Could not find question. Please form your requests like the following: api/question/QUESTION_ID",
EMAIL_IN_USE: "The provided email is already in use",
FEATURE_NOT_IMPLEMENTED: "This feature is not yet implemented."
};

exports.SUCCESS = {
SAVE: {
ANSWER: "Successfully saved answer.",
QUESTION: "Successfully saved question.",
USER: "Successfully saved user."
}
SAVE: {
ANSWER: "Successfully saved answer.",
QUESTION: "Successfully saved question.",
USER: "Successfully saved user."
}
};

exports.URLS = {
API_DOC: "http://docs.waterlooanswers.apiary.io/"
API_DOC: "http://docs.waterlooanswers.apiary.io/"
}
544 changes: 298 additions & 246 deletions index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions models/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ var answerSchema = mongoose.Schema({
{type: mongoose.Schema.Types.ObjectId, ref: 'User'}
],
text: String,
time: { type: Date, default: Date.now }
time: {type: Date, default: Date.now}
});

answerSchema.statics.format = function(answer, done) {
answerSchema.statics.format = function (answer, done) {
var out = {};
Question.findOne({answers: answer._id}, function (err, questionAnswered) {
if (err || !questionAnswered) {
Expand Down
14 changes: 7 additions & 7 deletions models/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ global.questionCategories = [
];

var questionSchema = mongoose.Schema({
asker: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
name: String,
favourites: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
text: String,
time : { type : Date, default: Date.now },
answers: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Answer' }],
asker: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
name: String,
favourites: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
text: String,
time: {type: Date, default: Date.now},
answers: [{type: mongoose.Schema.Types.ObjectId, ref: 'Answer'}],
viewers: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }
{type: mongoose.Schema.Types.ObjectId, ref: 'User'}
],
category: {type: String, enum: global.questionCategories}
});
Expand Down
2 changes: 1 addition & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var userSchema = mongoose.Schema({
password: String,
graduationYear: Number,
program: String,
dateCreated: { type: Date, default: Date.now }
dateCreated: {type: Date, default: Date.now}
});

// generating a hash
Expand Down
Loading