Skip to content

Commit 28dbdd2

Browse files
committed
Merge branch 'main' into question-spa-frontend
2 parents ae4ba44 + aaf5883 commit 28dbdd2

37 files changed

+4177
-33
lines changed

.env.sample

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
# This is a sample environment configuration file.
22
# Copy this file to .env and replace the placeholder values with your own.
3+
4+
# Question Service
35
QUESTION_DB_CLOUD_URI=<FILL-THIS-IN>
46
QUESTION_DB_LOCAL_URI=mongodb://question-db:27017/question
57
QUESTION_DB_USERNAME=user
68
QUESTION_DB_PASSWORD=password
79

8-
NODE_ENV=development
10+
# User Service
11+
USER_SERVICE_CLOUD_URI=mongodb+srv://admin:<db_password>@cluster0.uo0vu.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
12+
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB
13+
14+
# Will use cloud MongoDB Atlas database
15+
ENV=PROD
16+
17+
# Secret for creating JWT signature
18+
JWT_SECRET=you-can-replace-this-with-your-own-secret
19+
20+
NODE_ENV=development

.gitignore

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
# Ignore IntelliJ IDEA project files
2-
.idea/
3-
.env
1+
**/node_modules
2+
**/.env
3+
**/.idea
4+
# Vim temp files
5+
*~
6+
*.swp
7+
*.swo

docker-compose.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
- ./frontend:/app
1313
networks:
1414
- question-network
15+
- user-network
1516

1617
question:
1718
container_name: question
@@ -32,6 +33,7 @@ services:
3233
networks:
3334
- question-network
3435
- question-db-network
36+
- user-network
3537

3638
question-db:
3739
container_name: question-db
@@ -46,11 +48,33 @@ services:
4648
- question-db-network
4749
restart: always
4850

51+
user:
52+
container_name: user
53+
image: user
54+
build:
55+
context: services/user
56+
dockerfile: Dockerfile
57+
ports:
58+
- 3001
59+
environment:
60+
USER_SERVICE_CLOUD_URI: ${USER_SERVICE_CLOUD_URI}
61+
USER_SERVICE_LOCAL_URI: ${USER_SERVICE_LOCAL_URI}
62+
ENV: ${ENV}
63+
JWT_SECRET: ${JWT_SECRET}
64+
volumes:
65+
- /app/node_modules
66+
- ./services/user:/app
67+
networks:
68+
- user-network
69+
restart: always
70+
4971
volumes:
5072
question-db:
5173

5274
networks:
5375
question-network:
5476
driver: bridge
5577
question-db-network:
78+
driver: bridge
79+
user-network:
5680
driver: bridge

services/question/README.md

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ uniqueness.
295295

296296
### Responses:
297297

298-
| Response Code | Explanation |
299-
|-----------------------------|--------------------------------------------------------------------|
300-
| 201 (Created) | The question is created successfully. |
301-
| 400 (Bad Request) | Required fields are missing or invalid or question already exists. |
302-
| 500 (Internal Server Error) | Unexpected error in the database or server. |
298+
| Response Code | Explanation |
299+
|-----------------------------|---------------------------------------------------------------------|
300+
| 201 (Created) | The question is created successfully. |
301+
| 400 (Bad Request) | Required fields are missing or invalid, or question already exists. |
302+
| 500 (Internal Server Error) | Unexpected error in the database or server. |
303303

304304
### Command Line Example:
305305

