Skip to content

Commit da9cee5

Browse files
committed
add testcase to question added
1 parent a2a66d9 commit da9cee5

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

cmd/portal-api/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ router.Handle("GET /api/admin/test",
7777
router.HandleFunc("GET /api/contest/{id}",contest.GetContestById(storage))
7878
router.HandleFunc("GET /api/question/{id}",question.GetQuestionById(storage))
7979
router.HandleFunc("POST /api/contest/{id}/question", contest.AddQuestionToContest(storage))
80+
router.HandleFunc("POST /api/question/{id}/testcase", question.AddTestCaseToQuestion(storage))
8081

8182
//start server
8283

pkg/http/handler/question/question.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"strings"
8+
89
"github.com/krishkumar84/bdcoe-golang-portal/pkg/storage"
910
"github.com/krishkumar84/bdcoe-golang-portal/pkg/types"
1011
"github.com/krishkumar84/bdcoe-golang-portal/pkg/utils/response"
@@ -45,4 +46,37 @@ func GetQuestionById(storage storage.Storage) http.HandlerFunc {
4546
}
4647
response.WriteJson(w, http.StatusOK, question)
4748
}
49+
}
50+
51+
func AddTestCaseToQuestion(storage storage.Storage) http.HandlerFunc {
52+
return func(w http.ResponseWriter, r *http.Request) {
53+
path := r.URL.Path
54+
parts := strings.Split(path, "/")
55+
if len(parts) < 5 {
56+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(fmt.Errorf("invalid URL format")))
57+
return
58+
}
59+
questionId := parts[3]
60+
61+
fmt.Printf("Extracted question ID: %s\n", questionId)
62+
63+
if questionId == "" {
64+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(fmt.Errorf("question id is required")))
65+
return
66+
}
67+
68+
var testCase types.TestCase
69+
if err := json.NewDecoder(r.Body).Decode(&testCase); err != nil {
70+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(err))
71+
return
72+
}
73+
74+
testCaseId, err := storage.AddTestCaseToQuestion(questionId, testCase)
75+
if err != nil {
76+
response.WriteJson(w, http.StatusInternalServerError, response.GeneralError(err))
77+
return
78+
}
79+
80+
response.WriteJson(w, http.StatusCreated, map[string]string{"test_case_id": testCaseId})
81+
}
4882
}

pkg/storage/mongodb/mongodb.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,39 @@ func (m *MongoDB) AddQuestionToContest(contestId string, question types.Question
348348
fmt.Printf("Updated contest. Modified count: %d\n", result.ModifiedCount)
349349

350350
return questionId, nil
351+
}
352+
353+
func (m *MongoDB) AddTestCaseToQuestion(questionId string, testCase types.TestCase) (string, error) {
354+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
355+
defer cancel()
356+
357+
fmt.Printf("Received question ID: %s\n", questionId)
358+
359+
// Create test case
360+
testCaseResult, err := m.db.Collection("test_cases").InsertOne(ctx, testCase)
361+
if err != nil {
362+
return "", fmt.Errorf("failed to create test case: %v", err)
363+
}
364+
testCaseId := testCaseResult.InsertedID.(primitive.ObjectID).Hex()
365+
366+
fmt.Printf("Created test case with ID: %s\n", testCaseId)
367+
368+
// Convert question ID to ObjectID
369+
questionObjID, err := primitive.ObjectIDFromHex(questionId)
370+
if err != nil {
371+
return "", fmt.Errorf("invalid question id format: %v", err)
372+
}
373+
374+
// Update question with test case ID
375+
filter := bson.M{"_id": questionObjID}
376+
update := bson.M{"$push": bson.M{"test_case_ids": testCaseId}}
377+
378+
result, err := m.db.Collection("questions").UpdateOne(ctx, filter, update)
379+
if err != nil {
380+
return "", fmt.Errorf("failed to update question: %v", err)
381+
}
382+
383+
fmt.Printf("Updated question. Modified count: %d\n", result.ModifiedCount)
384+
385+
return testCaseId, nil
351386
}

pkg/storage/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ type Storage interface {
1919
GetContestById(id string) ([]bson.M, error)
2020
GetQuestionById(id string) ([]bson.M, error)
2121
AddQuestionToContest(contestId string, question types.Question) (string, error)
22+
AddTestCaseToQuestion(questionId string, testCase types.TestCase) (string, error)
2223
}

0 commit comments

Comments
 (0)