Skip to content

Commit 3b96ca3

Browse files
committed
Add endpoint to get indivudal scores for clan score
1 parent 08cccd6 commit 3b96ca3

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

cmd/api/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func initializeRoutes(engine *gin.Engine) {
101101

102102
// Clan Scores
103103
engine.GET("/v2/clan/:id/scores/:mode", handlers.CreateHandler(handlers.GetClanScoresForMode))
104+
engine.GET("/v2/clan/scores/:id", handlers.CreateHandler(handlers.GetUserScoresForClanScore))
104105

105106
// Users
106107
engine.POST("/v2/user", handlers.CreateHandler(handlers.RegisterNewUser))

db/clan_scores.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ func GetClanScore(md5 string, clanId int) (*ClanScore, error) {
5252
return &score, nil
5353
}
5454

55+
// GetClanScoreById Retrieves a clan score by id
56+
func GetClanScoreById(id int) (*ClanScore, error) {
57+
var score ClanScore
58+
59+
result := SQL.
60+
Where("id = ?", id).
61+
First(&score)
62+
63+
if result.Error != nil {
64+
return nil, result.Error
65+
}
66+
67+
return &score, nil
68+
}
69+
5570
// GetClanScoresForMode Retrieves all clan scores for a given mode
5671
func GetClanScoresForMode(clanId int, mode enums.GameMode) ([]*ClanScore, error) {
5772
clanScores := make([]*ClanScore, 0)
@@ -90,7 +105,7 @@ func GetClanScoresForModeFull(clanId int, mode enums.GameMode, page int) ([]*Cla
90105

91106
// CalculateClanScore Calculates a clan score for a given map
92107
func CalculateClanScore(md5 string, clanId int, mode enums.GameMode) (*ClanScore, error) {
93-
scores, err := GetClanPlayerScoresOnMap(md5, clanId)
108+
scores, err := GetClanPlayerScoresOnMap(md5, clanId, false)
94109

95110
if err != nil {
96111
return nil, err

db/scores.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func GetUserPersonalBestScoreRate(userId int, md5 string, mods int64) (*Score, e
543543
}
544544

545545
// GetClanPlayerScoresOnMap Fetches the top 10 scores from a clan on a given map
546-
func GetClanPlayerScoresOnMap(md5 string, clanId int) ([]*Score, error) {
546+
func GetClanPlayerScoresOnMap(md5 string, clanId int, callAfterFind bool) ([]*Score, error) {
547547
scores := make([]*Score, 0)
548548

549549
result := SQL.Raw(fmt.Sprintf(`
@@ -560,6 +560,13 @@ func GetClanPlayerScoresOnMap(md5 string, clanId int) ([]*Score, error) {
560560
return nil, result.Error
561561
}
562562

563+
if callAfterFind {
564+
for _, score := range scores {
565+
_ = score.AfterFind(nil)
566+
_ = score.User.AfterFind(nil)
567+
}
568+
}
569+
563570
return scores, nil
564571
}
565572

handlers/clan_scores.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/Quaver/api2/db"
55
"github.com/Quaver/api2/enums"
66
"github.com/gin-gonic/gin"
7+
"gorm.io/gorm"
78
"net/http"
89
"strconv"
910
)
@@ -38,3 +39,32 @@ func GetClanScoresForMode(c *gin.Context) *APIError {
3839
c.JSON(http.StatusOK, gin.H{"scores": scores})
3940
return nil
4041
}
42+
43+
// GetUserScoresForClanScore Retrieves all individual scores that make up a clan score
44+
// Endpoint: /v2/clan/scores/:id
45+
func GetUserScoresForClanScore(c *gin.Context) *APIError {
46+
id, err := strconv.Atoi(c.Param("id"))
47+
48+
if err != nil {
49+
return APIErrorBadRequest("Invalid id")
50+
}
51+
52+
clanScore, err := db.GetClanScoreById(id)
53+
54+
if err != nil && err != gorm.ErrRecordNotFound {
55+
return APIErrorServerError("Error retrieving clan score from db", err)
56+
}
57+
58+
if clanScore == nil {
59+
return APIErrorNotFound("Clan score")
60+
}
61+
62+
scores, err := db.GetClanPlayerScoresOnMap(clanScore.MapMD5, clanScore.ClanId, true)
63+
64+
if err != nil {
65+
return APIErrorServerError("Error retrieving player clan scores from db", err)
66+
}
67+
68+
c.JSON(http.StatusOK, gin.H{"scores": scores})
69+
return nil
70+
}

0 commit comments

Comments
 (0)