From 6bae1e23905cf05a7346e87fb6e0d84552b31f1d Mon Sep 17 00:00:00 2001 From: Howl Date: Sun, 3 Apr 2016 23:09:28 +0200 Subject: [PATCH] User retrieval by both ID and username --- app/start.go | 3 ++- app/v1/user.go | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/app/start.go b/app/start.go index 6f94383..9b2b4f9 100644 --- a/app/start.go +++ b/app/start.go @@ -24,7 +24,8 @@ func Start(conf common.Conf, db *sql.DB) { gv1.GET("/privileges", Method(v1.PrivilegesGET, db)) // Read privilege required - gv1.GET("/users/:id", Method(v1.UserGET, db, common.PrivilegeRead)) + gv1.GET("/users/id/:id", Method(v1.UserByIDGET, db, common.PrivilegeRead)) + gv1.GET("/users/name/:name", Method(v1.UserByNameGET, db, common.PrivilegeRead)) gv1.GET("/badges", Method(v1.BadgesGET, db, common.PrivilegeRead)) gv1.GET("/badges/:id", Method(v1.BadgeByIDGET, db, common.PrivilegeRead)) } diff --git a/app/v1/user.go b/app/v1/user.go index 56b118c..30a3ce6 100644 --- a/app/v1/user.go +++ b/app/v1/user.go @@ -22,8 +22,8 @@ type userData struct { Badges []int `json:"badges"` } -// UserGET is the API handler for GET /user/:id -func UserGET(md common.MethodData) (r common.Response) { +// UserByIDGET is the API handler for GET /users/id/:id +func UserByIDGET(md common.MethodData) (r common.Response) { var err error var uid int uidStr := md.C.Param("id") @@ -38,14 +38,45 @@ func UserGET(md common.MethodData) (r common.Response) { } } - user := userData{} + query := ` +SELECT users.id, users.username, register_datetime, rank, + latest_activity, users_stats.username_aka, users_stats.badges_shown, + users_stats.country, users_stats.show_country +FROM users +LEFT JOIN users_stats +ON users.id=users_stats.id +WHERE users.id=? +LIMIT 1` + r = userPuts(md, md.DB.QueryRow(query, uid)) + return +} + +// UserByNameGET is the API handler for GET /users/name/:name +func UserByNameGET(md common.MethodData) (r common.Response) { + username := md.C.Param("name") + + query := ` +SELECT users.id, users.username, register_datetime, rank, + latest_activity, users_stats.username_aka, users_stats.badges_shown, + users_stats.country, users_stats.show_country +FROM users +LEFT JOIN users_stats +ON users.id=users_stats.id +WHERE users.username=? +LIMIT 1` + r = userPuts(md, md.DB.QueryRow(query, username)) + return +} + +func userPuts(md common.MethodData, row *sql.Row) (r common.Response) { + var err error + var user userData registeredOn := int64(0) latestActivity := int64(0) var badges string var showcountry bool - err = md.DB.QueryRow("SELECT users.id, users.username, register_datetime, rank, latest_activity, users_stats.username_aka, users_stats.badges_shown, users_stats.country, users_stats.show_country FROM users LEFT JOIN users_stats ON users.id=users_stats.id WHERE users.id=? LIMIT 1", uid).Scan( - &user.ID, &user.Username, ®isteredOn, &user.Rank, &latestActivity, &user.UsernameAKA, &badges, &user.Country, &showcountry) + err = row.Scan(&user.ID, &user.Username, ®isteredOn, &user.Rank, &latestActivity, &user.UsernameAKA, &badges, &user.Country, &showcountry) switch { case err == sql.ErrNoRows: r.Code = 404