-
Notifications
You must be signed in to change notification settings - Fork 33
/
article.js
192 lines (130 loc) · 4.86 KB
/
article.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
const Bounce = require('@hapi/bounce');
const Schmervice = require('@hapipal/schmervice');
const { UniqueViolationError } = require('objection');
module.exports = class ArticleService extends Schmervice.Service {
async findById(id, txn) {
const { Article } = this.server.models();
return await Article.query(txn).throwIfNotFound().findById(id);
}
async findBySlug(slug, txn) {
const { Article } = this.server.models();
return await Article.query(txn).throwIfNotFound().first().where({ slug });
}
async find({ tag, author, favorited, limit, offset }, txn) {
const { Article } = this.server.models();
const query = Article.query(txn);
// Note that these joins don't ruin the result count
// only because each match must be unique or nil.
if (tag) {
query.innerJoinRelated('tags').where('tags.name', tag);
}
if (author) {
query.innerJoinRelated('author').where('author.username', author);
}
if (favorited) {
query.innerJoinRelated('favoritedBy').where('favoritedBy.username', favorited);
}
const [articles, total] = await Promise.all([
query.limit(limit).offset(offset).orderBy('createdAt', 'desc'),
query.resultSize()
]);
return { articles, total };
}
async feed(currentUserId, { limit, offset }, txn) {
const { Article } = this.server.models();
const query = Article.query(txn)
.joinRelated('author.followedBy')
.where('author:followedBy.id', currentUserId);
const [articles, total] = await Promise.all([
query.limit(limit).offset(offset).orderBy('createdAt', 'desc'),
query.resultSize()
]);
return { articles, total };
}
async create(currentUserId, { tagList, ...articleInfo }, txn) {
const { Article, Tag } = this.server.models();
if (tagList) {
const ensureTag = async (name) => await this._ensureTag(name, txn);
await Promise.all(tagList.map(ensureTag));
}
const tags = tagList && await Tag.query(txn).select('id').whereIn('name', tagList);
const { id } = await Article.query(txn).insertGraph({
...articleInfo,
authorId: currentUserId,
tags
}, {
relate: true
});
return id;
}
async update(id, { tagList, ...articleInfo }, txn) {
const { Article, Tag } = this.server.models();
if (tagList) {
const ensureTag = async (name) => await this._ensureTag(name, txn);
await Promise.all(tagList.map(ensureTag));
}
const tags = tagList && await Tag.query(txn).select('id').whereIn('name', tagList);
await Article.query(txn).upsertGraph({
...articleInfo,
id,
tags
}, {
relate: true,
unrelate: true
});
return id;
}
async _ensureTag(name, txn) {
const { Tag } = this.server.models();
try {
await Tag.query(txn).insert({ name });
}
catch (err) {
Bounce.ignore(err, UniqueViolationError);
}
}
async delete(id, txn) {
const { Article } = this.server.models();
await Article.query(txn).throwIfNotFound().delete().where({ id });
}
async favorite(currentUserId, id, txn) {
const { Article } = this.server.models();
try {
await Article.relatedQuery('favoritedBy', txn).for(id).relate(currentUserId);
}
catch (err) {
Bounce.ignore(err, UniqueViolationError);
}
}
async unfavorite(currentUserId, id, txn) {
const { Article } = this.server.models();
await Article.relatedQuery('favoritedBy', txn).for(id)
.unrelate().where({ id: currentUserId });
}
async tags(txn) {
const { Tag } = this.server.models();
return await Tag.query(txn);
}
async findCommentById(commentId, txn) {
const { Comment } = this.server.models();
return await Comment.query(txn).throwIfNotFound().findById(commentId);
}
async findCommentsByArticle(articleId, txn) {
const { Comment } = this.server.models();
return await Comment.query(txn).where({ articleId }).orderBy('createdAt', 'desc');
}
async addComment(currentUserId, articleId, { body }, txn) {
const { Comment } = this.server.models();
const { id } = await Comment.query(txn).insert({
authorId: currentUserId,
articleId,
body
});
return id;
}
async removeComment(commentId, txn) {
const { Comment } = this.server.models();
return await Comment.query(txn).delete().where({ id: commentId });
}
};