2013-12-25 16:36:33 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2013-07-09 20:40:39 +03:00
|
|
|
/**
|
|
|
|
|
* Module dependencies.
|
|
|
|
|
*/
|
2013-08-16 15:23:09 -04:00
|
|
|
var mongoose = require('mongoose'),
|
2014-10-24 17:48:07 +03:00
|
|
|
errorHandler = require('./errors.server.controller'),
|
2014-02-10 13:24:01 +02:00
|
|
|
Article = mongoose.model('Article'),
|
|
|
|
|
_ = require('lodash');
|
2014-04-23 03:57:09 +03:00
|
|
|
|
2013-07-09 20:40:39 +03:00
|
|
|
/**
|
|
|
|
|
* Create a article
|
|
|
|
|
*/
|
2013-09-29 16:00:23 +03:00
|
|
|
exports.create = function(req, res) {
|
2014-02-10 13:24:01 +02:00
|
|
|
var article = new Article(req.body);
|
|
|
|
|
article.user = req.user;
|
2013-09-10 16:28:47 +03:00
|
|
|
|
2014-02-10 13:24:01 +02:00
|
|
|
article.save(function(err) {
|
|
|
|
|
if (err) {
|
2014-08-02 21:29:38 +03:00
|
|
|
return res.status(400).send({
|
2014-07-31 11:27:14 +03:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
2014-10-14 20:24:28 +03:00
|
|
|
res.json(article);
|
2014-02-10 13:24:01 +02:00
|
|
|
}
|
|
|
|
|
});
|
2013-08-16 15:23:09 -04:00
|
|
|
};
|
2013-07-09 20:40:39 +03:00
|
|
|
|
2014-02-10 13:09:25 +02:00
|
|
|
/**
|
|
|
|
|
* Show the current article
|
|
|
|
|
*/
|
|
|
|
|
exports.read = function(req, res) {
|
2014-10-14 20:24:28 +03:00
|
|
|
res.json(req.article);
|
2014-02-10 13:09:25 +02:00
|
|
|
};
|
|
|
|
|
|
2013-07-09 20:40:39 +03:00
|
|
|
/**
|
|
|
|
|
* Update a article
|
|
|
|
|
*/
|
2013-08-17 01:06:17 +03:00
|
|
|
exports.update = function(req, res) {
|
2014-02-10 13:24:01 +02:00
|
|
|
var article = req.article;
|
2013-08-16 15:23:09 -04:00
|
|
|
|
2014-02-10 13:24:01 +02:00
|
|
|
article = _.extend(article, req.body);
|
2013-07-09 20:40:39 +03:00
|
|
|
|
2014-02-10 13:24:01 +02:00
|
|
|
article.save(function(err) {
|
|
|
|
|
if (err) {
|
2014-08-02 21:29:38 +03:00
|
|
|
return res.status(400).send({
|
2014-07-31 11:27:14 +03:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
2014-10-14 20:24:28 +03:00
|
|
|
res.json(article);
|
2014-02-10 13:24:01 +02:00
|
|
|
}
|
|
|
|
|
});
|
2013-08-16 15:23:09 -04:00
|
|
|
};
|
2013-07-09 20:40:39 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete an article
|
|
|
|
|
*/
|
2014-02-10 13:09:25 +02:00
|
|
|
exports.delete = function(req, res) {
|
2014-02-10 13:24:01 +02:00
|
|
|
var article = req.article;
|
2013-08-16 15:23:09 -04:00
|
|
|
|
2014-02-10 13:24:01 +02:00
|
|
|
article.remove(function(err) {
|
|
|
|
|
if (err) {
|
2014-08-02 21:29:38 +03:00
|
|
|
return res.status(400).send({
|
2014-07-31 11:27:14 +03:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
2014-10-14 20:24:28 +03:00
|
|
|
res.json(article);
|
2014-02-10 13:24:01 +02:00
|
|
|
}
|
|
|
|
|
});
|
2013-08-16 15:23:09 -04:00
|
|
|
};
|
2013-07-09 20:40:39 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List of Articles
|
|
|
|
|
*/
|
2014-02-10 13:09:25 +02:00
|
|
|
exports.list = function(req, res) {
|
2014-02-10 13:24:01 +02:00
|
|
|
Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
|
|
|
|
|
if (err) {
|
2014-08-02 21:29:38 +03:00
|
|
|
return res.status(400).send({
|
2014-07-31 11:27:14 +03:00
|
|
|
message: errorHandler.getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
2014-10-14 20:24:28 +03:00
|
|
|
res.json(articles);
|
2014-02-10 13:24:01 +02:00
|
|
|
}
|
|
|
|
|
});
|
2014-02-10 13:09:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Article middleware
|
|
|
|
|
*/
|
|
|
|
|
exports.articleByID = function(req, res, next, id) {
|
2015-01-12 23:22:25 +02:00
|
|
|
|
|
|
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
|
|
|
return res.status(500).send({
|
|
|
|
|
message: 'Article is invalid'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 19:16:40 +03:00
|
|
|
Article.findById(id).populate('user', 'displayName').exec(function(err, article) {
|
2015-01-12 23:22:25 +02:00
|
|
|
if (err) return next(err);
|
|
|
|
|
if (!article) {
|
|
|
|
|
res.status(404).send({
|
2015-01-09 11:06:17 +02:00
|
|
|
message: 'Article not found'
|
2015-01-12 23:22:25 +02:00
|
|
|
});
|
2015-01-09 11:06:17 +02:00
|
|
|
}
|
2014-02-10 13:24:01 +02:00
|
|
|
req.article = article;
|
|
|
|
|
next();
|
|
|
|
|
});
|
2014-02-10 13:09:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Article authorization middleware
|
|
|
|
|
*/
|
|
|
|
|
exports.hasAuthorization = function(req, res, next) {
|
2014-02-10 13:24:01 +02:00
|
|
|
if (req.article.user.id !== req.user.id) {
|
2014-08-02 21:29:38 +03:00
|
|
|
return res.status(403).send({
|
2014-04-21 22:52:34 +03:00
|
|
|
message: 'User is not authorized'
|
|
|
|
|
});
|
2014-02-10 13:24:01 +02:00
|
|
|
}
|
|
|
|
|
next();
|
2013-12-20 14:14:54 +02:00
|
|
|
};
|