/
blog.js
140 lines (129 loc) · 4.81 KB
/
blog.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
135
136
137
138
139
140
var loopback = require('loopback');
var slugify = require('slugify');
module.exports = function(Blog) {
// Register a 'publish' remote method: /blogs/some-id/publish
Blog.remoteMethod(
'publish',
{
http: {path: '/:id/publish', verb: 'put'},
accepts: {arg: 'id', type: 'string', required: true, http: { source: 'path' }},
returns: {root: true, type: 'object'},
description: 'Marks a blog as published.'
}
);
// the actual function called by the route to do the work
Blog.publish = function(id, cb) {
Blog.findById(id, function(err, record){
record.updateAttributes({isPublished: true, publishedDate: new Date()}, function(err, instance) {
if (err) cb(err);
if (!err) cb(null, instance);
})
})
};
// PUBLISH
// Register an 'upvote' remote method: /blogs/some-id/upvote
Blog.remoteMethod(
'upvote',
{
http: {path: '/:id/upvote', verb: 'post'},
accepts: {arg: 'id', type: 'string', required: true, http: { source: 'path' }},
returns: {root: true, type: 'object'},
description: 'Marks a blog as upvoted.'
}
);
// Remote hook called before running function
Blog.beforeRemote('upvote', function(ctx, user, next) {
Blog.findById(ctx.req.params.id, function(err, record){
// do not let the user upvote their own record
if (record.authorId === ctx.req.accessToken.userId) {
var err = new Error("User cannot upvote their own blog post.");
err.status = 403;
next(err);
// do no let the user upvote a comment more than once
} else if (record.upvotes.indexOf(ctx.req.accessToken.userId) != -1) {
var err = new Error("User has already upvoted the blog.");
err.status = 403;
next(err);
} else {
next();
}
})
});
// the actual function called by the route to do the work
Blog.upvote = function(id, cb) {
// get the current context
var ctx = loopback.getCurrentContext();
Blog.findById(id, function(err, record){
// get the calling user who 'upvoted' it from the context
record.upvotes.push(ctx.active.accessToken.userId);
record.updateAttributes({numOfUpVotes: record.upvotes.length, upvotes: record.upvotes}, function(err, instance) {
if (err) cb(err);
if (!err) cb(null, instance);
})
})
};
// UPVOTE
// Register a 'downvote' remote method: /blogs/some-id/downvote
// Note: essentially the same code as upvote
Blog.remoteMethod(
'downvote',
{
http: {path: '/:id/downvote', verb: 'post'},
accepts: {arg: 'id', type: 'string', required: true, http: { source: 'path' }},
returns: {root: true, type: 'object'},
description: 'Marks a blog as downvoted.'
}
);
// Remote hook called before running function
Blog.beforeRemote('downvote', function(ctx, user, next) {
Blog.findById(ctx.req.params.id, function(err, record){
// do not let the user downvote their own record
if (record.authorId === ctx.req.accessToken.userId) {
var err = new Error("User cannot downvote their own blog post.");
err.status = 403;
next(err);
// do no let the user downvote a comment more than once
} else if (record.downvotes.indexOf(ctx.req.accessToken.userId) != -1) {
var err = new Error("User has already downvoted the blog.");
err.status = 403;
next(err);
} else {
next();
}
})
});
// the actual function called by the route to do the work
Blog.downvote = function(id, cb) {
// get the current context
var ctx = loopback.getCurrentContext();
Blog.findById(id, function(err, record){
// get the calling user who 'downvoted' it from the context
record.downvotes.push(ctx.active.accessToken.userId);
record.updateAttributes({numOfDownVotes: record.downvotes.length, downvote: record.downvotes}, function(err, instance) {
if (err) cb(err);
if (!err) cb(null, instance);
})
})
};
// DOWNVOTE
// Call an operation hook that runs before each record is saved
Blog.observe('before save', function filterProperties(ctx, next) {
// TODO ensure the slug is unique per user
// If there is a record in the context
if (ctx.instance) {
// slugify the title
if (ctx.instance.slug === undefined) {
ctx.instance.slug = slugify(ctx.instance.title).toLowerCase();
}
// Ensure a valid createdDate
if (ctx.instance.createdDate === undefined) {
ctx.instance.createdDate = new Date();
}
// Ensure the lines, dislikes and tags are an empty array
if (ctx.instance.upvotes === undefined) ctx.instance.upvotes = [];
if (ctx.instance.downvotes === undefined) ctx.instance.downvotes = [];
if (ctx.instance.tags === undefined) ctx.instance.tags = [];
}
next();
});
};