-
Notifications
You must be signed in to change notification settings - Fork 0
/
userRoutes.js
54 lines (48 loc) · 1.25 KB
/
userRoutes.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
const express = require("express");
const router = express.Router();
const { Post, Tag } = require("./model");
const DEFAULT_QUERY_PAGE = 1;
const DEFAULT_QUERY_PERPAGE = 10;
// async function getAllTags(req, res) {
// const tags = Tag.find({}, {name: 1, _id:0});
// return res.status(200).json(tags);
// }
async function getPaginatedPosts(req, res) {
let data = {};
data.tags = req.tags;
const query = {
// "tag.name" : req.params.tag,
};
req.params.tag = req.params.tag.replace(/\+/g, " ");
let options = {
populate: {
path: "tag",
select: "name -_id",
match: {
name: req.params.tag,
},
},
select: "title desc links people tag updatedAt",
order: {
updatedAt: -1,
},
};
data.posts = await Post.find(query)
.select(options.select)
.populate(options.populate)
.sort(options.order);
data.posts = data.posts.filter((post) => {
return post.tag !== null;
});
data.postlength = data.posts.length;
return res.render("topic", data);
}
function home(req, res) {
let data = {};
data.tags = req.tags;
return res.render("index", data);
}
// router.get("/tags", getAllTags);
router.get("/posts/:tag", getPaginatedPosts);
router.get("/", home);
module.exports = router;