peppyapi: replicate behaviour of original API (check for user ID first, then for username)
This commit is contained in:
parent
5320c62e17
commit
2afc557ef8
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue
Block a user