forked from tolustar/cerbos-authorization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
166 lines (137 loc) · 3.64 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const express = require("express");
const router = express.Router();
const db = require("./db");
const authorization = require("./authorization");
const checkPostExistAndGet = (id) => {
const getPost = db.posts.find((item) => item.id === Number(id));
if (!getPost) throw new Error("Post doesn't exist");
return getPost;
};
router.post("/", async (req, res, next) => {
try {
const { title, content } = req.body;
const { user_id: userId } = req.headers;
await authorization(userId, "create", req.body);
const newData = {
id: Math.floor(Math.random() * 999999 + 1),
title,
content,
userId: Number(userId),
flagged: false,
};
db.posts.push(newData);
res.status(201).json({
code: 201,
data: newData,
message: "Post created successfully",
});
} catch (error) {
next(error);
}
});
router.get("/", async (req, res, next) => {
try {
const getPosts = db.posts.filter((item) => item.flagged === false);
const { user_id: userId } = req.headers;
await authorization(userId, "view:all");
res.json({
code: 200,
data: getPosts,
message: "All posts fetched successfully",
});
} catch (error) {
next(error);
}
});
router.get("/:id", async (req, res, next) => {
try {
const getPost = db.posts.find(
(item) => item.flagged === false && item.id === Number(req.params.id)
);
const { user_id: userId } = req.headers;
await authorization(userId, "view:single");
res.json({
code: 200,
data: getPost,
message: "Post fetched successfully",
});
} catch (error) {
next(error);
}
});
router.patch("/:id", async (req, res, next) => {
try {
const { title, content } = req.body;
let updatedContent = { title, content };
const { user_id: userId } = req.headers;
const postId = req.params.id;
checkPostExistAndGet(postId);
const tempUpdatedPosts = db.posts.map((item) => {
if (item.id === Number(postId)) {
updatedContent = {
...item,
...updatedContent,
};
return updatedContent;
}
return item;
});
await authorization(userId, "update", updatedContent);
db.posts = tempUpdatedPosts;
res.json({
code: 200,
data: updatedContent,
message: "Post updated successfully",
});
} catch (error) {
next(error);
}
});
router.delete("/:id", async (req, res, next) => {
try {
const { user_id: userId } = req.headers;
const postId = req.params.id;
const post = checkPostExistAndGet(postId);
const allPosts = db.posts.filter(
(item) => item.flagged === false && item.id !== Number(postId)
);
await authorization(userId, "delete", post);
db.posts = allPosts;
res.json({
code: 200,
message: "Post deleted successfully",
});
} catch (error) {
next(error);
}
});
router.post("/flag/:id", async (req, res, next) => {
try {
let flaggedContent = {
flagged: req.body.flag,
};
const { user_id: userId } = req.headers;
const postId = req.params.id;
checkPostExistAndGet(postId);
const tempUpdatedPosts = db.posts.map((item) => {
if (item.id === Number(postId)) {
flaggedContent = {
...item,
...flaggedContent,
};
return flaggedContent;
}
return item;
});
await authorization(userId, "flag", flaggedContent);
db.posts = tempUpdatedPosts;
res.json({
code: 200,
data: flaggedContent,
message: `Post ${req.body.flag ? "flagged" : "unflagged"} successfully`,
});
} catch (error) {
next(error);
}
});
module.exports = router;