1 | module.exports = ({
|
2 | User,
|
3 | router,
|
4 | authMiddleware,
|
5 | permissionMiddleware,
|
6 | asyncMiddleware,
|
7 | getConfig,
|
8 | handleResponse,
|
9 | handleError,
|
10 | }) => {
|
11 |
|
12 | router.post(
|
13 | '/user.:ext?',
|
14 | authMiddleware,
|
15 | permissionMiddleware.bind(null, 'user'),
|
16 | asyncMiddleware(async (req, res) => {
|
17 | const user = User(await getConfig(req.session.slug));
|
18 |
|
19 | try {
|
20 | handleResponse(req, res, await user.create(req.body.user));
|
21 | } catch (error) {
|
22 | handleError(req, res, error);
|
23 | }
|
24 | })
|
25 | );
|
26 |
|
27 | router.get(
|
28 | '/user.:ext?',
|
29 | authMiddleware,
|
30 | permissionMiddleware.bind(null, 'user'),
|
31 | asyncMiddleware(async (req, res) => {
|
32 | const user = User(await getConfig(req.session.slug));
|
33 |
|
34 | try {
|
35 | handleResponse(req, res, await user.read(req.query.userId));
|
36 | } catch (error) {
|
37 | handleError(req, res, error);
|
38 | }
|
39 | })
|
40 | );
|
41 |
|
42 | router.put(
|
43 | '/user.:ext?',
|
44 | authMiddleware,
|
45 | permissionMiddleware.bind(null, 'user'),
|
46 | asyncMiddleware(async (req, res) => {
|
47 | const user = User(await getConfig(req.session.slug));
|
48 |
|
49 | try {
|
50 | handleResponse(req, res, await user.update(req.body.user));
|
51 | } catch (error) {
|
52 | handleError(req, res, error);
|
53 | }
|
54 | })
|
55 | );
|
56 |
|
57 | router.delete(
|
58 | '/user.:ext?',
|
59 | authMiddleware,
|
60 | permissionMiddleware.bind(null, 'user'),
|
61 | asyncMiddleware(async (req, res) => {
|
62 | const user = User(await getConfig(req.session.slug));
|
63 |
|
64 | try {
|
65 | handleResponse(req, res, await user.delete(req.body.userId || req.body.userIds || req.query.userId || req.query.userIds));
|
66 | } catch (error) {
|
67 | handleError(req, res, error);
|
68 | }
|
69 | })
|
70 | );
|
71 |
|
72 | };
|