Skip to content

Commit

Permalink
Merge pull request #108 from CS3219-AY2425S1/kervyn/question-duplicate
Browse files Browse the repository at this point in the history
Updated duplication validation in Create and Update question
  • Loading branch information
smolegz authored Nov 13, 2024
2 parents 35ed6c1 + c795a9d commit 4922752
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions peer-prep-be/src/controllers/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ func CreateQuestion(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.StatusResponse{Status: http.StatusBadRequest, Message: errMessage, Data: &echo.Map{"data": validationErr.Error()}})
}

err := questionCollection.FindOne(ctx, bson.M{"question_title": question.Question_title}).Decode(&existingQuestion)
err := questionCollection.FindOne(ctx, bson.M{
"question_title": bson.M{
"$regex": strings.TrimSpace(question.Question_title),
"$options": "i",
},
}).Decode(&existingQuestion)

// Only want to throw this error if there's a duplicate found, meaning FindOne has no error
if err == nil {
Expand Down Expand Up @@ -197,7 +202,12 @@ func UpdateQuestion(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.StatusResponse{Status: http.StatusBadRequest, Message: errMessage, Data: &echo.Map{"data": validationErr.Error()}})
}

err = questionCollection.FindOne(ctx, bson.M{"question_title": question.Question_title}).Decode(&existingQuestion)
err = questionCollection.FindOne(ctx, bson.M{
"question_title": bson.M{
"$regex": strings.TrimSpace(question.Question_title),
"$options": "i",
},
}).Decode(&existingQuestion)

// Only want to throw this error if there's a duplicate found, meaning FindOne has no error
if err == nil {
Expand Down

0 comments on commit 4922752

Please sign in to comment.