Skip to content

Commit

Permalink
Question Dup
Browse files Browse the repository at this point in the history
  • Loading branch information
AidenLYT committed Nov 13, 2024
1 parent fbaa2e0 commit 860cde4
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions peer-prep-be/src/controllers/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,35 @@ 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": bson.M{
"$regex": strings.TrimSpace(question.Question_title),
"$options": "i",
},
}).Decode(&existingQuestion)

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

err = questionCollection.FindOne(ctx, bson.M{"question_id": objId}).Decode(&existingQuestion)
if err != nil {
return c.JSON(http.StatusNotFound, responses.StatusResponse{Status: http.StatusNotFound, Message: "Question not found.", Data: &echo.Map{"data": err.Error()}})
}

if question.Question_title != "" && strings.ToLower(question.Question_title) != strings.ToLower(existingQuestion.Question_title) { var duplicateQuestion models.Question
err = questionCollection.FindOne(ctx, bson.M{
"question_title": bson.M{
"$regex": "^" + strings.TrimSpace(question.Question_title) + "$",
"$options": "i", // Case-insensitive option
},
}).Decode(&duplicateQuestion)
if err == nil {
return c.JSON(http.StatusBadRequest, responses.StatusResponse{Status: http.StatusBadRequest, Message: "Question with the same title already exists.", Data: &echo.Map{"data": "Title conflict."}})
}
}

// Only want to throw this error if there's a duplicate found, meaning FindOne has no error
if err == nil {
return c.JSON(http.StatusBadRequest, responses.StatusResponse{Status: http.StatusBadRequest, Message: errMessage, Data: &echo.Map{"data": "Question with the same title already exists."}})
}
// if err == nil {
// return c.JSON(http.StatusBadRequest, responses.StatusResponse{Status: http.StatusBadRequest, Message: errMessage, Data: &echo.Map{"data": "Question with the same title already exists."}})
// }

// Update the question in the database
update := bson.M{
Expand Down

0 comments on commit 860cde4

Please sign in to comment.