Skip to content

Commit a2a66d9

Browse files
committed
add question to contest and create contest completed
1 parent 625e00f commit a2a66d9

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
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("GET /api/contest",contest.GetAllContests(storage))
7777
router.HandleFunc("GET /api/contest/{id}",contest.GetContestById(storage))
7878
router.HandleFunc("GET /api/question/{id}",question.GetQuestionById(storage))
79+
router.HandleFunc("POST /api/contest/{id}/question", contest.AddQuestionToContest(storage))
7980

8081
//start server
8182

pkg/http/handler/contest/contest.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,37 @@ func GetContestById(storage storage.Storage) http.HandlerFunc {
6363
}
6464
response.WriteJson(w, http.StatusOK, contest)
6565
}
66+
}
67+
68+
func AddQuestionToContest(storage storage.Storage) http.HandlerFunc {
69+
return func(w http.ResponseWriter, r *http.Request) {
70+
path := r.URL.Path
71+
parts := strings.Split(path, "/")
72+
if len(parts) < 5 {
73+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(fmt.Errorf("invalid URL format")))
74+
return
75+
}
76+
contestId := parts[3]
77+
78+
fmt.Printf("Extracted contest ID: %s\n", contestId)
79+
80+
if contestId == "" {
81+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(fmt.Errorf("contest id is required")))
82+
return
83+
}
84+
85+
var question types.Question
86+
if err := json.NewDecoder(r.Body).Decode(&question); err != nil {
87+
response.WriteJson(w, http.StatusBadRequest, response.GeneralError(err))
88+
return
89+
}
90+
91+
questionId, err := storage.AddQuestionToContest(contestId, question)
92+
if err != nil {
93+
response.WriteJson(w, http.StatusInternalServerError, response.GeneralError(err))
94+
return
95+
}
96+
97+
response.WriteJson(w, http.StatusCreated, map[string]string{"question_id": questionId})
98+
}
6699
}

pkg/storage/mongodb/mongodb.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,39 @@ func (m *MongoDB) GetQuestionById(id string) ([]bson.M, error) {
313313
}
314314

315315
return results, nil
316+
}
317+
318+
func (m *MongoDB) AddQuestionToContest(contestId string, question types.Question) (string, error) {
319+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
320+
defer cancel()
321+
322+
fmt.Printf("Received contest ID: %s\n", contestId)
323+
324+
question.TestCaseIDs = []string{}
325+
326+
//Create question
327+
questionResult, err := m.db.Collection("questions").InsertOne(ctx, question)
328+
if err != nil {
329+
return "", fmt.Errorf("failed to create question: %v", err)
330+
}
331+
questionId := questionResult.InsertedID.(primitive.ObjectID).Hex()
332+
333+
fmt.Printf("Created question with ID: %s\n", questionId)
334+
335+
contestObjID, err := primitive.ObjectIDFromHex(contestId)
336+
if err != nil {
337+
return "", fmt.Errorf("invalid contest id format: %v", err)
338+
}
339+
340+
filter := bson.M{"_id": contestObjID}
341+
update := bson.M{"$push": bson.M{"question_ids": questionId}}
342+
343+
result, err := m.db.Collection("contests").UpdateOne(ctx, filter, update)
344+
if err != nil {
345+
return "", fmt.Errorf("failed to update contest: %v", err)
346+
}
347+
348+
fmt.Printf("Updated contest. Modified count: %d\n", result.ModifiedCount)
349+
350+
return questionId, nil
316351
}

pkg/storage/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ type Storage interface {
1818
GetAllContests() ([]types.ContestBasicInfo, error)
1919
GetContestById(id string) ([]bson.M, error)
2020
GetQuestionById(id string) ([]bson.M, error)
21+
AddQuestionToContest(contestId string, question types.Question) (string, error)
2122
}

0 commit comments

Comments
 (0)