-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles.js
134 lines (118 loc) · 3.09 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
var spawn = require('child_process').spawn;
var currencyGetter = spawn('python', ['/Users/boolgom/Develop/Champong/packages/articles/server/controllers/currency.py']);
var currency = '';
currencyGetter.stdout.on('data', function(data){
currency += data;
});
/**
* 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();
});
};
exports.articleByName = function(req, res, next, id) {
Article.findOne({ 'title': id }, function (err, article) {
if (article) {
res.write(article.content);
res.end();
} else {
var python = spawn('python', ['/Users/boolgom/Develop/Champong/packages/articles/server/controllers/wrapper.py', id, currency]);
var output = '';
python.stdout.on('data', function(data){
output += data;
});
python.on('close', function(code){
if (code !== 0) { return res.send(500, code); }
res.write(output);
res.end();
article = new Article({title: id, content: output});
article.save();
});
}
});
};
/**
* 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.send('users/signup', {
errors: err.errors,
article: article
});
} else {
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.send('users/signup', {
errors: err.errors,
article: article
});
} else {
res.jsonp(article);
}
});
};
/**
* Delete an article
*/
exports.destroy = function(req, res) {
var article = req.article;
article.remove(function(err) {
if (err) {
return res.send('users/signup', {
errors: err.errors,
article: article
});
} 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', 'name username').exec(function(err, articles) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(articles);
}
});
};
exports.showWithName = function(req, res) {
res.jsonp(req.searchResult);
};