From 2afc557ef87a7f944bd1347045902f7987021262 Mon Sep 17 00:00:00 2001 From: Howl Date: Wed, 18 May 2016 17:47:39 +0200 Subject: [PATCH] peppyapi: replicate behaviour of original API (check for user ID first, then for username) --- app/peppy/common.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/peppy/common.go b/app/peppy/common.go index 080bcaf..995756e 100644 --- a/app/peppy/common.go +++ b/app/peppy/common.go @@ -26,7 +26,7 @@ func genUser(c *gin.Context, db *sql.DB) (string, string) { var p string // used in second case of switch - _, err := strconv.Atoi(c.Query("u")) + s, err := strconv.Atoi(c.Query("u")) switch { // We know for sure that it's an username. @@ -35,14 +35,15 @@ func genUser(c *gin.Context, db *sql.DB) (string, string) { p = c.Query("u") // It could be an user ID, so we look for an user with that username first. case err == nil: - err = db.QueryRow("SELECT id FROM users WHERE username = ? LIMIT 1", c.Query("u")).Scan(&p) - // If there is an error, that means u is an userID. - // If there is none, p will automatically have become the user id retrieved from the database - // in the last query. + err = db.QueryRow("SELECT id FROM users WHERE id = ? LIMIT 1", s).Scan(&p) if err == sql.ErrNoRows { + // If no user with that userID were found, assume username. p = c.Query("u") + whereClause = "WHERE users.username = ?" + } else { + // An user with that userID was found. Thus it's an userID. + whereClause = "WHERE users.id = ?" } - whereClause = "WHERE users.id = ?" // u contains letters, so it's an username. default: p = c.Query("u")