-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle-controller.js
59 lines (49 loc) · 1.05 KB
/
article-controller.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
'use strict';
const AbstractController = require('./abstract-controller');
const ARTICLES_COUNT = 25;
class ArticleController extends AbstractController {
constructor({ logger, configs, database, articleModel }) {
super();
this.logger = logger;
this.configs = configs;
this.database = database;
this.articleModel_ = articleModel;
this.articleModel = database.model('Article');
}
findAll(query, page) {
page = page || 1;
return this
.articleModel
.find(query)
.skip((page - 1) * ARTICLES_COUNT)
.populate('tags')
.limit(ARTICLES_COUNT)
.exec();
}
findOne(query) {
return this
.articleModel
.findOne(query)
.exec();
}
create(entity) {
return this.articleModel.create(entity);
}
update(entity) {
if (!entity._id) {
throw new Error('_id is required');
}
return this
.articleModel
.update({ where: { _id: entity.id } }, entity);
}
delete(query) {
if (!query._id) {
throw new Error('_id is required');
}
return this
.articleModel
.remove(query);
}
}
module.exports = ArticleController;