-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles.js
80 lines (69 loc) · 1.37 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
/**
* Module dependencies.
*/
var mongoose = require('mongoose')
, async = require('async')
, Article = mongoose.model('Article')
, _ = require('underscore')
/**
* Find article by id
*/
exports.article = function(req, res, next, id){
var User = mongoose.model('User')
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 a article
*/
exports.create = function (req, res) {
var article = new Article(req.body)
article.user = req.user
article.save()
res.jsonp(article)
}
/**
* Update a article
*/
exports.update = function(req, res){
var article = req.article
article = _.extend(article, req.body)
article.save(function(err) {
res.jsonp(article)
})
}
/**
* Delete an article
*/
exports.destroy = function(req, res){
var article = req.article
article.remove(function(err){
if (err) {
res.render('error', {status: 500});
} else {
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').exec(function(err, articles) {
if (err) {
res.render('error', {status: 500});
} else {
res.jsonp(articles);
}
});
}