Skip to content

Commit 7a8dfce

Browse files
committed
get all contest api
1 parent 65ebb24 commit 7a8dfce

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

cmd/portal-api/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ router.Handle("GET /api/admin/test",
7373
router.HandleFunc("POST /api/contest",contest.CreateContest(storage))
7474
router.HandleFunc("POST /api/question",question.CreateQuestion(storage))
7575
router.HandleFunc("POST /api/testcase",testcase.CreateTestCase(storage))
76+
router.HandleFunc("GET /api/contest",contest.GetAllContests(storage))
7677
// router.HandleFunc("GET /api/users/{id}",users.GetById(storage))
7778
// router.HandleFunc("GET /api/users",users.GetAll(storage))
7879

pkg/http/handler/contest/contest.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package contest
22

33
import (
4-
"net/http"
54
"encoding/json"
5+
"net/http"
6+
67
"github.com/krishkumar84/bdcoe-golang-portal/pkg/storage"
78
"github.com/krishkumar84/bdcoe-golang-portal/pkg/types"
89
"github.com/krishkumar84/bdcoe-golang-portal/pkg/utils/response"
@@ -24,4 +25,16 @@ func CreateContest(storage storage.Storage) http.HandlerFunc {
2425

2526
response.WriteJson(w, http.StatusCreated, map[string]string{"contest_id": contestId})
2627
}
28+
}
29+
30+
func GetAllContests(storage storage.Storage) http.HandlerFunc {
31+
return func(w http.ResponseWriter, r *http.Request) {
32+
contests, err := storage.GetAllContests()
33+
if err != nil {
34+
response.WriteJson(w, http.StatusInternalServerError, response.GeneralError(err))
35+
return
36+
}
37+
38+
response.WriteJson(w, http.StatusOK, contests)
39+
}
2740
}

pkg/storage/mongodb/mongodb.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/go-playground/validator/v10"
89
"github.com/krishkumar84/bdcoe-golang-portal/pkg/config"
910
"github.com/krishkumar84/bdcoe-golang-portal/pkg/types"
1011
"go.mongodb.org/mongo-driver/bson"
1112
"go.mongodb.org/mongo-driver/bson/primitive"
1213
"go.mongodb.org/mongo-driver/mongo"
1314
"go.mongodb.org/mongo-driver/mongo/options"
14-
"github.com/go-playground/validator/v10"
1515
)
1616

1717
type MongoDB struct {
@@ -147,4 +147,31 @@ func (m *MongoDB) CreateTestCase(testCase types.TestCase) (string, error) {
147147
}
148148

149149
return result.InsertedID.(primitive.ObjectID).Hex(), nil
150+
}
151+
152+
func (m *MongoDB) GetAllContests() ([]types.ContestBasicInfo, error) {
153+
collection := m.db.Collection("contests")
154+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
155+
defer cancel()
156+
157+
projection := bson.D{
158+
{Key: "_id", Value: 1},
159+
{Key: "title", Value: 1},
160+
{Key: "start_time", Value: 1},
161+
{Key: "end_time", Value: 1},
162+
{Key: "description", Value: 1},
163+
}
164+
165+
var contests []types.ContestBasicInfo
166+
cursor, err := collection.Find(ctx, bson.M{}, options.Find().SetProjection(projection))
167+
if err != nil {
168+
return nil, err
169+
}
170+
defer cursor.Close(ctx)
171+
172+
if err = cursor.All(ctx, &contests); err != nil {
173+
return nil, err
174+
}
175+
176+
return contests, nil
150177
}

pkg/storage/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ type Storage interface {
1111
CreateContest(contest types.Contest) (string, error)
1212
CreateQuestion(question types.Question) (string, error)
1313
CreateTestCase(testCase types.TestCase) (string, error)
14+
GetAllContests() ([]types.ContestBasicInfo, error)
1415
}

pkg/types/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package types
2+
3+
import "time"
4+
type ContestBasicInfo struct {
5+
ID string `bson:"_id" json:"contest_id"`
6+
Title string `bson:"title" json:"title"`
7+
StartTime time.Time `bson:"start_time" json:"start_time"`
8+
EndTime time.Time `bson:"end_time" json:"end_time"`
9+
Description string `bson:"description" json:"description"`
10+
}

0 commit comments

Comments
 (0)