@@ -318,12 +318,9 @@ curl -X POST http://localhost:8081/questions -H "Content-Type: application/json"
318318
"id": 21,
319319
"title": "New Question",
320320
"description": "This is a description for a new question.",
321-
"topics": [
322-
"Data Structures",
323-
"Algorithms"
324-
],
321+
"topics": ["Data Structures", "Algorithms"],
325322
"difficulty": "Medium",
326-
"_id": "66eedf739672ca081e9fd5ff"
323+
"_id": "66f77e7bf9530832bd839239"
327324
}
328325
}
329326
```
@@ -350,11 +347,12 @@ This endpoint allows updating an existing question. Only the title, description,
350347

351348
### Responses:
352349

353-
| Response Code | Explanation |
354-
|-----------------------------|------------------------------------------------|
355-
| 200 (OK) | Success, the question is updated successfully. |
356-
| 404 (Not Found) | Question with the specified `id` not found. |
357-
| 500 (Internal Server Error) | Unexpected error in the database or server. |
350+
| Response Code | Explanation |
351+
|-----------------------------|------------------------------------------------------------------------------------|
352+
| 200 (OK) | Success, the question is updated successfully. |
353+
| 400 (Bad Request) | Invalid request body such as including `id` or duplicate `title` or `description`. |
354+
| 404 (Not Found) | Question with the specified `id` not found. |
355+
| 500 (Internal Server Error) | Unexpected error in the database or server. |
358356

359357
### Command Line Example:
360358

@@ -370,14 +368,11 @@ curl -X PUT http://localhost:8081/questions/21 -H "Content-Type: application/jso
370368
"status": "Success",
371369
"message": "Question updated successfully",
372370
"data": {
373-
"_id": "66eedf739672ca081e9fd5ff",
371+
"_id": "66f77e7bf9530832bd839239",
374372
"id": 21,
375-
"title": "Updated Title",
376-
"description": "Updated description for the existing question.",
377-
"topics": [
378-
"Data Structures",
379-
"Algorithms"
380-
],
373+
"title": "Updated Question Title",
374+
"description": "This is the updated description.",
375+
"topics": ["Updated Topic"],
381376
"difficulty": "Hard"
382377
}
383378
}
@@ -418,14 +413,11 @@ curl -X DELETE http://localhost:8081/questions/21
418413
"status": "Success",
419414
"message": "Question deleted successfully",
420415
"data": {
421-
"_id": "66eedf739672ca081e9fd5ff",
416+
"_id": "66f77e7bf9530832bd839239",
422417
"id": 21,
423-
"title": "Updated Title",
424-
"description": "Updated description for the existing question.",
425-
"topics": [
426-
"Data Structures",
427-
"Algorithms"
428-
],
418+
"title": "Duplicate Title",
419+
"description": "This is the updated description.",
420+
"topics": ["Updated Topic"],
429421
"difficulty": "Hard"
430422
}
431423
}

services/question/eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default tseslint.config({
1414
],
1515
rules: {
1616
'@typescript-eslint/no-explicit-any': 'off',
17+
"@typescript-eslint/no-extraneous-class": "off",
1718

1819
// https://stackoverflow.com/questions/68816664/get-rid-of-error-delete-eslint-prettier-prettier-and-allow-use-double
1920
'prettier/prettier': [

services/question/src/controllers/questionController.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ export const updateQuestion = async (req: Request, res: Response) => {
200200
}
201201

202202
try {
203+
const existingQuestion = await Question.findOne({
204+
$or: [{ title: updates.title }, { description: updates.description }],
205+
}).collation({ locale: 'en', strength: 2 });
206+
if (existingQuestion) {
207+
return handleBadRequest(res, 'A question with the same title or description already exists.');
208+
}
209+
203210
const updatedQuestion = await Question.findOneAndUpdate({ id: parseInt(id, 10) }, updates, {
204211
new: true,
205212
runValidators: true,

services/user/.env.sample

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# User Service
2+
USER_SERVICE_CLOUD_URI=<cloud_uri>
3+
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB
4+
PORT=3001
5+
6+
# Will use cloud MongoDB Atlas database
7+
ENV=PROD
8+
9+
# Secret for creating JWT signature
10+
JWT_SECRET=you-can-replace-this-with-your-own-secret
11+
12+
NODE_ENV=development

services/user/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /app
4+
COPY package.json package-lock.json ./
5+
RUN npm install
6+
COPY . .
7+
EXPOSE 3001
8+
9+
CMD ["npm", "start"]

services/user/MongoDBSetup.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Setting up MongoDB Instance for User Service
2+
3+
1. Visit the MongoDB Atlas Site [https://www.mongodb.com/atlas](https://www.mongodb.com/atlas) and click on "Try Free"
4+
5+
2. Sign Up/Sign In with your preferred method.
6+
7+
3. You will be greeted with welcome screens. Feel free to skip them till you reach the Dashboard page.
8+
9+
4. Create a Database Deployment by clicking on the green `+ Create` Button:
10+
11+
![alt text](./GuideAssets/Creation.png)
12+
13+
5. Make selections as followings:
14+
15+
- Select Shared Cluster
16+
- Select `aws` as Provider
17+
18+
![alt text](./GuideAssets/Selection1.png)
19+
20+
- Select `Singapore` for Region
21+
22+
![alt text](./GuideAssets/Selection2.png)
23+
24+
- Select `M0 Sandbox` Cluster (Free Forever - No Card Required)
25+
26+
> Ensure to select M0 Sandbox, else you may be prompted to enter card details and may be charged!
27+
28+
![alt text](./GuideAssets/Selection3.png)
29+
30+
- Leave `Additional Settings` as it is
31+
32+
- Provide a suitable name to the Cluster
33+
34+
![alt text](./GuideAssets/Selection4.png)
35+
36+
6. You will be prompted to set up Security for the database by providing `Username and Password`. Select that option and enter `Username` and `Password`. Please keep this safe as it will be used in User Service later on.
37+
38+
![alt text](./GuideAssets/Security.png)
39+
40+
7. Next, click on `Add my Current IP Address`. This will whiteliste your IP address and allow you to connect to the MongoDB Database.
41+
42+
![alt text](./GuideAssets/Network.png)
43+
44+
8. Click `Finish and Close` and the MongoDB Instance should be up and running.
45+
46+
## Whitelisting All IP's
47+
48+
1. Select `Network Access` from the left side pane on Dashboard.
49+
50+
![alt text](./GuideAssets/SidePane.png)
51+
52+
2. Click on the `Add IP Address` Button
53+
54+
![alt text](./GuideAssets/AddIPAddress.png)
55+
56+
3. Select the `ALLOW ACCESS FROM ANYWHERE` Button and Click `Confirm`
57+
58+
![alt text](./GuideAssets/IPWhitelisting.png)
59+
60+
Now, any IP Address can access this Database.

0 commit comments

Comments
 (0)