-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.js
More file actions
72 lines (51 loc) · 1.6 KB
/
Copy pathRouter.js
File metadata and controls
72 lines (51 loc) · 1.6 KB
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
const logger = require('@notores/core/logger')(module);
const {stringToUrl} = require("./lib/stringExtend");
class PostRouter {
static getModel() {
return require('./model').model;
}
static getModelWrapper() {
return require('./model');
}
static async get(req, res, next) {
next('route');
}
static async getAdmin(req, res, next) {
next('route');
}
static async getById(req, res, next) {
next('route');
}
static async getByPostUrl(req, res, next) {
try {
const Wrapper = PostRouter.getModelWrapper();
const Post = Wrapper.model;
const result = await Post
.findOne({live: true, url: req.params.postUrl.toLowerCase()})
.select(Wrapper.whitelist.get)
.populate('relatedPosts')
.exec();
if (!result)
return next('route');
res.locals.page = result.page ? 'page' : 'post';
res.locals.setBody(result.page ? {page: result} : {post: result});
return next();
} catch (e) {
res.locals.setBody({post: e});
}
next('route');
}
static async post(req, res, next) {
next('router');
}
static async patch(req, res, next) {
next('router');
}
static async delete(req, res, next) {
const Post = PostRouter.getModel();
const result = await Post.deleteOne({_id: req.params.postId}).exec();
res.locals.setBody({post: result});
next('router');
}
}
module.exports = PostRouter;