1 | module.exports = ({
|
2 | Taxonomy,
|
3 | router,
|
4 | authMiddleware,
|
5 | permissionMiddleware,
|
6 | cacheMiddleware,
|
7 | asyncMiddleware,
|
8 | getConfig,
|
9 | handleResponse,
|
10 | handleError,
|
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 | router.post(
|
39 | '/taxonomy.:ext?',
|
40 | authMiddleware,
|
41 | permissionMiddleware.bind(null, 'taxonomyUpdate'),
|
42 | asyncMiddleware(async (req, res) => {
|
43 | const taxonomy = Taxonomy(await getConfig(req.session.slug));
|
44 |
|
45 | try {
|
46 | handleResponse(req, res, await taxonomy.create(req.body.taxonomy));
|
47 | } catch (error) {
|
48 | handleError(req, res, error);
|
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 | router.get(
|
76 | '/taxonomy.:ext?',
|
77 | cacheMiddleware,
|
78 | asyncMiddleware(async (req, res) => {
|
79 | const taxonomy = Taxonomy(await getConfig(req.session.slug));
|
80 |
|
81 | try {
|
82 | handleResponse(req, res, await taxonomy.read(req.query.slug || req.query.taxonomySlug), true);
|
83 | } catch (error) {
|
84 | handleError(req, res, error);
|
85 | }
|
86 | })
|
87 | );
|
88 |
|
89 | router.put(
|
90 | '/taxonomy.:ext?',
|
91 | authMiddleware,
|
92 | permissionMiddleware.bind(null, 'taxonomyUpdate'),
|
93 | asyncMiddleware(async (req, res) => {
|
94 | const taxonomy = Taxonomy(await getConfig(req.session.slug));
|
95 |
|
96 | try {
|
97 | handleResponse(req, res, await taxonomy.update(req.body.taxonomy));
|
98 | } catch (error) {
|
99 | handleError(req, res, error);
|
100 | }
|
101 | })
|
102 | );
|
103 |
|
104 | router.delete(
|
105 | '/taxonomy.:ext?',
|
106 | authMiddleware,
|
107 | permissionMiddleware.bind(null, 'taxonomyUpdate'),
|
108 | asyncMiddleware(async (req, res) => {
|
109 | const taxonomy = Taxonomy(await getConfig(req.session.slug));
|
110 |
|
111 | try {
|
112 | handleResponse(req, res, await taxonomy.delete(req.body.taxonomySlug || req.body.taxonomySlugs || req.query.taxonomySlug || req.query.taxonomySlugs));
|
113 | } catch (error) {
|
114 | handleError(req, res, error);
|
115 | }
|
116 | })
|
117 | );
|
118 |
|
119 | router.post(
|
120 | '/taxonomy/term.:ext?',
|
121 | authMiddleware,
|
122 | permissionMiddleware.bind(null, 'taxonomyUpdate'),
|
123 | asyncMiddleware(async (req, res) => {
|
124 | const taxonomy = Taxonomy(await getConfig(req.session.slug));
|
125 |
|
126 | try {
|
127 | handleResponse(req, res, await taxonomy.createTerm(req.body.slug || req.body.taxonomySlug, req.body.term));
|
128 | } catch (error) {
|
129 | handleError(req, res, error);
|
130 | }
|
131 | })
|
132 | );
|
133 |
|
134 | router.put(
|
135 | '/taxonomy/term.:ext?',
|
136 | authMiddleware,
|
137 | permissionMiddleware.bind(null, 'taxonomyUpdate'),
|
138 | asyncMiddleware(async (req, res) => {
|
139 | const taxonomy = Taxonomy(await getConfig(req.session.slug));
|
140 |
|
141 | try {
|
142 | handleResponse(req, res, await taxonomy.updateTerm(req.query.term || req.body.term));
|
143 | } catch (error) {
|
144 | handleError(req, res, error);
|
145 | }
|
146 | })
|
147 | );
|
148 |
|
149 | router.delete(
|
150 | '/taxonomy/term.:ext?',
|
151 | authMiddleware,
|
152 | permissionMiddleware.bind(null, 'taxonomyUpdate'),
|
153 | asyncMiddleware(async (req, res) => {
|
154 | const taxonomy = Taxonomy(await getConfig(req.session.slug));
|
155 |
|
156 | try {
|
157 | handleResponse(req, res, await taxonomy.deleteTerm(req.query.term || req.body.term));
|
158 | } catch (error) {
|
159 | handleError(req, res, error);
|
160 | }
|
161 | })
|
162 | );
|
163 |
|
164 | };
|