Add endpoint to retrieve people who have a certain badge

This commit is contained in:
Morgan Bazalgette 2017-05-01 18:05:22 +02:00
parent ace4c6bee0
commit 8a3dae0592
No known key found for this signature in database
GPG Key ID: 40D328300D245DA5
2 changed files with 33 additions and 0 deletions

View File

@ -97,6 +97,7 @@ func Start(conf common.Conf, dbO *sqlx.DB) *fhr.Router {
r.Method("/api/v1/users/scores/best", v1.UserScoresBestGET)
r.Method("/api/v1/users/scores/recent", v1.UserScoresRecentGET)
r.Method("/api/v1/badges", v1.BadgesGET)
r.Method("/api/v1/badges/members", v1.BadgeMembersGET)
r.Method("/api/v1/beatmaps", v1.BeatmapGET)
r.Method("/api/v1/leaderboard", v1.LeaderboardGET)
r.Method("/api/v1/tokens", v1.TokenGET)

View File

@ -48,3 +48,35 @@ func BadgesGET(md common.MethodData) common.CodeMessager {
r.ResponseBase.Code = 200
return r
}
type badgeMembersData struct {
common.ResponseBase
Members []userData `json:"members"`
}
// BadgeMembersGET retrieves the people who have a certain badge.
func BadgeMembersGET(md common.MethodData) common.CodeMessager {
i := common.Int(md.Query("id"))
if i == 0 {
return ErrMissingField("id")
}
var members badgeMembersData
err := md.DB.Select(&members.Members, `SELECT users.id, users.username, register_datetime, users.privileges,
latest_activity, users_stats.username_aka,
users_stats.country
FROM user_badges ub
INNER JOIN users ON users.id = ub.user
INNER JOIN users_stats ON users_stats.id = ub.user
WHERE badge = ?
ORDER BY id ASC `+common.Paginate(md.Query("p"), md.Query("l"), 50), i)
if err != nil {
md.Err(err)
return Err500
}
members.Code = 200
return members
}