Skip to content

Commit c711fd3

Browse files
committed
edit testcasebyId completed
1 parent 9b83afd commit c711fd3

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

cmd/portal-api/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ router.Handle("GET /api/admin/test",
7676
router.HandleFunc("POST /api/question",question.CreateQuestion(storage))
7777
router.HandleFunc("PUT /api/question/{id}",question.EditQuestionById(storage))
7878
router.HandleFunc("POST /api/testcase",testcase.CreateTestCase(storage))
79+
router.HandleFunc("PUT /api/testcase/{id}",testcase.EditTestCaseById(storage))
7980
router.HandleFunc("GET /api/contest",contest.GetAllContests(storage))
8081
router.HandleFunc("GET /api/contest/{id}",contest.GetContestById(storage))
8182
router.HandleFunc("GET /api/question/{id}",question.GetQuestionById(storage))

pkg/http/handler/testcase/testcase.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package testcase
33
import (
44
"encoding/json"
55
"net/http"
6-
6+
"strings"
7+
"fmt"
78
"github.com/krishkumar84/bdcoe-golang-portal/pkg/storage"
89
"github.com/krishkumar84/bdcoe-golang-portal/pkg/types"
910
"github.com/krishkumar84/bdcoe-golang-portal/pkg/utils/response"
@@ -25,4 +26,29 @@ func CreateTestCase(storage storage.Storage) http.HandlerFunc {
2526

2627
response.WriteJson(w, http.StatusCreated, map[string]string{"test_case_id": testCaseId})
2728
}
29+
}
30+
31+
func EditTestCaseById(storage storage.Storage) http.HandlerFunc {
32+
return func(w http.ResponseWriter, r *http.Request) {
33+
path := r.URL.Path
34+
id := path[strings.LastIndex(path, "/")+1:]
35+
36+
if id == "" {
37+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(fmt.Errorf("test case id is required")))
38+
return
39+
}
40+
41+
var testCaseReq types.TestCase
42+
if err := json.NewDecoder(r.Body).Decode(&testCaseReq); err != nil {
43+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(err))
44+
return
45+
}
46+
47+
if err := storage.EditTestCaseById(id, testCaseReq); err != nil {
48+
response.WriteJson(w, http.StatusInternalServerError, response.GeneralError(err))
49+
return
50+
}
51+
52+
response.WriteJson(w, http.StatusOK, map[string]string{"status": "success", "message": "test case updated successfully"})
53+
}
2854
}

pkg/storage/mongodb/mongodb.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,45 @@ func (m *MongoDB) AddTestCaseToQuestion(questionId string, testCase types.TestCa
548548
return testCaseId, nil
549549
}
550550

551+
func (m *MongoDB) EditTestCaseById(id string, updateData types.TestCase) error {
552+
testCaseObjID, err := primitive.ObjectIDFromHex(id)
553+
if err != nil {
554+
return fmt.Errorf("invalid test case id format")
555+
}
556+
557+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
558+
defer cancel()
559+
560+
var testCase types.TestCase
561+
err = m.db.Collection("test_cases").FindOne(ctx, bson.M{"_id": testCaseObjID}).Decode(&testCase)
562+
if err != nil {
563+
if err == mongo.ErrNoDocuments {
564+
return fmt.Errorf("no test case found with the given id")
565+
}
566+
return fmt.Errorf("error checking test case existence: %v", err)
567+
}
568+
569+
// Prepare the update
570+
update := bson.M{}
571+
if updateData.Input != nil {
572+
update["input"] = updateData.Input
573+
}
574+
if updateData.ExpectedOutput != nil {
575+
update["expected_output"] = updateData.ExpectedOutput
576+
}
577+
if updateData.Visibility != "" {
578+
update["visibility"] = updateData.Visibility
579+
}
580+
if len(update) > 0 {
581+
_, err = m.db.Collection("test_cases").UpdateOne(ctx, bson.M{"_id": testCaseObjID}, bson.M{"$set": update})
582+
if err != nil {
583+
return fmt.Errorf("failed to update test case: %v", err)
584+
}
585+
}
586+
587+
return nil
588+
}
589+
551590
func (m *MongoDB) DeleteTestCaseFromQuestionById(questionId string, testCaseId string) error {
552591
questionObjID, err := primitive.ObjectIDFromHex(questionId)
553592
if err != nil {

pkg/storage/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ type Storage interface {
2525
AddQuestionToContest(contestId string, question types.Question) (string, error)
2626
AddTestCaseToQuestion(questionId string, testCase types.TestCase) (string, error)
2727
DeleteTestCaseFromQuestionById(questionId string, testCaseId string) error
28+
EditTestCaseById(testCaseId string, testCase types.TestCase) error
2829
}

0 commit comments

Comments
 (0)