forked from linnovate/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles.js
97 lines (81 loc) · 1.84 KB
/
articles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Article = mongoose.model('Article'),
_ = require('lodash');
/**
* Find article by id
*/
exports.article = function(req, res, next, id) {
Article.load(id, function(err, article) {
if (err) return next(err);
if (!article) return next(new Error('Failed to load article ' + id));
req.article = article;
next();
});
};
/**
* Create an article
*/
exports.create = function(req, res) {
var article = new Article(req.body);
article.user = req.user;
article.save(function(err) {
if (err) {
return res.jsonp(500, {
error: 'Cannot save the article'
});
}
res.jsonp(article);
});
};
/**
* Update an article
*/
exports.update = function(req, res) {
var article = req.article;
article = _.extend(article, req.body);
article.save(function(err) {
if (err) {
return res.jsonp(500, {
error: 'Cannot update the article'
});
}
res.jsonp(article);
});
};
/**
* Delete an article
*/
exports.destroy = function(req, res) {
var article = req.article;
article.remove(function(err) {
if (err) {
return res.jsonp(500, {
error: 'Cannot delete the article'
});
}
res.jsonp(article);
});
};
/**
* Show an article
*/
exports.show = function(req, res) {
res.jsonp(req.article);
};
/**
* List of Articles
*/
exports.all = function(req, res) {
Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
if (err) {
return res.jsonp(500, {
error: 'Cannot list the articles'
});
}
res.jsonp(articles);
});
};