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-02-10 13:24:01 +02:00
|
|
|
Article = mongoose.model('Article'),
|
|
|
|
|
_ = require('lodash');
|
2014-04-23 03:57:09 +03:00
|
|
|
|
2014-04-21 02:32:59 +03:00
|
|
|
/**
|
|
|
|
|
* Get the error message from error object
|
|
|
|
|
*/
|
|
|
|
|
var getErrorMessage = function(err) {
|
|
|
|
|
var message = '';
|
|
|
|
|
|
|
|
|
|
if (err.code) {
|
|
|
|
|
switch (err.code) {
|
|
|
|
|
case 11000:
|
|
|
|
|
case 11001:
|
2014-04-23 03:57:09 +03:00
|
|
|
message = 'Article already exists';
|
2014-04-21 02:32:59 +03:00
|
|
|
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;
|
|
|
|
|
};
|
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-04-21 02:32:59 +03:00
|
|
|
return res.send(400, {
|
|
|
|
|
message: getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.jsonp(article);
|
|
|
|
|
}
|
|
|
|
|
});
|
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-02-10 13:24:01 +02:00
|
|
|
res.jsonp(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-04-21 02:32:59 +03:00
|
|
|
return res.send(400, {
|
|
|
|
|
message: getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.jsonp(article);
|
|
|
|
|
}
|
|
|
|
|
});
|
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-04-21 02:32:59 +03:00
|
|
|
return res.send(400, {
|
|
|
|
|
message: getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.jsonp(article);
|
|
|
|
|
}
|
|
|
|
|
});
|
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-04-21 02:32:59 +03:00
|
|
|
return res.send(400, {
|
|
|
|
|
message: getErrorMessage(err)
|
2014-02-10 13:24:01 +02:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.jsonp(articles);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-02-10 13:09:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Article middleware
|
|
|
|
|
*/
|
|
|
|
|
exports.articleByID = function(req, res, next, id) {
|
2014-04-02 19:16:40 +03:00
|
|
|
Article.findById(id).populate('user', 'displayName').exec(function(err, article) {
|
2014-02-10 13:24:01 +02:00
|
|
|
if (err) return next(err);
|
|
|
|
|
if (!article) return next(new Error('Failed to load article ' + id));
|
|
|
|
|
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-04-21 22:52:34 +03:00
|
|
|
return res.send(403, {
|
|
|
|
|
message: 'User is not authorized'
|
|
|
|
|
});
|
2014-02-10 13:24:01 +02:00
|
|
|
}
|
|
|
|
|
next();
|
2013-12-20 14:14:54 +02:00
|
|
|
};
|