-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathupdatePost.js
More file actions
62 lines (42 loc) · 1.37 KB
/
updatePost.js
File metadata and controls
62 lines (42 loc) · 1.37 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
/**
* @module Update Post Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller will allow the user to update his own post, if all parameters are correct.
*
* @todo
* Nothing for now.
*/
module.exports.updatePost = async (req, res) => {
// check if all the parameters are valid.
const { newPostTitle, newPostContent } = req.body;
if (!newPostTitle || !newPostContent) {
return res.status(400).send('post content or post title cannot be blank.');
}
if (!req.params.postId) {
return res.status(400).send('invalid post id.');
}
// find the original post.
const foundPost = await Post.findOne({
where: {
id: req.params.postId,
}
});
// check that it exists.
if (!foundPost) {
return res.status(404).send({ msg: 'The post that you are trying to update does not exist.' });
}
// check if the owner is the active user.
if (foundPost.ownerId != req.user.id) {
return res.status(401).send({ msg: 'You are not allowed to change a ressource that you do not own.' })
}
foundPost.update({
title: newPostTitle,
content: newPostContent,
})
// inform the user with the process status.
return res.status(200).send('post updated');
}