Skip to content

Commit acc31c6

Browse files
authored
Merge pull request #68 from CS3219-AY2425S1/collab-post-ms6
Add online indicator status for collab editor, and fixed various other services
2 parents f2cc844 + c4893ca commit acc31c6

File tree

187 files changed

+12127
-872
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+12127
-872
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
**/.env
1+
**/.env
2+
.idea
3+
*.log
4+
main.exe

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cd ./peerprep && npx lint-staged

.lefthook.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pre-commit:
2+
# parallel: true
3+
# commands:
4+
# golangci-lint:
5+
# glob: "*.go"
6+
# run: golangci-lint run {staged_files}
7+
# lint-staged:
8+
# root: "peerprep/"
9+
# glob: "*.(ts|tsx|css|scss|md|json)"
10+
# run: npx lint-staged

backend/common/question_struct.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// defines the JSON format of quesitons.
1+
// defines the JSON format of questions.
22
package common
33

44
type Question struct {
@@ -17,3 +17,8 @@ type FrontendQuestion struct {
1717
TopicTags []string `json:"topicTags"`
1818
Content string `json:"content"`
1919
}
20+
21+
type MatchingQuestion struct {
22+
TopicTags []string `json:"topicTags"`
23+
Difficulty string `json:"difficulty"`
24+
}

backend/database/database_interactions.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"peerprep/common"
1111

1212
"go.mongodb.org/mongo-driver/bson"
13+
"go.mongodb.org/mongo-driver/mongo"
1314
"go.mongodb.org/mongo-driver/mongo/options"
1415
)
1516

@@ -34,6 +35,36 @@ func (db *QuestionDB) GetAllQuestionsWithQuery(
3435
return questions, nil
3536
}
3637

38+
func (db *QuestionDB) GetOneQuestionWithQuery(
39+
logger *common.Logger,
40+
filter bson.D,
41+
) (*common.Question, error) {
42+
// Define the aggregation pipeline with the $match and $sample stages
43+
pipeline := mongo.Pipeline{
44+
{{Key: "$match", Value: filter}},
45+
{{Key: "$sample", Value: bson.D{{Key: "size", Value: 1}}}},
46+
}
47+
48+
// Execute the aggregation pipeline
49+
cursor, err := db.questions.Aggregate(context.Background(), pipeline)
50+
if err != nil {
51+
logger.Log.Error("Error retrieving questions: ", err.Error())
52+
return nil, err
53+
}
54+
55+
var questions []common.Question
56+
if err = cursor.All(context.Background(), &questions); err != nil {
57+
logger.Log.Error("Error decoding questions: ", err.Error())
58+
return nil, err
59+
}
60+
61+
if len(questions) == 0 {
62+
return nil, nil
63+
}
64+
65+
return &questions[0], nil
66+
}
67+
3768
func (db *QuestionDB) AddQuestion(logger *common.Logger, question *common.Question) (int, error) {
3869
if db.QuestionExists(question) {
3970
logger.Log.Warn("Cannot add question: question already exists")

backend/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// this is the main file to run the server
21
package main
32

43
import (
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package transport
2+
3+
import (
4+
"net/http"
5+
"peerprep/common"
6+
"peerprep/database"
7+
8+
"github.com/gin-gonic/gin"
9+
"go.mongodb.org/mongo-driver/bson"
10+
)
11+
12+
func GetRandomMatchingQuestion(db *database.QuestionDB, logger *common.Logger) (gin.HandlerFunc) {
13+
return func(ctx *gin.Context) {
14+
var request common.MatchingQuestion
15+
16+
err := ctx.BindJSON(&request)
17+
18+
if err != nil {
19+
ctx.JSON(http.StatusBadGateway, "error binding request from JSON")
20+
logger.Log.Error("Error converting JSON to matching request:", err.Error())
21+
return
22+
}
23+
24+
filter := bson.D{
25+
{Key: "topicTags", Value: bson.D{{Key: "$in", Value: request.TopicTags}}},
26+
{Key: "difficulty", Value: request.Difficulty},
27+
}
28+
question, err := db.GetOneQuestionWithQuery(logger, filter)
29+
30+
if err != nil {
31+
ctx.JSON(http.StatusBadGateway, "error retrieving questions from database")
32+
return
33+
}
34+
35+
if question == nil {
36+
ctx.JSON(http.StatusNotFound, "no questions found matching the request")
37+
return
38+
}
39+
40+
ctx.JSON(http.StatusOK, question)
41+
logger.Log.Info("matching-service request handled successfully")
42+
}
43+
}

backend/transport/question_http_requests.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ func SetAllEndpoints(router *gin.Engine, db *database.QuestionDB, logger *common
1919
router.DELETE("/questions/delete/:id", DeleteQuestionWithLogger(db, logger))
2020
router.PUT("/questions/replace/:id", ReplaceQuestionWithLogger(db, logger))
2121
router.GET("/health", HealthCheck(logger))
22+
router.POST("/match", GetRandomMatchingQuestion(db, logger))
2223
}
2324

2425
// enable CORS for the frontend
2526
func SetCors(router *gin.Engine, origin string) {
2627
router.Use(cors.New(cors.Config{
27-
AllowOrigins: []string{origin},
28+
AllowOrigins: []string{"http://host.docker.internal", origin},
2829
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
2930
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Authorization"},
3031
ExposeHeaders: []string{"Content-Length"},

backend/transport/replace_question.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ func ReplaceQuestionWithLogger(db *database.QuestionDB, logger *common.Logger) g
3333
return
3434
}
3535

36-
if id_param >= db.FindNextQuestionId() {
37-
logger.Log.Info(
38-
"Attempting to update a question with an ID greater than next ID, creating a new question",
39-
)
40-
status, err := db.AddQuestion(logger, &new_question)
41-
42-
if err != nil {
43-
ctx.JSON(status, err.Error())
44-
return
45-
}
46-
47-
ctx.JSON(status, "Question added successfully")
48-
logger.Log.Info("Question added successfully")
49-
return
50-
}
36+
//if id_param >= db.FindNextQuestionId() {
37+
// logger.Log.Info(
38+
// "Attempting to update a question with an ID greater than next ID, creating a new question",
39+
// )
40+
// status, err := db.AddQuestion(logger, &new_question)
41+
//
42+
// if err != nil {
43+
// ctx.JSON(status, err.Error())
44+
// return
45+
// }
46+
//
47+
// ctx.JSON(status, "Question added successfully")
48+
// logger.Log.Info("Question added successfully")
49+
// return
50+
//}
5151

5252
logger.Log.Info("Replacing question with ID: ", id_param)
5353
new_question.Id = id_param

collab/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM golang:1.20
2+
3+
WORKDIR /collab
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download
7+
8+
COPY . .
9+
10+
# Build
11+
RUN CGO_ENABLED=0 GOOS=linux go build -o /collab/app
12+
13+
EXPOSE 4000
14+
15+
# Run
16+
CMD ["/collab/app"]

0 commit comments

Comments
 (0)