Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
262 commits
Select commit Hold shift + click to select a range
60399ab
Add new logic for serializing Ids
DistractedCat Sep 21, 2024
22c3d07
Add logic for serializing Ids and other utility functions
DistractedCat Sep 21, 2024
9c07b86
Refactor code to support logging
DistractedCat Sep 21, 2024
13c59a7
Add GET functionality on a specific query
DistractedCat Sep 21, 2024
a28029c
Add ability to select a specific quesiton for solving
DistractedCat Sep 21, 2024
8f4a2e2
Add delete feature, change endpoint definition for GetMatchingQuestions
DistractedCat Sep 22, 2024
cd52221
Initialise project
alvinlim277 Sep 22, 2024
8febf76
Update file structure for components
WZWren Sep 22, 2024
8203b1e
Add basic title-bar element
WZWren Sep 23, 2024
511a5df
Modify the title-bar to not use sticky
WZWren Sep 23, 2024
8c91a42
Start adding questions page
alvinlim277 Sep 23, 2024
87911e4
Add JSON structures, API file
WZWren Sep 23, 2024
6a8ff03
Add .env file to .gitignore
WZWren Sep 23, 2024
6b33560
Add generic ErrorBlock component
WZWren Sep 23, 2024
2082911
Add rudimentary Question page
WZWren Sep 23, 2024
c0172b2
Add PUT request support
DistractedCat Sep 23, 2024
af43945
Add basic question layout to individual qn page
WZWren Sep 24, 2024
40d8650
Update Question interface
alvinlim277 Sep 24, 2024
9a66dd5
Update question structure (again)
alvinlim277 Sep 24, 2024
84a1566
Update question structure (again again)
alvinlim277 Sep 24, 2024
b16a22c
Add search bar
alvinlim277 Sep 24, 2024
9c720be
Add rudimentary page for adding questions
WZWren Sep 24, 2024
b5c0526
Add CORS support
DistractedCat Sep 24, 2024
2b5aa3c
Refactor some response messages
DistractedCat Sep 24, 2024
2c114b3
Add question categories
DistractedCat Sep 25, 2024
1f0297d
refactor GET
DistractedCat Sep 28, 2024
f7f9ed5
Refactor GET requests for specific question and ALL questions
DistractedCat Sep 28, 2024
1116178
refactor get_matching_questions
DistractedCat Sep 28, 2024
c4db155
refactor adding questions
DistractedCat Sep 28, 2024
d11c06c
refactor replace_question
DistractedCat Sep 28, 2024
9036a30
Refactor delete_question
DistractedCat Sep 28, 2024
d1a5e93
reorganize code
DistractedCat Sep 28, 2024
ecc89a5
Fixed CORS requirement for adding questions
WZWren Sep 28, 2024
c9386a8
Merge pull request #3 from CS3219-AY2425S1/api-test-yuheng
DistractedCat Sep 28, 2024
0f06c3f
Merge branch 'main' of https://github.com/CS3219-AY2425S1/cs3219-ay24…
alvinlim277 Sep 28, 2024
76bf68b
Merge pull request #2 from CS3219-AY2425S1/qn-page
alvinlim277 Sep 28, 2024
58a14c4
Merge branch 'frontend' of https://github.com/CS3219-AY2425S1/cs3219-…
alvinlim277 Sep 28, 2024
b108795
Query API in QuestionList
alvinlim277 Sep 28, 2024
e2ff470
Change routing
alvinlim277 Sep 28, 2024
5e3ac1d
Merge pull request #4 from CS3219-AY2425S1/question-page
alvinlim277 Sep 28, 2024
edb1a30
Merge pull request #5 from CS3219-AY2425S1/frontend
WZWren Sep 28, 2024
4fc34f2
Use structs.ts in QuestionCard and QuestionPage
alvinlim277 Sep 28, 2024
da1dad7
Add delete button
alvinlim277 Sep 28, 2024
fe2dfc6
Improve code quality
DistractedCat Sep 29, 2024
5303643
Merge branch 'api-test-yuheng'
DistractedCat Sep 29, 2024
fe23d88
Adjust structs to add category, handle errors
WZWren Sep 29, 2024
ff29a09
Merge pull request #11 from CS3219-AY2425S1/fix-err-status
alvinlim277 Sep 29, 2024
727c4b6
Merge branch 'frontend' of https://github.com/CS3219-AY2425S1/cs3219-…
alvinlim277 Sep 29, 2024
4575fde
Add styling and sync with new question struct
alvinlim277 Sep 29, 2024
710229b
Add delete button to QuestionCard
alvinlim277 Sep 29, 2024
7ce889b
Formatted AddQuestion page
WZWren Sep 29, 2024
4f79e77
Add non-functional delete button
WZWren Sep 29, 2024
2908bc0
Merge remote-tracking branch 'origin/qn-refactor' into enhance-indiv-qn
WZWren Sep 29, 2024
bb289e5
Merge pull request #12 from CS3219-AY2425S1/qn-refactor
WZWren Sep 29, 2024
ae166c9
Reformat files to match prettier settings
WZWren Sep 29, 2024
d31617a
Add delete functionality to button on qn page
WZWren Sep 29, 2024
d252f09
Format question.tsx
WZWren Sep 29, 2024
9a0a23f
Merge pull request #13 from CS3219-AY2425S1/enhance-indiv-qn
alvinlim277 Sep 29, 2024
4596f44
Merge pull request #15 from CS3219-AY2425S1/frontend
WZWren Sep 29, 2024
e2607d2
Merge branch 'main' of https://github.com/CS3219-AY2425S1/cs3219-ay24…
DistractedCat Oct 1, 2024
5c476f5
Add React import to suppress fragment warning
WZWren Oct 4, 2024
05f5eae
add user service
modembcc Oct 4, 2024
5bfcfd6
Add basic auth pages with minimal handling
WZWren Oct 5, 2024
a7b89d9
Add basic middleware, prepare for API refactor
WZWren Oct 5, 2024
9ff311b
Add initial nginx config file
alvinlim277 Oct 5, 2024
3771446
Add 3rd party lib for code editor
tzyian Oct 5, 2024
6e587f5
Temporary fix for question view fetch fail
alvinlim277 Oct 5, 2024
6beeb44
Refactor out routes for Client Components
WZWren Oct 5, 2024
d1782f8
Merge branch 'frontend' into login-page
WZWren Oct 5, 2024
6281ed7
Add prettier as a dev dependency
WZWren Oct 5, 2024
3681298
Fix dependency flags
WZWren Oct 5, 2024
723b321
Merge pull request #22 from CS3219-AY2425S1/login-page
tzyian Oct 5, 2024
375fa63
Merge branch 'login-page' into code-editor-styling
WZWren Oct 5, 2024
5d6cb8e
Move CollabEditor to components
tzyian Oct 5, 2024
df61dad
Merge pull request #23 from CS3219-AY2425S1/code-editor-styling
tzyian Oct 5, 2024
c87b0e1
Use HTML to display all question content
tzyian Oct 6, 2024
dbd17f8
Styling for QuestionCard.tsx
tzyian Oct 6, 2024
7947f6e
Add styling for [question] page
tzyian Oct 6, 2024
680b5a5
Minor style change
tzyian Oct 6, 2024
193aac4
Fix broken text hook
tzyian Oct 6, 2024
0f1dd99
Fix add question
tzyian Oct 6, 2024
4c0665a
Merge pull request #29 from CS3219-AY2425S1/question-card-html
tzyian Oct 6, 2024
dbdd7a6
Docker WIP (not working)
tzyian Oct 6, 2024
d3c0cd6
Use volume binding to completely bind user service
WZWren Oct 6, 2024
93659bb
Change Dockerfile of user-service to drop binding
WZWren Oct 6, 2024
f2cc844
Merge pull request #31 from CS3219-AY2425S1/frontend-docker-vol-binding
WZWren Oct 6, 2024
ae0183e
Merge branch 'main' of https://github.com/CS3219-AY2425S1/cs3219-ay24…
DistractedCat Oct 8, 2024
253a8e4
ignore .idea
DistractedCat Oct 8, 2024
9696b11
Create rabbitMQ
DistractedCat Oct 8, 2024
128c99a
Create mappings storage
DistractedCat Oct 8, 2024
6530e1e
Add logic to start handling requests
DistractedCat Oct 8, 2024
8938cb4
Add rooms
DistractedCat Oct 9, 2024
5a77ff9
Implement new architecture
DistractedCat Oct 12, 2024
002c739
add logic for publishing messages
DistractedCat Oct 12, 2024
b3ea591
bug fixes for message queue
DistractedCat Oct 13, 2024
fc92c7c
Add ability to store in redis
DistractedCat Oct 15, 2024
376783d
Add logic to send rooms to storage blob
DistractedCat Oct 15, 2024
4dc02f5
Finish up v1 of user matching
DistractedCat Oct 16, 2024
aa6c5ba
Example .env files
alvinlim277 Oct 16, 2024
cc96d18
Add filter context
alvinlim277 Oct 16, 2024
b23c658
Add stub functions to periodically query resource blob
alvinlim277 Oct 17, 2024
1675d46
Add stub for user context and request time
alvinlim277 Oct 17, 2024
d8c53ea
Set up internal API, verify local testing and fix question filter bug
alvinlim277 Oct 17, 2024
994ba1a
Fix expiry time bug, add .gitignore files
DistractedCat Oct 17, 2024
61ac5cc
Merge remote-tracking branch 'origin/user-matching' into match-button
alvinlim277 Oct 17, 2024
6309549
Initial setup of dockerfiles and compose.yaml
alvinlim277 Oct 18, 2024
a55ca7d
Attempt to fix some issues
alvinlim277 Oct 18, 2024
38f8732
Forgot these
alvinlim277 Oct 18, 2024
eebbfbe
Change .env.example templates
alvinlim277 Oct 18, 2024
90717b7
Rectify storage blob Dockerfile
alvinlim277 Oct 18, 2024
bb68806
Fix matching service and storage blob connection issues + compose.yaml
alvinlim277 Oct 19, 2024
a7f0a6c
Add functionality to match button
WZWren Oct 20, 2024
8454cf8
Fix rabbitmq depend_on
tzyian Oct 20, 2024
422e509
Add stopwatch for matchmaking and default difficulty to easy
alvinlim277 Oct 20, 2024
258db87
Merge remote-tracking branch 'origin/dockerise-match-service' into ad…
alvinlim277 Oct 20, 2024
6770d98
Revert filter hooks, add matching button dropdown
WZWren Oct 20, 2024
d8f75ac
Merge pull request #34 from CS3219-AY2425S1/add-match-timer
WZWren Oct 20, 2024
924c484
Minor css changes to login page
tzyian Oct 20, 2024
d0cdf95
Add MS4 requirements
WZWren Oct 20, 2024
83f4844
Merge pull request #35 from CS3219-AY2425S1/minor-css-changes-to-login
WZWren Oct 20, 2024
6a291d0
Add websocket for collab
modembcc Oct 23, 2024
84830a8
Redirects to question on match found and connect to websocket
modembcc Oct 25, 2024
416e7c9
Add verify user on connection
modembcc Oct 27, 2024
371f27f
Fix questionID invalid
modembcc Oct 27, 2024
9937f89
Prettify home page, add prettier pre-commit
tzyian Oct 27, 2024
c0c6882
Fix pre-commit hook
tzyian Oct 27, 2024
3a64cfc
Add show/hide password
tzyian Oct 27, 2024
db8d994
Add lefthook
tzyian Oct 27, 2024
eda104f
UI icons and navbar dropdown
tzyian Oct 27, 2024
bcd5b64
Update lefthook
tzyian Oct 27, 2024
386d863
Fix broken addquestion difficulty
tzyian Oct 28, 2024
81b8d61
add close button
modembcc Oct 28, 2024
a09ab51
dockerize
modembcc Oct 28, 2024
d59dd85
Basic addquestion page
tzyian Oct 28, 2024
c2f0dbb
Add room verification
modembcc Oct 28, 2024
2733b84
Update Match found alert and collab server down feedback
modembcc Oct 31, 2024
a3eae90
Merge remote-tracking branch 'origin/nginx-initial-setup' into n9-api…
alvinlim277 Oct 31, 2024
e986eaf
Rudimentary Dockerfile and compose
alvinlim277 Oct 31, 2024
8d5b39e
Fix close room button showing up
modembcc Oct 31, 2024
590b0b4
Dockerise nginx and add basic nginx.conf
alvinlim277 Oct 31, 2024
79276ed
Fix compiler error related to DOMPurify
WZWren Nov 1, 2024
9a80388
Fix add question page except for css
tzyian Nov 1, 2024
5dad4c4
Add logout under profile dropdown
tzyian Nov 1, 2024
1b06717
Fix login bug
alvinlim277 Nov 1, 2024
a4d07a6
Change blob to be queried by a random hash instead
WZWren Nov 1, 2024
1b02dd9
Hook up new logic for matching to frontend
WZWren Nov 1, 2024
59785b1
Add manifests
DistractedCat Nov 1, 2024
72ed9ec
Merge remote-tracking branch 'origin/collab-gorilla' into collab-gorilla
DistractedCat Nov 1, 2024
704fdbc
Merge branch 'n9-api-gateway' of https://github.com/CS3219-AY2425S1/c…
DistractedCat Nov 2, 2024
8088237
Add edit question
tzyian Nov 2, 2024
b13dd9a
Remove lefthook
tzyian Nov 2, 2024
cf26dd2
fix issues for kubernetes
DistractedCat Nov 2, 2024
003d526
Add persistent store of rooms
WZWren Nov 2, 2024
5b990dc
Update nginx conf for watch (i think)
alvinlim277 Nov 2, 2024
61a7f67
Add collab fix to next config
WZWren Nov 3, 2024
f83113e
temp fix for collab
DistractedCat Nov 3, 2024
a7013c5
Add collab fix to next config
WZWren Nov 3, 2024
3ae3404
Add collab fix to next config
WZWren Nov 3, 2024
ce53139
Attempt to fix collab ws issues
alvinlim277 Nov 3, 2024
5ac751a
Fix collab bug
DistractedCat Nov 3, 2024
26eaa53
update manifests
DistractedCat Nov 3, 2024
0c90257
Add comms component, without dockerization
WZWren Nov 3, 2024
22a497a
Add UI for Comms, switched off StrictMode
WZWren Nov 3, 2024
95261be
Use dmp for hopefully faster changes
tzyian Nov 4, 2024
6397c42
Add formatter url
tzyian Nov 4, 2024
ba9de29
Merge branch 'collab-gorilla' into persist-matches
WZWren Nov 4, 2024
0ffb306
Better error response in console when formatters fail
tzyian Nov 4, 2024
dc831be
Change question seed to be syntactically valid
tzyian Nov 4, 2024
41bf8b8
Remove duplicated code
tzyian Nov 4, 2024
026e2c7
Add logs and deploy script
modembcc Nov 4, 2024
b54506a
Fix formatting
tzyian Nov 4, 2024
f7f712e
Merge remote-tracking branch 'origin/formatter' into formatting
DistractedCat Nov 4, 2024
b87bac3
Merge branch 'persist-matches' into n1-comms
WZWren Nov 4, 2024
d4ce18d
Show better error message on syntax errors
tzyian Nov 4, 2024
6fcd886
Merge branch 'collab-gorilla' into formatter
tzyian Nov 4, 2024
ee7c8eb
test npm install
DistractedCat Nov 4, 2024
75dcc50
test formatter
DistractedCat Nov 4, 2024
2838af0
Add Dockerfile to Comms module
WZWren Nov 4, 2024
367f010
Fix cookie issue
DistractedCat Nov 4, 2024
9cb7ff4
Merge branch 'n1-comms' into formatting
tzyian Nov 4, 2024
20b6ce0
Fix merge conflicts from branch: n1-comms
tzyian Nov 4, 2024
921673d
add comms
DistractedCat Nov 4, 2024
dada4cf
Fix questions page not refreshing when deleted
tzyian Nov 4, 2024
952fcdb
Merge branch 'fix-refresh-on-delete-question' into post-ms6
tzyian Nov 5, 2024
7f3a449
Merge branch 'formatter' into post-ms6
tzyian Nov 5, 2024
833dd56
Merge branch 'home-page-editor-page' into post-ms6
tzyian Nov 5, 2024
88d7556
Merged fix-add-question and home-page
tzyian Nov 5, 2024
743a3c4
Fix unterminated string literals
tzyian Nov 6, 2024
bb0b52c
Fix /questions page refreshing on add/edit
tzyian Nov 6, 2024
81ec1b0
Fix unterminated string literal
tzyian Nov 6, 2024
4dc737d
Add second gateway to connect socket.io behind
WZWren Nov 6, 2024
d9fd8fd
Swapped gateway ports 70 and 80 to prep for SSL
WZWren Nov 7, 2024
9eb79db
Set edit/add question to admin only actions
tzyian Nov 7, 2024
6f5e27b
Fix editQuestion form style and error handling
tzyian Nov 7, 2024
3c3c46c
CSS for questions list, new, edit
tzyian Nov 7, 2024
8db03d9
Fix carousel
tzyian Nov 7, 2024
44f69d8
Add alert on fail to match
tzyian Nov 7, 2024
2eb49fe
Make question filter sticky
tzyian Nov 7, 2024
fe287dc
Show error on duplicate user registration
tzyian Nov 8, 2024
9c75fcd
Make textbox visible in login/register
tzyian Nov 8, 2024
114b7f3
Merge branch 'post-ms6' into second-nginx
WZWren Nov 8, 2024
0eabf1a
Adjust logout handling to be done in middleware
WZWren Nov 8, 2024
587ebe4
Add eslint disable to let npm build go through
WZWren Nov 8, 2024
fde213a
Merge pull request #64 from CS3219-AY2425S1/second-nginx
tzyian Nov 8, 2024
bf1bbd3
prevent users from matching with themselves
DistractedCat Nov 9, 2024
46978ee
Merge pull request #65 from CS3219-AY2425S1/collab-post-ms6
tzyian Nov 9, 2024
03a0b02
Add deployment
modembcc Nov 9, 2024
327b667
Increase timeoutSec
modembcc Nov 9, 2024
f162037
Add pinging to other user during collab
DistractedCat Nov 9, 2024
3946273
Add hook to prevent webcall until authorized
WZWren Nov 9, 2024
58a4522
Properly hook authentication up to message
WZWren Nov 9, 2024
c1f60db
Add room deletion on room closure
WZWren Nov 9, 2024
c4a2387
Remove unnecessary fields in persistentRoom store
WZWren Nov 9, 2024
95c92f9
Store text-editor content within server
tzyian Nov 9, 2024
65fd9c3
Merge remote-tracking branch 'origin/post-ms6' into post-ms6-deployment
modembcc Nov 10, 2024
ed2e83a
merge post-ms6
modembcc Nov 10, 2024
febb737
Start adding online indicator for collab editor
alvinlim277 Nov 10, 2024
e41e20f
Temp fix for heartbeat
alvinlim277 Nov 10, 2024
73a9287
Merge remote-tracking branch 'origin/post-ms6' into collab-post-ms6
alvinlim277 Nov 10, 2024
01666e6
Merge remote-tracking branch 'origin/save-history-on-server' into col…
alvinlim277 Nov 10, 2024
f039836
Fix types
alvinlim277 Nov 10, 2024
092d26a
Fix after merge
alvinlim277 Nov 10, 2024
7942b6b
Update deployment and stress test with locust
modembcc Nov 10, 2024
37e1bcb
add risky room expiry
DistractedCat Nov 11, 2024
b13d1b2
Add refresh of simple-peer objects when DC
WZWren Nov 11, 2024
f2fb11f
Merge remote-tracking branch 'origin/collab-post-ms6-risky' into depl…
modembcc Nov 11, 2024
78011ce
Adjusted comms fix to be independent of latency
WZWren Nov 12, 2024
9572fb7
Add a simple status message indicating no call
WZWren Nov 12, 2024
17f60e5
Merge branch 'collab-post-ms6-risky' into deploy-risky
modembcc Nov 12, 2024
185dc0f
Merge pull request #70 from CS3219-AY2425S1/minor-add-status-msg
WZWren Nov 12, 2024
a3b58da
Add fix for close room
modembcc Nov 12, 2024
e0bf1a3
Fix the server content being wiped
tzyian Nov 12, 2024
cf3cea7
Change ping interval for failure to 5
tzyian Nov 12, 2024
fd174ab
Fixed alert not showing up on format error
tzyian Nov 12, 2024
e2befa9
Set ping stop to 3x failed pings
tzyian Nov 12, 2024
adbd48f
Adjust collab delete algorithm
WZWren Nov 13, 2024
71e5c8a
Merge branch 'collab-post-ms6-risky' into deploy-risky
modembcc Nov 13, 2024
b0c34c0
Fix navbar showing logged in when not
tzyian Nov 13, 2024
5e1ee7c
Fix middleware not doing anything
tzyian Nov 13, 2024
556c718
Deploy more risky
modembcc Nov 13, 2024
283e2c6
Fix REGEX not matching all routes
WZWren Nov 13, 2024
81a1e21
Add missing message type for close-room
WZWren Nov 13, 2024
52b21d6
Merge pull request #72 from CS3219-AY2425S1/fix-navbar
WZWren Nov 13, 2024
c4893ca
Merge pull request #69 from CS3219-AY2425S1/collab-post-ms6-risky
WZWren Nov 13, 2024
acc31c6
Merge pull request #68 from CS3219-AY2425S1/collab-post-ms6
WZWren Nov 13, 2024
cc81302
Merge branch 'main' into deploy-risky
modembcc Nov 13, 2024
5140ef3
fix collab
modembcc Nov 13, 2024
2d95ebc
add kubernetes to gitignore
modembcc Nov 13, 2024
f4ba1fc
Merge pull request #73 from CS3219-AY2425S1/deploy-risky
modembcc Nov 13, 2024
286bfd8
Add link to readme
tzyian Mar 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/.env
.idea
*.log
main.exe
kubernetes
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cd ./peerprep && npx lint-staged
10 changes: 10 additions & 0 deletions .lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pre-commit:
# parallel: true
# commands:
# golangci-lint:
# glob: "*.go"
# run: golangci-lint run {staged_files}
# lint-staged:
# root: "peerprep/"
# glob: "*.(ts|tsx|css|scss|md|json)"
# run: npx lint-staged
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/bzPrOe11)
# CS3219 Project (PeerPrep) - AY2425S1
## Group: Gxx
## Group: G14

