Skip to content

Commit 2de90c7

Browse files
authored
Merge pull request #69 from judemanutd/develop
Fetch all components API
2 parents 8b080f6 + d23c453 commit 2de90c7

File tree

9 files changed

+325
-218
lines changed

9 files changed

+325
-218
lines changed

functions/package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@
4040
"@types/nanoid": "^2.1.0",
4141
"@types/ua-parser-js": "^0.7.33",
4242
"@types/uuid": "^3.4.6",
43-
"@types/yup": "^0.26.24",
43+
"@types/yup": "^0.26.26",
4444
"husky": "^3.1.0",
45-
"lint-staged": "^9.4.3",
45+
"lint-staged": "^9.5.0",
4646
"prettier": "^1.19.1",
4747
"pretty-quick": "^2.0.1",
4848
"tslint": "^5.20.1",
4949
"tslint-config-prettier": "^1.18.0",
5050
"tslint-plugin-prettier": "^2.0.1",
51-
"typescript": "^3.7.2"
51+
"typescript": "^3.7.3"
5252
},
5353
"husky": {
5454
"hooks": {

functions/src/controllers/ComponentController.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
addGalleryImage,
1414
deleteGalleryImage,
1515
filterComponents,
16+
fetchComponentsForSelect,
1617
} from "~repository/ComponentRepo";
1718
import { IMulterFileUpload } from "~interfaces/IMulterFileUpload";
1819
import { uploadFile, deleteFile } from "~utils/fileHelper";
@@ -199,6 +200,24 @@ export default class ComponentController {
199200
}
200201
};
201202

203+
/**
204+
* ADMIN & PUBLIC
205+
*
206+
* fetch all components for display in a select, does not fetch related objects
207+
*
208+
*/
209+
public fetchComponentsForSelect = async () => {
210+
try {
211+
const components = await fetchComponentsForSelect();
212+
213+
return {
214+
results: components,
215+
};
216+
} catch (error) {
217+
throw error;
218+
}
219+
};
220+
202221
/**
203222
* ADMIN
204223
*

functions/src/repository/ComponentRepo.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,36 @@ export const fetchComponents = async (projectId: string, isPublic: boolean = fal
185185
}
186186
};
187187

188+
/**
189+
* ADMIN & PUBLIC
190+
*
191+
* fetch all components for display in a select, does not fetch related objects
192+
*
193+
*/
194+
export const fetchComponentsForSelect = async () => {
195+
try {
196+
const components = await getDb()
197+
.collection("components")
198+
.where("status", "==", STATUS_ACTIVE)
199+
.get();
200+
201+
const result = await Promise.all(
202+
components.docs.map(row => {
203+
const item = row.data();
204+
205+
return {
206+
id: item.id,
207+
text: item.name,
208+
};
209+
}),
210+
);
211+
212+
return result;
213+
} catch (error) {
214+
throw error;
215+
}
216+
};
217+
188218
/**
189219
* ADMIN
190220
*

functions/src/routes/adminRoutes/componentRoutes.ts

Lines changed: 3 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -1,205 +1,15 @@
11
import express, { Request, Response, Router, NextFunction } from "express";
2-
import multer from "multer";
3-
import path from "path";
42
import { response, successResponse } from "~utils/helpers";
5-
import { receiveFiles } from "~utils/multerHelper";
63
import ComponentController from "~controllers/ComponentController";
74

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-
335
const router: Router = express.Router();
346

357
const componentController: ComponentController = new ComponentController();
368

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) => {
17211
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();
20313

20414
return response(res, successResponse(result));
20515
} catch (error) {

0 commit comments

Comments
 (0)