|
1 | 1 | import express, { Request, Response, Router, NextFunction } from "express";
|
2 |
| -import multer from "multer"; |
3 |
| -import path from "path"; |
4 | 2 | import { response, successResponse } from "~utils/helpers";
|
5 |
| -import { receiveFiles } from "~utils/multerHelper"; |
6 | 3 | import ComponentController from "~controllers/ComponentController";
|
7 | 4 |
|
8 |
| -const multerOptions: any = { |
9 |
| - storage: multer.memoryStorage(), |
10 |
| - fileFilter: (req, file, callback) => { |
11 |
| - const ext = path.extname(file.originalname); |
12 |
| - if (ext !== ".png" && ext !== ".jpg" && ext !== ".jpeg") { |
13 |
| - return callback(new Error("Only images are allowed")); |
14 |
| - } |
15 |
| - return callback(null, true); |
16 |
| - }, |
17 |
| - limits: { |
18 |
| - // 1MB |
19 |
| - fileSize: 1024 * 1024, |
20 |
| - }, |
21 |
| - // increase size limit if needed |
22 |
| - // support firebase cloud functions |
23 |
| - // the multipart form-data request object is pre-processed by the cloud functions |
24 |
| - // currently the `multer` library doesn't natively support this behaviour |
25 |
| - // as such, a custom fork is maintained to enable this by adding `startProcessing` |
26 |
| - // https://github.com/emadalam/multer |
27 |
| - startProcessing(req, busboy) { |
28 |
| - req.rawBody ? busboy.end(req.rawBody) : req.pipe(busboy); |
29 |
| - }, |
30 |
| -}; |
31 |
| -const multipartFormDataParser = multer(multerOptions).any(); |
32 |
| - |
33 | 5 | const router: Router = express.Router();
|
34 | 6 |
|
35 | 7 | const componentController: ComponentController = new ComponentController();
|
36 | 8 |
|
37 |
| -// add an image to the component gallery |
38 |
| -router.post( |
39 |
| - "/:projectId/component/:componentId/gallery", |
40 |
| - async (req: Request, res: Response, next: NextFunction) => { |
41 |
| - try { |
42 |
| - const uploads = await receiveFiles(multipartFormDataParser, req, res); |
43 |
| - |
44 |
| - const projectId = req.params.projectId; |
45 |
| - const componentId = req.params.componentId; |
46 |
| - const name = req.body.name; |
47 |
| - const description = req.body.description; |
48 |
| - |
49 |
| - const result = await componentController.addGalleryImage( |
50 |
| - componentId, |
51 |
| - projectId, |
52 |
| - uploads, |
53 |
| - name, |
54 |
| - description, |
55 |
| - ); |
56 |
| - |
57 |
| - return response(res, successResponse(result)); |
58 |
| - } catch (error) { |
59 |
| - next(error); |
60 |
| - } |
61 |
| - }, |
62 |
| -); |
63 |
| - |
64 |
| -// delete an image from the component gallery |
65 |
| -router.delete( |
66 |
| - "/:projectId/component/:componentId/gallery/:galleryImageId", |
67 |
| - async (req: Request, res: Response, next: NextFunction) => { |
68 |
| - try { |
69 |
| - const projectId = req.params.projectId; |
70 |
| - const componentId = req.params.componentId; |
71 |
| - const galleryImageId = req.params.galleryImageId; |
72 |
| - |
73 |
| - const result = await componentController.deleteGalleryImage( |
74 |
| - componentId, |
75 |
| - projectId, |
76 |
| - galleryImageId, |
77 |
| - ); |
78 |
| - |
79 |
| - return response(res, successResponse(result)); |
80 |
| - } catch (error) { |
81 |
| - next(error); |
82 |
| - } |
83 |
| - }, |
84 |
| -); |
85 |
| - |
86 |
| -// add a cover image to a component |
87 |
| -router.post( |
88 |
| - "/:projectId/component/:componentId/cover", |
89 |
| - async (req: Request, res: Response, next: NextFunction) => { |
90 |
| - try { |
91 |
| - const uploads = await receiveFiles(multipartFormDataParser, req, res); |
92 |
| - |
93 |
| - const projectId = req.params.projectId; |
94 |
| - const componentId = req.params.componentId; |
95 |
| - |
96 |
| - const result = await componentController.addCoverImage(projectId, componentId, uploads); |
97 |
| - |
98 |
| - return response(res, successResponse(result)); |
99 |
| - } catch (error) { |
100 |
| - next(error); |
101 |
| - } |
102 |
| - }, |
103 |
| -); |
104 |
| - |
105 |
| -// delete cover image for a project |
106 |
| -router.delete( |
107 |
| - "/:projectId/component/:componentId/cover", |
108 |
| - async (req: Request, res: Response, next: NextFunction) => { |
109 |
| - try { |
110 |
| - const projectId = req.params.projectId; |
111 |
| - const componentId = req.params.componentId; |
112 |
| - |
113 |
| - const result = await componentController.deleteCoverImage(projectId, componentId); |
114 |
| - |
115 |
| - return response(res, successResponse(result)); |
116 |
| - } catch (error) { |
117 |
| - next(error); |
118 |
| - } |
119 |
| - }, |
120 |
| -); |
121 |
| - |
122 |
| -// update a component for a project |
123 |
| -router.put( |
124 |
| - "/:projectId/component/:componentId", |
125 |
| - async (req: Request, res: Response, next: NextFunction) => { |
126 |
| - try { |
127 |
| - const name = req.body.name; |
128 |
| - const summary = req.body.summary; |
129 |
| - const description = req.body.description; |
130 |
| - const projectId = req.params.projectId; |
131 |
| - const componentId = req.params.componentId; |
132 |
| - const categoryId = req.body.categoryId; |
133 |
| - const technologyIds = req.body.technologyId; |
134 |
| - const links = req.body.links; |
135 |
| - |
136 |
| - const result = await componentController.updateComponent( |
137 |
| - componentId, |
138 |
| - projectId, |
139 |
| - name, |
140 |
| - categoryId, |
141 |
| - technologyIds, |
142 |
| - links, |
143 |
| - summary, |
144 |
| - description, |
145 |
| - ); |
146 |
| - |
147 |
| - return response(res, successResponse(result)); |
148 |
| - } catch (error) { |
149 |
| - next(error); |
150 |
| - } |
151 |
| - }, |
152 |
| -); |
153 |
| - |
154 |
| -// fetch a single component for a project |
155 |
| -router.get( |
156 |
| - "/:projectId/component/:componentId", |
157 |
| - async (req: Request, res: Response, next: NextFunction) => { |
158 |
| - try { |
159 |
| - const componentId = req.params.componentId; |
160 |
| - |
161 |
| - const result = await componentController.fetchComponent(componentId); |
162 |
| - |
163 |
| - return response(res, successResponse(result)); |
164 |
| - } catch (error) { |
165 |
| - next(error); |
166 |
| - } |
167 |
| - }, |
168 |
| -); |
169 |
| - |
170 |
| -// fetch all components for a project |
171 |
| -router.get("/:projectId/component", async (req: Request, res: Response, next: NextFunction) => { |
| 9 | +// fetch all components without attached data for select |
| 10 | +router.get("/select", async (req: Request, res: Response, next: NextFunction) => { |
172 | 11 | try {
|
173 |
| - const projectId = req.params.projectId; |
174 |
| - |
175 |
| - const result = await componentController.fetchComponents(projectId); |
176 |
| - |
177 |
| - return response(res, successResponse(result)); |
178 |
| - } catch (error) { |
179 |
| - next(error); |
180 |
| - } |
181 |
| -}); |
182 |
| - |
183 |
| -// add a component to a project |
184 |
| -router.post("/:projectId/component", async (req: Request, res: Response, next: NextFunction) => { |
185 |
| - try { |
186 |
| - const name = req.body.name; |
187 |
| - const summary = req.body.summary; |
188 |
| - const description = req.body.description; |
189 |
| - const projectId = req.params.projectId; |
190 |
| - const categoryId = req.body.categoryId; |
191 |
| - const technologyIds = req.body.technologyId; |
192 |
| - const links = req.body.links; |
193 |
| - |
194 |
| - const result = await componentController.addComponent( |
195 |
| - name, |
196 |
| - projectId, |
197 |
| - categoryId, |
198 |
| - technologyIds, |
199 |
| - links, |
200 |
| - summary, |
201 |
| - description, |
202 |
| - ); |
| 12 | + const result = await componentController.fetchComponentsForSelect(); |
203 | 13 |
|
204 | 14 | return response(res, successResponse(result));
|
205 | 15 | } catch (error) {
|
|
0 commit comments