Skip to content

Commit 2744490

Browse files
authored
Merge pull request #92 from ABHINAVGARG05/master
added leaderboard
2 parents 414de02 + 0ad54b2 commit 2744490

File tree

12 files changed

+382
-79
lines changed

12 files changed

+382
-79
lines changed

database/queries/score.sql

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,47 @@ WHERE id = $9;
1414

1515
-- name: DeleteScore :exec
1616
DELETE FROM score
17-
WHERE id = $1;
17+
WHERE id = $1;
18+
19+
-- name: GetLeaderboardWithPagination :many
20+
WITH RoundScores AS (
21+
SELECT
22+
s.team_id,
23+
t.name,
24+
s.round,
25+
s.design,
26+
s.implementation,
27+
s.presentation,
28+
s.innovation,
29+
s.teamwork,
30+
(s.design + s.implementation + s.presentation + s.innovation + s.teamwork) AS round_total
31+
FROM score s
32+
JOIN teams t ON s.team_id = t.id
33+
),
34+
TotalScores AS (
35+
SELECT
36+
s.team_id,
37+
t.name,
38+
SUM(s.design + s.implementation + s.presentation + s.innovation + s.teamwork) AS overall_total
39+
FROM score s
40+
JOIN teams t ON s.team_id = t.id
41+
GROUP BY s.team_id, t.name
42+
)
43+
SELECT
44+
rs.team_id,
45+
rs.name,
46+
rs.round,
47+
rs.design,
48+
rs.implementation,
49+
rs.presentation,
50+
rs.innovation,
51+
rs.teamwork,
52+
rs.round_total,
53+
ts.overall_total
54+
FROM RoundScores rs
55+
JOIN TotalScores ts ON rs.team_id = ts.team_id
56+
WHERE
57+
($1::UUID IS NULL OR rs.team_id > $1) -- Cursor-based pagination
58+
AND ($2::TEXT IS NULL OR rs.name ILIKE '%' || $2 || '%') -- Optional name filter
59+
ORDER BY ts.overall_total DESC, rs.round ASC
60+
LIMIT $3;

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ require (
1414
github.com/labstack/echo/v4 v4.13.2
1515
github.com/redis/go-redis/v9 v9.7.0
1616
go.uber.org/zap v1.27.0
17-
golang.org/x/crypto v0.31.0
17+
golang.org/x/crypto v0.32.0
1818
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
1919
)
2020

21+
require github.com/gabriel-vasile/mimetype v1.4.8 // indirect
22+
2123
require (
2224
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2325
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2426
github.com/go-playground/locales v0.14.1 // indirect
2527
github.com/go-playground/universal-translator v0.18.1 // indirect
28+
github.com/go-playground/validator/v10 v10.24.0
2629
github.com/jackc/pgpassfile v1.0.0 // indirect
2730
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
2831
github.com/jackc/puddle/v2 v2.2.2 // indirect
@@ -33,9 +36,9 @@ require (
3336
github.com/valyala/bytebufferpool v1.0.0 // indirect
3437
github.com/valyala/fasttemplate v1.2.2 // indirect
3538
go.uber.org/multierr v1.11.0 // indirect
36-
golang.org/x/net v0.32.0 // indirect
39+
golang.org/x/net v0.34.0 // indirect
3740
golang.org/x/sync v0.10.0 // indirect
38-
golang.org/x/sys v0.28.0 // indirect
41+
golang.org/x/sys v0.29.0 // indirect
3942
golang.org/x/text v0.21.0 // indirect
4043
golang.org/x/time v0.8.0 // indirect
4144
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1313
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1414
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
1515
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
16+
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
17+
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
1618
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
1719
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
1820
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
1921
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
2022
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
2123
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
24+
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
25+
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
2226
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
2327
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
2428
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -67,14 +71,20 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
6771
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
6872
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
6973
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
74+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
75+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
7076
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
7177
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
78+
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
79+
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
7280
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
7381
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
7482
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7583
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7684
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
7785
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
86+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
87+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7888
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
7989
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
8090
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=

pkg/controller/admin.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,88 @@ func UpdateTeamRounds(c echo.Context) error {
582582
})
583583
}
584584

