Skip to content

Commit

Permalink
"Updated leaderboard API to accept limit parameter and return top N e…
Browse files Browse the repository at this point in the history
…ntries, instead of fixed top 10"
  • Loading branch information
p-shubh committed Oct 23, 2024
1 parent 4bc8182 commit e2d0a5e
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions api/v1/leaderboard/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"sort"
"strconv"
"sync"

"github.com/NetSepio/gateway/config/dbconfig"
Expand All @@ -24,7 +25,7 @@ func ApplyRoutes(r *gin.RouterGroup) {
{
g.GET("", getLeaderboard)
}
h := r.Group("/getScoreboard/top10")
h := r.Group("/getScoreboard/top")
{
h.GET("", getScoreBoard)
}
Expand Down Expand Up @@ -66,6 +67,14 @@ func getScoreBoard(c *gin.Context) {
var response []models.UserScoreBoard

var scoreBoard []models.ScoreBoard

limitParam := c.Query("limit")
limit, err := strconv.Atoi(limitParam)
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
logwrapper.Error("failed to get scoreBoard", err)
return
}

if err := db.Order("reviews desc").Find(&scoreBoard).Error; err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
Expand Down Expand Up @@ -128,12 +137,12 @@ func getScoreBoard(c *gin.Context) {
})

// Take the top 10 entries
if len(response) > 10 {
response = response[:10]
if len(response) >= limit {
response = response[:limit]
}
}

httpo.NewSuccessResponseP(200, "ScoreBoard fetched successfully", response).SendD(c)
httpo.NewSuccessResponseP(200, "ScoreBoard fetched successfully length := "+strconv.Itoa(len(response)), response).SendD(c)
}

func getAllUsersScoreBoard(c *gin.Context) {
Expand Down

0 comments on commit e2d0a5e

Please sign in to comment.