Skip to content

Commit

Permalink
Merge pull request #52 from feliciagan/profilepic
Browse files Browse the repository at this point in the history
  • Loading branch information
jolynloh authored Oct 6, 2024
2 parents cbaf0ad + 5e80ee0 commit 4fd4834
Show file tree
Hide file tree
Showing 21 changed files with 1,810 additions and 86 deletions.
1 change: 1 addition & 0 deletions backend/question-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
node_modules
tests
.env*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ export const createImageLink = async (

const uploadPromises = files.map((file) => uploadFileToFirebase(file));
const imageUrls = await Promise.all(uploadPromises);
res
console.log(imageUrls);
return res
.status(200)
.json({ message: "Images uploaded successfully", imageUrls });
} catch (error) {
res.status(500).json({ message: "Server error", error });
return res.status(500).json({ message: "Server error", error });
}
});
};
Expand Down
43 changes: 43 additions & 0 deletions backend/question-service/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,46 @@ paths:
application/json:
schema:
$ref: "#/definitions/ServerError"
/api/questions/images:
post:
summary: Publish image to firebase storage
tags:
- questions
security:
- bearerAuth: []
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
profilePic:
type: string
format: binary
required: true
responses:
200:
description: Successful Response
content:
application/json:
schema:
type: object
properties:
message:
type: string
description: Message
imageUrl:
type: string
description: image url
400:
description: Bad Request
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
500:
description: Internal Server Error
content:
application/json:
schema:
$ref: "#/components/schemas/ServerErrorResponse"
6 changes: 6 additions & 0 deletions backend/user-service/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ ADMIN_USERNAME=administrator
ADMIN_EMAIL=[email protected]
ADMIN_PASSWORD=Admin@123

# firebase
FIREBASE_PROJECT_ID=FIREBASE_PROJECT_ID
FIREBASE_PRIVATE_KEY=FIREBASE_PRIVATE_KEY
FIREBASE_CLIENT_EMAIL=FIREBASE_CLIENT_EMAIL
FIREBASE_STORAGE_BUCKET=FIREBASE_STORAGE_BUCKET

# origins for cors
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
4 changes: 2 additions & 2 deletions backend/user-service/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# User Service Guide

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

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

2. Update `MONGO_CLOUD_URI`, `MONGO_LOCAL_URI`, `JWT_SECRET`.
2. Update `MONGO_CLOUD_URI`, `MONGO_LOCAL_URI`, `FIREBASE_PROJECT_ID`, `FIREBASE_PRIVATE_KEY`, `FIREBASE_CLIENT_EMAIL`, `FIREBASE_STORAGE_BUCKET`, `JWT_SECRET`.

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.

Expand Down
14 changes: 14 additions & 0 deletions backend/user-service/config/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import admin from "firebase-admin";

admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
privateKey: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, "\n"),
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
} as admin.ServiceAccount),
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
});

const bucket = admin.storage().bucket();

export { bucket };
6 changes: 6 additions & 0 deletions backend/user-service/config/multer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import multer from "multer";

const storage = multer.memoryStorage();
const upload = multer({ storage }).single("profilePic");

export { upload };
30 changes: 30 additions & 0 deletions backend/user-service/controller/user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
validateBiography,
} from "../utils/validators";
import { IUser } from "../model/user-model";
import { upload } from "../config/multer";
import { uploadFileToFirebase } from "../utils/utils";

export async function createUser(
req: Request,
Expand Down Expand Up @@ -92,6 +94,34 @@ export async function createUser(
}
}

export const createImageLink = async (
req: Request,
res: Response
): Promise<void> => {
upload(req, res, async (err) => {
if (err) {
return res
.status(500)
.json({ message: "Failed to upload image", error: err.message });
}

if (!req.file) {
return res.status(400).json({ message: "No image uploaded" });
}

try {
const file = req.file as Express.Multer.File;
const imageUrl = await uploadFileToFirebase("profilePics/", file);

return res
.status(200)
.json({ message: "Image uploaded successfully", imageUrl: imageUrl });
} catch (error) {
return res.status(500).json({ message: "Server error", error });
}
});
};

export async function getUser(req: Request, res: Response): Promise<Response> {
try {
const userId = req.params.id;
Expand Down
Loading

0 comments on commit 4fd4834

Please sign in to comment.