### Note:
- You can choose to develop individual microservices within separate folders within this repository **OR** use individual repositories (all public) for each microservice.
- In the latter scenario, you should enable sub-modules on this GitHub classroom repository to manage the development/deployment **AND** add your mentor to the individual repositories as a collaborator.
- The teaching team should be given access to the repositories as we may require viewing the history of the repository in case of any disputes or disagreements.
View README [here](https://docs.google.com/document/d/1yadBdlLO0Mf9iY_GwbeY8kvQEzlA9_R3mVLtTnpQkVk/edit?tab=t.0)
3 changes: 3 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGODB_URI=mongodb+srv://<user>:<password>@<collection>.<name>.mongodb.net/?retryWrites=true&w=majority&appName=<appname>
PORT=:9090
CORS_ORIGIN=http://localhost:3000
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
log
29 changes: 29 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# syntax=docker/dockerfile:1

FROM golang:1.23

# Set destination for COPY
WORKDIR /backend

# Download Go modules
# TODO: don't include the .env file in the COPY
# TODO: multistage build
COPY go.mod go.sum ./
RUN go mod download

# Copy the source code. Note the slash at the end, as explained in
# https://docs.docker.com/reference/dockerfile/#copy
COPY . .

# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /backend/app

# Optional:
# To bind to a TCP port, runtime parameters must be supplied to the docker command.
# But we can document in the Dockerfile what ports
# the application is going to listen on by default.
# https://docs.docker.com/reference/dockerfile/#expose
EXPOSE 9090

# Run
CMD ["/backend/app"]
14 changes: 14 additions & 0 deletions backend/common/logger_struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common

import (
"github.com/sirupsen/logrus"
)

// contains the logger
type Logger struct {
Log *logrus.Logger
}

func NewLogger(logger *logrus.Logger) *Logger {
return &Logger{Log: logger}
}
24 changes: 24 additions & 0 deletions backend/common/question_struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// defines the JSON format of questions.
package common

type Question struct {
Title string `json:"title"`
TitleSlug string `json:"titleSlug"`
Difficulty string `json:"difficulty"`
TopicTags []string `json:"topicTags"`
Content string `json:"content"`
Schemas []string `json:"schemas"`
Id int `json:"id"`
}

type FrontendQuestion struct {
Title string `json:"title"`
Difficulty string `json:"difficulty"`
TopicTags []string `json:"topicTags"`
Content string `json:"content"`
}

type MatchingQuestion struct {
TopicTags []string `json:"topicTags"`
Difficulty string `json:"difficulty"`
}
117 changes: 117 additions & 0 deletions backend/database/database_interactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// contains the database-related functions for the questions API.
package database

import (
"context"
"errors"
"fmt"
"net/http"

"peerprep/common"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func (db *QuestionDB) GetAllQuestionsWithQuery(
logger *common.Logger,
filter bson.D,
) ([]common.Question, error) {
questionCursor, err := db.questions.Find(context.Background(), filter)

if err != nil {
logger.Log.Error("Error retrieving questions: ", err.Error())
return nil, err
}

var questions []common.Question

if err = questionCursor.All(context.Background(), &questions); err != nil {
logger.Log.Error("Error decoding questions: ", err.Error())
return nil, err
}

return questions, nil
}

func (db *QuestionDB) GetOneQuestionWithQuery(
logger *common.Logger,
filter bson.D,
) (*common.Question, error) {
// Define the aggregation pipeline with the $match and $sample stages
pipeline := mongo.Pipeline{
{{Key: "$match", Value: filter}},
{{Key: "$sample", Value: bson.D{{Key: "size", Value: 1}}}},
}

// Execute the aggregation pipeline
cursor, err := db.questions.Aggregate(context.Background(), pipeline)
if err != nil {
logger.Log.Error("Error retrieving questions: ", err.Error())
return nil, err
}

var questions []common.Question
if err = cursor.All(context.Background(), &questions); err != nil {
logger.Log.Error("Error decoding questions: ", err.Error())
return nil, err
}

if len(questions) == 0 {
return nil, nil
}

return &questions[0], nil
}

func (db *QuestionDB) AddQuestion(logger *common.Logger, question *common.Question) (int, error) {
if db.QuestionExists(question) {
logger.Log.Warn("Cannot add question: question already exists")
return http.StatusConflict, errors.New("question already exists")
}

if _, err := db.questions.InsertOne(context.Background(), question); err != nil {
logger.Log.Error("Error adding question", err.Error())
return http.StatusBadGateway, err
}

return http.StatusOK, nil
}

func (db *QuestionDB) UpsertQuestion(
logger *common.Logger,
question *common.Question,
) (int, error) {

filter := bson.D{bson.E{Key: "id", Value: question.Id}}
setter := bson.M{"$set": question}
upsert := options.Update().SetUpsert(true)

_, err := db.questions.UpdateOne(context.Background(), filter, setter, upsert)

if err != nil {
logger.Log.Error("Error while upserting question", err.Error())
return http.StatusBadGateway, err
}

return http.StatusOK, nil
}

func (db *QuestionDB) DeleteQuestion(logger *common.Logger, id int) (int, error) {
deleteStatus, err := db.questions.DeleteOne(
context.Background(),
bson.D{bson.E{Key: "id", Value: id}},
)

if err != nil {
logger.Log.Error("Error deleting question", err.Error())
return http.StatusBadGateway, err
} else if deleteStatus.DeletedCount == 0 {
msg := fmt.Sprintf("Question with ID %d not found when deleting question", id)
logger.Log.Warn(msg)
return http.StatusNotFound, errors.New(msg)
}

return http.StatusNoContent, nil
}
45 changes: 45 additions & 0 deletions backend/database/database_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// utility functions for questions api
package database

import (
"context"
"peerprep/common"

"go.mongodb.org/mongo-driver/bson"
)

// used to check if a question already exists in the database.
func (db *QuestionDB) QuestionExists(question *common.Question) bool {
filter := bson.D{bson.E{Key: "title", Value: question.Title}}
err := db.questions.FindOne(context.Background(), filter).
Decode(&common.Question{})
//FindOne() returns error if no document is found
return err == nil
}

// used to find the next question ID to be used. Similar to SERIAL in psql.
func (db *QuestionDB) FindNextQuestionId() int {
id := struct {
Next int `json:"next"`
}{}
filter := bson.D{}

if err := db.nextId.FindOne(context.Background(), filter).Decode(&id); err != nil {
return -1
}

return id.Next
}

// used to check if a question being replaced will cause duplicates in the database

func (db *QuestionDB) QuestionExistsExceptId(question *common.Question) bool {
filter := bson.D{
bson.E{Key: "title", Value: question.Title},
bson.E{Key: "id", Value: bson.D{bson.E{Key: "$ne", Value: question.Id}}},
}
err := db.questions.FindOne(context.Background(), filter).
Decode(&common.Question{})
//FindOne() returns error if no document is found
return err == nil
}
45 changes: 45 additions & 0 deletions backend/database/initialise_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// this is used to initialise the database connection
package database

import (
"context"
"log"
"os"

"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func InitialiseDB() (*mongo.Client, error) {
// Load environment variables
err := godotenv.Load(".env")

if err != nil {
log.Fatal("Error loading environment variables: " + err.Error())
}

mongoURI := os.Getenv("MONGODB_URI")

if mongoURI == "" {
log.Fatal("MONGODB_URI not set in environment variables")
}

clientOptions := options.Client().ApplyURI(mongoURI)

// Connect to MongoDB
server, err := mongo.Connect(context.Background(), clientOptions)

if err != nil {
log.Fatal("Error connecting to MongoDB" + err.Error())
}

// Check the connection
err = server.Ping(context.Background(), nil)

if err != nil {
return nil, err
}

return server, nil
}
20 changes: 20 additions & 0 deletions backend/database/questionDB_struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package database

import (
"go.mongodb.org/mongo-driver/mongo"
)

// QuestionDB is a struct that contains a pointer to a mongo client.
// questions is the collection with all the questions, nextId is a single-entry collection that stores the next ID to be used.
type QuestionDB struct {
questions *mongo.Collection
nextId *mongo.Collection
}

// returns a pointer to an instance of the Question collection
func NewQuestionDB(client *mongo.Client) *QuestionDB {

questionCollection := client.Database("questions").Collection("questions")
idCollection := client.Database("questions").Collection("counter")
return &QuestionDB{questions: questionCollection, nextId: idCollection}
}
Loading