-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
72 lines (62 loc) · 1.9 KB
/
routes.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
const router = require('express').Router()
const checkJwt = require('./checkJwt')
const jwtAuthz = require('express-jwt-authz')
let PostModel = require('./post.model')
// get all posts
router.route('/').get((req, res) => {
PostModel.find()
.sort({ createdAt: -1 })
.then((posts) => res.json(posts))
.catch((err) => res.status(400).json('Error: ' + err))
})
//get post by id
router.route('/:id').get((req, res) => {
PostModel.findById(req.params.id)
.then((exercise) => res.json(exercise))
.catch((err) => res.status(400).json('Error: ' + err))
})
// new post
router.route('/').post(checkJwt, jwtAuthz(['add:posts']), async (req, res) => {
const title = req.body.title
const shortContent = req.body.shortContent
const longContent = req.body.longContent
const author = req.body.author
const createdAt = Date.now()
const newPost = new PostModel({
title,
shortContent,
longContent,
author,
createdAt,
})
newPost
.save()
.then(() => res.json('Post added!'))
.catch((err) => res.status(400).json('Error: ' + err))
})
// delete post
router
.route('/:id')
.delete(checkJwt, jwtAuthz(['delete:posts']), async (req, res) => {
console.log(req.params.id)
PostModel.findByIdAndDelete(req.params.id)
.then(() => res.json('Post deleted.'))
.catch((err) => res.status(400).json('Error: ' + err))
})
// update post
router
.route('/:id')
.put(checkJwt, jwtAuthz(['edit:posts']), async (req, res) => {
PostModel.findByIdAndUpdate(req.params.id)
.then((post) => {
post.title = req.body.title
post.shortContent = req.body.shortContent
post.longContent = req.body.longContent
post
.save()
.then(() => res.json('Post updated!'))
.catch((err) => res.status(400).json('Error: ' + err))
})
.catch((err) => res.status(400).json('Error: ' + err))
})
module.exports = router