585+
586+
func GetLeaderBoard(c echo.Context) error {
587+
ctx := c.Request().Context()
588+
589+
limitParam := c.QueryParam("limit")
590+
cursorParam := c.QueryParam("cursor")
591+
nameParam := c.QueryParam("name")
592+
593+
limit := 10
594+
var cursor uuid.NullUUID
595+
var err error
596+
597+
if limitParam != "" {
598+
parsedLimit, err := strconv.Atoi(limitParam)
599+
if err == nil && parsedLimit > 0 {
600+
limit = parsedLimit
601+
}
602+
}
603+
604+
if cursorParam != "" {
605+
parsedCursor, err := uuid.Parse(cursorParam)
606+
if err == nil {
607+
cursor = uuid.NullUUID{UUID: parsedCursor, Valid: true}
608+
}
609+
}
610+
611+
rows, err := utils.Queries.GetLeaderboardWithPagination(ctx, db.GetLeaderboardWithPaginationParams{
612+
Column1: cursor.UUID,
613+
Limit: int32(limit),
614+
Column2: nameParam,
615+
})
616+
if err != nil {
617+
return c.JSON(echo.ErrInternalServerError.Code, &models.Response{
618+
Status: "fail",
619+
Message: "Some error occurred",
620+
Data: map[string]string{
621+
"error": err.Error(),
622+
},
623+
})
624+
}
625+
626+
leaderboardMap := make(map[uuid.UUID]*models.TeamLeaderboard)
627+
var nextCursor uuid.NullUUID
628+
629+
for _, row := range rows {
630+
if _, exists := leaderboardMap[row.TeamID]; !exists {
631+
leaderboardMap[row.TeamID] = &models.TeamLeaderboard{
632+
TeamID: row.TeamID,
633+
TeamName: row.Name,
634+
Rounds: []models.Round{},
635+
OverallTotal: int(row.OverallTotal),
636+
}
637+
}
638+
639+
leaderboardMap[row.TeamID].Rounds = append(leaderboardMap[row.TeamID].Rounds, models.Round{
640+
Round: int(row.Round),
641+
Design: int(row.Design),
642+
Implementation: int(row.Implementation),
643+
Presentation: int(row.Presentation),
644+
Innovation: int(row.Innovation),
645+
Teamwork: int(row.Teamwork),
646+
RoundTotal: int(row.RoundTotal),
647+
})
648+
nextCursor = uuid.NullUUID{UUID: row.TeamID, Valid: true}
649+
}
650+
651+
leaderBoard := make([]models.TeamLeaderboard, 0, len(leaderboardMap))
652+
for _, team := range leaderboardMap {
653+
leaderBoard = append(leaderBoard, *team)
654+
}
655+
656+
response := map[string]interface{}{
657+
"leaderboard": leaderBoard,
658+
"next_cursor": nextCursor.UUID.String(),
659+
}
660+
661+
return c.JSON(http.StatusOK, &models.Response{
662+
Status: "success",
663+
Message: "Leaderboard fetched successfully",
664+
Data: response,
665+
})
666+
585667
func GetAllIdeas(c echo.Context) error {
586668
ctx := c.Request().Context()
587669

pkg/controller/auth.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package controller
22

33
import (
44
"errors"
5+
"fmt"
56
"net/http"
67
"strings"
78

89
"github.com/CodeChefVIT/devsoc-be-24/pkg/db"
910
logger "github.com/CodeChefVIT/devsoc-be-24/pkg/logging"
11+
"github.com/CodeChefVIT/devsoc-be-24/pkg/middleware"
1012
"github.com/CodeChefVIT/devsoc-be-24/pkg/models"
1113
"github.com/CodeChefVIT/devsoc-be-24/pkg/utils"
1214
"github.com/google/uuid"
@@ -155,6 +157,8 @@ func CompleteProfile(c echo.Context) error {
155157
})
156158
}
157159

160+
fmt.Printf("Type of req: %T\n", req)
161+
158162
if err := utils.Validate.Struct(req); err != nil {
159163
logger.Errorf(logger.InternalError, err.Error())
160164
return c.JSON(http.StatusBadRequest, &models.Response{
@@ -163,6 +167,15 @@ func CompleteProfile(c echo.Context) error {
163167
})
164168
}
165169

170+
if err := middleware.TrimSpaces(&req); err != nil {
171+
return c.JSON(echo.ErrBadRequest.Code, models.Response{
172+
Status: "fail",
173+
Data: map[string]string{
174+
"error": err.Error(),
175+
},
176+
})
177+
}
178+
166179
req.FirstName = strings.TrimSpace(req.FirstName)
167180
req.LastName = strings.TrimSpace(req.LastName)
168181

pkg/controller/score.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func GetScore(c echo.Context) error {
6565

6666
return c.JSON(http.StatusOK, &models.Response{
6767
Status: "success",
68+
Message: "Scores fetched successfully",
6869
Data: map[string]interface{}{
69-
"message": "Scores fetched successfully",
7070
"scores": scores,
7171
},
7272
})

pkg/controller/stargazer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func CheckStarred(c echo.Context) error {
148148
}
149149

150150
return c.JSON(http.StatusOK, models.Response{
151-
Status: "success",
152-
Data: "user has starred the repo",
151+
Status: "success",
152+
Message: "user has starred the repo",
153153
})
154154
}

0 commit comments

Comments
 (0)