Skip to content

Commit 625e00f

Browse files
committed
createquestion api completed
1 parent 696408b commit 625e00f

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

cmd/portal-api/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313

1414
"github.com/krishkumar84/bdcoe-golang-portal/pkg/config"
1515
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/auth"
16+
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/contest"
17+
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/question"
1618
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/test"
19+
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/testcase"
1720
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/users"
1821
"github.com/krishkumar84/bdcoe-golang-portal/pkg/middleware"
1922
"github.com/krishkumar84/bdcoe-golang-portal/pkg/storage/mongodb"
20-
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/contest"
21-
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/question"
22-
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/testcase"
2323
// "github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/users"
2424
)
2525

pkg/storage/mongodb/mongodb.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,16 @@ func (m *MongoDB) CreateContest(contest types.Contest) (string, error) {
101101
collection := m.db.Collection("contests")
102102
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
103103
defer cancel()
104+
104105
if err := validator.New().Struct(contest); err != nil {
105106
validateErrs := err.(validator.ValidationErrors)
106107
return "", fmt.Errorf("validation failed: %v", validateErrs)
107108
}
109+
110+
if contest.QuestionIDs == nil {
111+
contest.QuestionIDs = []string{}
112+
}
113+
108114
result, err := collection.InsertOne(ctx, contest)
109115
if err != nil {
110116
return "", err

pkg/storage/storage.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package storage
2+
23
import (
34
"github.com/krishkumar84/bdcoe-golang-portal/pkg/types"
4-
"go.mongodb.org/mongo-driver/bson"
5+
"go.mongodb.org/mongo-driver/bson"
56
)
67

78

pkg/types/models.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ type Contest struct {
2727
StartTime time.Time `bson:"start_time" json:"start_time" validate:"required"`
2828
EndTime time.Time `bson:"end_time" json:"end_time" validate:"required"`
2929
Description string `bson:"description" json:"description" validate:"required"`
30-
CreatedBy string `bson:"created_by" json:"created_by"`
31-
QuestionIDs []string `bson:"question_ids" json:"question_ids" validate:"required"`
32-
CreatedAt time.Time `bson:"created_at" json:"created_at"`
30+
CreatedBy string `bson:"created_by" json:"created_by"`
31+
QuestionIDs []string `bson:"question_ids" json:"question_ids,omitempty"`
32+
CreatedAt time.Time `bson:"created_at" json:"created_at"`
3333
}
3434

3535
type Question struct {

0 commit comments

Comments
 (0)