Skip to content

Commit

Permalink
Merge branch 'main' into question-spa-frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
limcaaarl committed Sep 28, 2024
2 parents ae4ba44 + aaf5883 commit 28dbdd2
Show file tree
Hide file tree
Showing 37 changed files with 4,177 additions and 33 deletions.
14 changes: 13 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# This is a sample environment configuration file.
# Copy this file to .env and replace the placeholder values with your own.

# Question Service
QUESTION_DB_CLOUD_URI=<FILL-THIS-IN>
QUESTION_DB_LOCAL_URI=mongodb://question-db:27017/question
QUESTION_DB_USERNAME=user
QUESTION_DB_PASSWORD=password

NODE_ENV=development
# User Service
USER_SERVICE_CLOUD_URI=mongodb+srv://admin:<db_password>@cluster0.uo0vu.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB

# Will use cloud MongoDB Atlas database
ENV=PROD

# Secret for creating JWT signature
JWT_SECRET=you-can-replace-this-with-your-own-secret

NODE_ENV=development
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Ignore IntelliJ IDEA project files
.idea/
.env
**/node_modules
**/.env
**/.idea
# Vim temp files
*~
*.swp
*.swo
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
- ./frontend:/app
networks:
- question-network
- user-network

question:
container_name: question
Expand All @@ -32,6 +33,7 @@ services:
networks:
- question-network
- question-db-network
- user-network

question-db:
container_name: question-db
Expand All @@ -46,11 +48,33 @@ services:
- question-db-network
restart: always

user:
container_name: user
image: user
build:
context: services/user
dockerfile: Dockerfile
ports:
- 3001
environment:
USER_SERVICE_CLOUD_URI: ${USER_SERVICE_CLOUD_URI}
USER_SERVICE_LOCAL_URI: ${USER_SERVICE_LOCAL_URI}
ENV: ${ENV}
JWT_SECRET: ${JWT_SECRET}
volumes:
- /app/node_modules
- ./services/user:/app
networks:
- user-network
restart: always

volumes:
question-db:

networks:
question-network:
driver: bridge
question-db-network:
driver: bridge
user-network:
driver: bridge
50 changes: 21 additions & 29 deletions services/question/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ uniqueness.

### Responses:

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

### Command Line Example:

Expand All @@ -318,12 +318,9 @@ curl -X POST http://localhost:8081/questions -H "Content-Type: application/json"
"id": 21,
"title": "New Question",
"description": "This is a description for a new question.",
"topics": [
"Data Structures",
"Algorithms"
],
"topics": ["Data Structures", "Algorithms"],
"difficulty": "Medium",
"_id": "66eedf739672ca081e9fd5ff"
"_id": "66f77e7bf9530832bd839239"
}
}
```
Expand All @@ -350,11 +347,12 @@ This endpoint allows updating an existing question. Only the title, description,

### Responses:

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

### Command Line Example:

Expand All @@ -370,14 +368,11 @@ curl -X PUT http://localhost:8081/questions/21 -H "Content-Type: application/jso
"status": "Success",
"message": "Question updated successfully",
"data": {
"_id": "66eedf739672ca081e9fd5ff",
"_id": "66f77e7bf9530832bd839239",
"id": 21,
"title": "Updated Title",
"description": "Updated description for the existing question.",
"topics": [
"Data Structures",
"Algorithms"
],
"title": "Updated Question Title",
"description": "This is the updated description.",
"topics": ["Updated Topic"],
"difficulty": "Hard"
}
}
Expand Down Expand Up @@ -418,14 +413,11 @@ curl -X DELETE http://localhost:8081/questions/21
"status": "Success",
"message": "Question deleted successfully",
"data": {
"_id": "66eedf739672ca081e9fd5ff",
"_id": "66f77e7bf9530832bd839239",
"id": 21,
"title": "Updated Title",
"description": "Updated description for the existing question.",
"topics": [
"Data Structures",
"Algorithms"
],
"title": "Duplicate Title",
"description": "This is the updated description.",
"topics": ["Updated Topic"],
"difficulty": "Hard"
}
}
Expand Down
1 change: 1 addition & 0 deletions services/question/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default tseslint.config({
],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
"@typescript-eslint/no-extraneous-class": "off",

// https://stackoverflow.com/questions/68816664/get-rid-of-error-delete-eslint-prettier-prettier-and-allow-use-double
'prettier/prettier': [
Expand Down
7 changes: 7 additions & 0 deletions services/question/src/controllers/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ export const updateQuestion = async (req: Request, res: Response) => {
}

try {
const existingQuestion = await Question.findOne({
$or: [{ title: updates.title }, { description: updates.description }],
}).collation({ locale: 'en', strength: 2 });
if (existingQuestion) {
return handleBadRequest(res, 'A question with the same title or description already exists.');
}

const updatedQuestion = await Question.findOneAndUpdate({ id: parseInt(id, 10) }, updates, {
new: true,
runValidators: true,
Expand Down
12 changes: 12 additions & 0 deletions services/user/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# User Service
USER_SERVICE_CLOUD_URI=<cloud_uri>
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB
PORT=3001

# Will use cloud MongoDB Atlas database
ENV=PROD

# Secret for creating JWT signature
JWT_SECRET=you-can-replace-this-with-your-own-secret

NODE_ENV=development
9 changes: 9 additions & 0 deletions services/user/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:20-alpine

WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 3001

CMD ["npm", "start"]
60 changes: 60 additions & 0 deletions services/user/MongoDBSetup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Setting up MongoDB Instance for User Service

1. Visit the MongoDB Atlas Site [https://www.mongodb.com/atlas](https://www.mongodb.com/atlas) and click on "Try Free"

2. Sign Up/Sign In with your preferred method.

3. You will be greeted with welcome screens. Feel free to skip them till you reach the Dashboard page.

4. Create a Database Deployment by clicking on the green `+ Create` Button:

![alt text](./GuideAssets/Creation.png)

5. Make selections as followings:

- Select Shared Cluster
- Select `aws` as Provider

![alt text](./GuideAssets/Selection1.png)

- Select `Singapore` for Region

![alt text](./GuideAssets/Selection2.png)

- Select `M0 Sandbox` Cluster (Free Forever - No Card Required)

> Ensure to select M0 Sandbox, else you may be prompted to enter card details and may be charged!
![alt text](./GuideAssets/Selection3.png)

- Leave `Additional Settings` as it is

- Provide a suitable name to the Cluster

![alt text](./GuideAssets/Selection4.png)

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.

![alt text](./GuideAssets/Security.png)

7. Next, click on `Add my Current IP Address`. This will whiteliste your IP address and allow you to connect to the MongoDB Database.

![alt text](./GuideAssets/Network.png)

8. Click `Finish and Close` and the MongoDB Instance should be up and running.

## Whitelisting All IP's

1. Select `Network Access` from the left side pane on Dashboard.

![alt text](./GuideAssets/SidePane.png)

2. Click on the `Add IP Address` Button

![alt text](./GuideAssets/AddIPAddress.png)

3. Select the `ALLOW ACCESS FROM ANYWHERE` Button and Click `Confirm`

![alt text](./GuideAssets/IPWhitelisting.png)

Now, any IP Address can access this Database.
Loading

0 comments on commit 28dbdd2

Please sign in to comment.