UNPKG

4.08 kBJavaScriptView Raw
1module.exports = ({
2 Taxonomy,
3 router,
4 authMiddleware,
5 permissionMiddleware,
6 cacheMiddleware,
7 asyncMiddleware,
8 getConfig,
9 handleResponse,
10 handleError,
11}) => {
12
13 /**
14 * @swagger
15 * definitions:
16 * Taxonomy:
17 * type: object
18 * properties:
19 * title:
20 * type: string
21 * slug:
22 * type: string
23 * terms:
24 * type: array
25 * items:
26 * type: object
27 * properties:
28 * id:
29 * type: string
30 * title:
31 * type: string
32 * slug:
33 * type: string
34 * terms:
35 * type: array
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 * @swagger
55 * /taxonomy:
56 * get:
57 * tags:
58 * - taxonomy
59 * summary: Get taxonomy
60 * produces:
61 * - application/json
62 * parameters:
63 * - name: slug
64 * description: Taxonomy slug
65 * in: query
66 * required: true
67 * type: string
68 * responses:
69 * 200:
70 * description: Taxonomy
71 * schema:
72 * type: object
73 * $ref: '#/definitions/Taxonomy'
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};