-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathposts.js
More file actions
46 lines (38 loc) · 943 Bytes
/
posts.js
File metadata and controls
46 lines (38 loc) · 943 Bytes
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
var User = require('./models').User;
var renderPost = function(post) {
return {
id: post.id,
content: post.content,
username: post.username
};
};
var renderPosts = function(user) {
var out = {
posts: [],
username: user.username
};
var posts = user.getPosts();
for(var i in posts) {
var post = posts[i];
out.posts.push(renderPost(post));
}
return out;
};
exports.userPosts = function *() {
var username = this.params.username;
var user = User.findByUsername(username);
if (!user) {
this.throw("No user found", 404);
}
this.status = 200;
this.body = renderPosts(user);
};
exports.createPost = function *() {
var user = this.passport.user;
var params = this.request.body || {};
var content = (params.content || "").trim();
if(!content) this.throw("No content provided", 422);
var post = user.addPost(content);
this.status = 201;
this.body = renderPost(post);
};