From 8a3dae059285cf5344e410d7863002645f384141 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Mon, 1 May 2017 18:05:22 +0200 Subject: [PATCH] Add endpoint to retrieve people who have a certain badge --- app/start.go | 1 + app/v1/badge.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/start.go b/app/start.go index fee5512..69e79c9 100644 --- a/app/start.go +++ b/app/start.go @@ -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) diff --git a/app/v1/badge.go b/app/v1/badge.go index bcff1e2..8c3227d 100644 --- a/app/v1/badge.go +++ b/app/v1/badge.go @@ -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 +}