From ce739a8ae5a633dcd579b2944a58caf6f970a2a0 Mon Sep 17 00:00:00 2001 From: Howl Date: Thu, 19 May 2016 17:31:49 +0200 Subject: [PATCH] Implement users/scores/recent --- app/start.go | 1 + app/v1/user_scores.go | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/start.go b/app/start.go index c8c6c21..a2f83f5 100644 --- a/app/start.go +++ b/app/start.go @@ -38,6 +38,7 @@ func Start(conf common.Conf, dbO *sql.DB) *gin.Engine { gv1.GET("/users/userpage", Method(v1.UserUserpageGET, common.PrivilegeRead)) gv1.GET("/users/lookup", Method(v1.UserLookupGET, common.PrivilegeRead)) gv1.GET("/users/scores/best", Method(v1.UserScoresBestGET, common.PrivilegeRead)) + gv1.GET("/users/scores/recent", Method(v1.UserScoresRecentGET, common.PrivilegeRead)) gv1.GET("/badges", Method(v1.BadgesGET, common.PrivilegeRead)) // ReadConfidential privilege required diff --git a/app/v1/user_scores.go b/app/v1/user_scores.go index c877c8e..aa1c424 100644 --- a/app/v1/user_scores.go +++ b/app/v1/user_scores.go @@ -61,13 +61,6 @@ func UserScoresBestGET(md common.MethodData) common.CodeMessager { if cm != nil { return *cm } - var modeClause string - if md.C.Query("mode") != "" { - m, err := strconv.Atoi(md.C.Query("mode")) - if err == nil && m >= 0 && m <= 3 { - modeClause = fmt.Sprintf("AND scores.play_mode = '%d'", m) - } - } return scoresPuts(md, fmt.Sprintf( `WHERE scores.completed = '3' @@ -75,7 +68,23 @@ func UserScoresBestGET(md common.MethodData) common.CodeMessager { %s AND users.allowed = '1' ORDER BY scores.pp DESC, scores.score DESC %s`, - wc, modeClause, common.Paginate(md.C.Query("p"), md.C.Query("l"), 100), + wc, genModeClause(md), common.Paginate(md.C.Query("p"), md.C.Query("l"), 100), + ), param) +} + +// UserScoresRecentGET retrieves an user's latest scores. +func UserScoresRecentGET(md common.MethodData) common.CodeMessager { + cm, wc, param := whereClauseUser(md, "users") + if cm != nil { + return *cm + } + return scoresPuts(md, fmt.Sprintf( + `WHERE + %s + %s + AND users.allowed = '1' + ORDER BY scores.time DESC %s`, + wc, genModeClause(md), common.Paginate(md.C.Query("p"), md.C.Query("l"), 100), ), param) } @@ -92,6 +101,17 @@ func getMode(m string) string { } } +func genModeClause(md common.MethodData) string { + var modeClause string + if md.C.Query("mode") != "" { + m, err := strconv.Atoi(md.C.Query("mode")) + if err == nil && m >= 0 && m <= 3 { + modeClause = fmt.Sprintf("AND scores.play_mode = '%d'", m) + } + } + return modeClause +} + func scoresPuts(md common.MethodData, whereClause string, params ...interface{}) common.CodeMessager { rows, err := md.DB.Query(userScoreSelectBase+whereClause, params...) if err != nil {