Skip to content

Commit 4fd4834

Browse files
authored
Merge pull request #52 from feliciagan/profilepic
2 parents cbaf0ad + 5e80ee0 commit 4fd4834

File tree

21 files changed

+1810
-86
lines changed

21 files changed

+1810
-86
lines changed

backend/question-service/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
coverage
12
node_modules
23
tests
34
.env*

backend/question-service/src/controllers/questionController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ export const createImageLink = async (
8080

8181
const uploadPromises = files.map((file) => uploadFileToFirebase(file));
8282
const imageUrls = await Promise.all(uploadPromises);
83-
res
83+
console.log(imageUrls);
84+
return res
8485
.status(200)
8586
.json({ message: "Images uploaded successfully", imageUrls });
8687
} catch (error) {
87-
res.status(500).json({ message: "Server error", error });
88+
return res.status(500).json({ message: "Server error", error });
8889
}
8990
});
9091
};

backend/question-service/swagger.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,46 @@ paths:
350350
application/json:
351351
schema:
352352
$ref: "#/definitions/ServerError"
353+
/api/questions/images:
354+
post:
355+
summary: Publish image to firebase storage
356+
tags:
357+
- questions
358+
security:
359+
- bearerAuth: []
360+
requestBody:
361+
content:
362+
multipart/form-data:
363+
schema:
364+
type: object
365+
properties:
366+
profilePic:
367+
type: string
368+
format: binary
369+
required: true
370+
responses:
371+
200:
372+
description: Successful Response
373+
content:
374+
application/json:
375+
schema:
376+
type: object
377+
properties:
378+
message:
379+
type: string
380+
description: Message
381+
imageUrl:
382+
type: string
383+
description: image url
384+
400:
385+
description: Bad Request
386+
content:
387+
application/json:
388+
schema:
389+
$ref: "#/components/schemas/ErrorResponse"
390+
500:
391+
description: Internal Server Error
392+
content:
393+
application/json:
394+
schema:
395+
$ref: "#/components/schemas/ServerErrorResponse"

backend/user-service/.env.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@ ADMIN_USERNAME=administrator
1414
ADMIN_EMAIL=[email protected]
1515
ADMIN_PASSWORD=Admin@123
1616

17+
# firebase
18+
FIREBASE_PROJECT_ID=FIREBASE_PROJECT_ID
19+
FIREBASE_PRIVATE_KEY=FIREBASE_PRIVATE_KEY
20+
FIREBASE_CLIENT_EMAIL=FIREBASE_CLIENT_EMAIL
21+
FIREBASE_STORAGE_BUCKET=FIREBASE_STORAGE_BUCKET
22+
1723
# origins for cors
1824
ORIGINS=http://localhost:5173,http://127.0.0.1:5173

backend/user-service/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# User Service Guide
22

3-
> If you have not set-up either a local or cloud MongoDB, go [here](../) first before proceding.
3+
> If you have not set-up either a local or cloud MongoDB, as well as Firebase, go [here](../) first before proceding.
44
55
## Setting-up
66

77
1. In the `user-service` directory, create a copy of the `.env.sample` file and name it `.env`.
88

9-
2. Update `MONGO_CLOUD_URI`, `MONGO_LOCAL_URI`, `JWT_SECRET`.
9+
2. Update `MONGO_CLOUD_URI`, `MONGO_LOCAL_URI`, `FIREBASE_PROJECT_ID`, `FIREBASE_PRIVATE_KEY`, `FIREBASE_CLIENT_EMAIL`, `FIREBASE_STORAGE_BUCKET`, `JWT_SECRET`.
1010

1111
3. A default admin account (`email: [email protected]` and `password: Admin@123`) wil be created. If you wish to change the default credentials, update them in `.env`. Alternatively, you can also edit your credentials and user profile after you have created the default account.
1212

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import admin from "firebase-admin";
2+
3+
admin.initializeApp({
4+
credential: admin.credential.cert({
5+
projectId: process.env.FIREBASE_PROJECT_ID,
6+
privateKey: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, "\n"),
7+
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
8+
} as admin.ServiceAccount),
9+
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
10+
});
11+
12+
const bucket = admin.storage().bucket();
13+
14+
export { bucket };
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import multer from "multer";
2+
3+
const storage = multer.memoryStorage();
4+
const upload = multer({ storage }).single("profilePic");
5+
6+
export { upload };

backend/user-service/controller/user-controller.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
validateBiography,
2121
} from "../utils/validators";
2222
import { IUser } from "../model/user-model";
23+
import { upload } from "../config/multer";
24+
import { uploadFileToFirebase } from "../utils/utils";
2325

2426
export async function createUser(
2527
req: Request,
@@ -92,6 +94,34 @@ export async function createUser(
9294
}
9395
}
9496

97+
export const createImageLink = async (
98+
req: Request,
99+
res: Response
100+
): Promise<void> => {
101+
upload(req, res, async (err) => {
102+
if (err) {
103+
return res
104+
.status(500)
105+
.json({ message: "Failed to upload image", error: err.message });
106+
}
107+
108+
if (!req.file) {
109+
return res.status(400).json({ message: "No image uploaded" });
110+
}
111+
112+
try {
113+
const file = req.file as Express.Multer.File;
114+
const imageUrl = await uploadFileToFirebase("profilePics/", file);
115+
116+
return res
117+
.status(200)
118+
.json({ message: "Image uploaded successfully", imageUrl: imageUrl });
119+
} catch (error) {
120+
return res.status(500).json({ message: "Server error", error });
121+
}
122+
});
123+
};
124+
95125
export async function getUser(req: Request, res: Response): Promise<Response> {
96126
try {
97127
const userId = req.params.id;

0 commit comments

Comments
 (0)