Skip to content

Commit 5cc6da2

Browse files
committed
DeleteTestCaseFromQuestionById completed
1 parent 14dae2c commit 5cc6da2

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

cmd/portal-api/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ router.Handle("GET /api/admin/test",
8080
router.HandleFunc("POST /api/contest/{id}/question", contest.AddQuestionToContest(storage))
8181
router.HandleFunc("DELETE /api/contest/{contestId}/question/{questionId}", contest.DeleteQuestionFromContestById(storage))
8282
router.HandleFunc("POST /api/question/{id}/testcase", question.AddTestCaseToQuestion(storage))
83+
router.HandleFunc("DELETE /api/question/{questionId}/testcase/{testCaseId}", question.DeleteTestCaseFromQuestionById(storage))
8384

8485
//start server
8586

pkg/http/handler/question/question.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,33 @@ func AddTestCaseToQuestion(storage storage.Storage) http.HandlerFunc {
7979

8080
response.WriteJson(w, http.StatusCreated, map[string]string{"test_case_id": testCaseId})
8181
}
82+
}
83+
84+
func DeleteTestCaseFromQuestionById(storage storage.Storage) http.HandlerFunc {
85+
return func(w http.ResponseWriter, r *http.Request) {
86+
path := r.URL.Path
87+
parts := strings.Split(path, "/")
88+
if len(parts) < 6 {
89+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(fmt.Errorf("invalid URL format")))
90+
return
91+
}
92+
questionId := parts[3]
93+
testCaseId := parts[5]
94+
95+
fmt.Printf("Extracted question ID: %s\n", questionId)
96+
fmt.Printf("Extracted test case ID: %s\n", testCaseId)
97+
98+
if questionId == "" || testCaseId == "" {
99+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(fmt.Errorf("question id and test case id are required")))
100+
return
101+
}
102+
103+
err := storage.DeleteTestCaseFromQuestionById(questionId, testCaseId)
104+
if err != nil {
105+
response.WriteJson(w, http.StatusInternalServerError, response.GeneralError(err))
106+
return
107+
}
108+
109+
response.WriteJson(w, http.StatusOK, map[string]string{"message": "test case deleted successfully"})
110+
}
82111
}

pkg/storage/mongodb/mongodb.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,49 @@ func (m *MongoDB) AddTestCaseToQuestion(questionId string, testCase types.TestCa
449449
fmt.Printf("Updated question. Modified count: %d\n", result.ModifiedCount)
450450

451451
return testCaseId, nil
452+
}
453+
454+
func (m *MongoDB) DeleteTestCaseFromQuestionById(questionId string, testCaseId string) error {
455+
questionObjID, err := primitive.ObjectIDFromHex(questionId)
456+
if err != nil {
457+
return fmt.Errorf("invalid question id format")
458+
}
459+
460+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
461+
defer cancel()
462+
463+
// Check if question exists
464+
var question types.Question
465+
err = m.db.Collection("questions").FindOne(ctx, bson.M{"_id": questionObjID}).Decode(&question)
466+
if err != nil {
467+
if err == mongo.ErrNoDocuments {
468+
return fmt.Errorf("no question found with the given id")
469+
}
470+
return fmt.Errorf("error checking question existence: %v", err)
471+
}
472+
473+
found := false
474+
for _, tcID := range question.TestCaseIDs {
475+
if tcID == testCaseId {
476+
found = true
477+
break
478+
}
479+
}
480+
if !found {
481+
return fmt.Errorf("no test case found with the given id in the question")
482+
}
483+
484+
filter := bson.M{"_id": questionObjID}
485+
update := bson.M{"$pull": bson.M{"test_case_ids": testCaseId}}
486+
487+
result, err := m.db.Collection("questions").UpdateOne(ctx, filter, update)
488+
if err != nil {
489+
return fmt.Errorf("failed to update question: %v", err)
490+
}
491+
492+
if result.ModifiedCount == 0 {
493+
return fmt.Errorf("no test case found with the given id in the question")
494+
}
495+
496+
return nil
452497
}

pkg/storage/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ type Storage interface {
2222
GetQuestionById(id string) ([]bson.M, error)
2323
AddQuestionToContest(contestId string, question types.Question) (string, error)
2424
AddTestCaseToQuestion(questionId string, testCase types.TestCase) (string, error)
25+
DeleteTestCaseFromQuestionById(questionId string, testCaseId string) error
2526
}

0 commit comments

Comments
 (0)