Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Qa quiz #316

Closed
wants to merge 28 commits into from
Closed
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Before you begin we recommend you read about the basic building blocks that asse
* AngularJS - Angular's [Official Website](http://angularjs.org/) is a great starting point. You can also use [Thinkster Popular Guide](http://www.thinkster.io/), and the [Egghead Videos](https://egghead.io/).
* Node.js - Start by going through [Node.js Official Website](http://nodejs.org/) and this [StackOverflow Thread](http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js), which should get you going with the Node.js platform in no time.

## Use MongoLabs - insert in server.js
var db = mongoose.connect('mongodb://cce:cce@ds039860.mongolab.com:39860/meandb', function(err) {
if (err) {
console.error('\x1b[31m', 'Could not connect to MongoLabDB!');
console.log(err);
}
});

## Prerequisites
Make sure you have installed all these prerequisites on your development machine.
Expand Down
129 changes: 129 additions & 0 deletions app/controllers/qas.server.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
'use strict';

/**
* Module dependencies...
*/
var mongoose = require('mongoose'),
Qa = mongoose.model('Qa'),
_ = require('lodash');

/**
* Get the error message from error object
*/
var getErrorMessage = function(err) {
var message = '';

if (err.code) {
switch (err.code) {
case 11000:
case 11001:
message = 'Qa already exists';
break;
default:
message = 'Something went wrong';
}
} else {
for (var errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
}
}

return message;
};

/**
* Create a Qa
*/
exports.create = function(req, res) {
var qa = new Qa(req.body);
qa.user = req.user;

qa.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(qa);
}
});
};

/**
* *
* Show the current Qa
*/
exports.read = function(req, res) {
res.jsonp(req.qa);
};

/**
* Update a Qa
*/
exports.update = function(req, res) {
var qa = req.qa ;

qa = _.extend(qa , req.body);

qa.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(qa);
}
});
};

/**
* Delete an Qa
*/
exports.delete = function(req, res) {
var qa = req.qa ;

qa.remove(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(qa);
}
});
};

/**
* List of Qas
*/
exports.list = function(req, res) { Qa.find().sort('-created').populate('user', 'displayName').exec(function(err, qas) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(qas);
}
});
};

/**
* Qa middleware
*/
exports.qaByID = function(req, res, next, id) { Qa.findById(id).populate('user', 'displayName').exec(function(err, qa) {
if (err) return next(err);
if (! qa) return next(new Error('Failed to load Qa ' + id));
req.qa = qa ;
next();
});
};

/**
* Qa authorization middleware
*/
exports.hasAuthorization = function(req, res, next) {
if (req.qa.user.id !== req.user.id) {
return res.send(403, 'User is not authorized');
}
next();
};
129 changes: 129 additions & 0 deletions app/controllers/quizzes.server.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
'use strict';

/**
* Module dependencies...
*/
var mongoose = require('mongoose'),
Quiz = mongoose.model('Quiz'),
_ = require('lodash');

/**
* Get the error message from error object
*/
var getErrorMessage = function(err) {
var message = '';

if (err.code) {
switch (err.code) {
case 11000:
case 11001:
message = 'Quiz already exists';
break;
default:
message = 'Something went wrong';
}
} else {
for (var errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
}
}

return message;
};

/**
* Create a Quiz
*/
exports.create = function(req, res) {
var quiz = new Quiz(req.body);
quiz.user = req.user;

quiz.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(quiz);
}
});
};

/**
* *
* Show the current Quiz
*/
exports.read = function(req, res) {
res.jsonp(req.quiz);
};

/**
* Update a Quiz
*/
exports.update = function(req, res) {
var quiz = req.quiz ;

quiz = _.extend(quiz , req.body);

quiz.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(quiz);
}
});
};

/**
* Delete an Quiz
*/
exports.delete = function(req, res) {
var quiz = req.quiz ;

quiz.remove(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(quiz);
}
});
};

/**
* List of quizzes
*/
exports.list = function(req, res) { Quiz.find().sort('-created').populate('user', 'displayName').exec(function(err, quizzes) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(quizzes);
}
});
};

/**
* Quiz middleware
*/
exports.quizByID = function(req, res, next, id) { Quiz.findById(id).populate('user', 'displayName').exec(function(err, quiz) {
if (err) return next(err);
if (! quiz) return next(new Error('Failed to load Quiz ' + id));
req.quiz = quiz ;
next();
});
};

/**
* Quiz authorization middleware
*/
exports.hasAuthorization = function(req, res, next) {
if (req.quiz.user.id !== req.user.id) {
return res.send(403, 'User is not authorized');
}
next();
};
Loading