Add user lookup (search for user with only initial part of their username)

This commit is contained in:
Howl 2016-05-14 22:43:47 +02:00
parent 35fb27e3c1
commit 42437bde44
2 changed files with 42 additions and 0 deletions

View File

@ -32,6 +32,7 @@ func Start(conf common.Conf, db *sql.DB) *gin.Engine {
gv1.GET("/users/whatid", Method(v1.UserWhatsTheIDGET, db, common.PrivilegeRead))
gv1.GET("/users/full", Method(v1.UserFullGET, db, common.PrivilegeRead))
gv1.GET("/users/userpage", Method(v1.UserUserpageGET, db, common.PrivilegeRead))
gv1.GET("/users/lookup", Method(v1.UserLookupGET, db, common.PrivilegeRead))
gv1.GET("/badges", Method(v1.BadgesGET, db, common.PrivilegeRead))
// ReadConfidential privilege required

View File

@ -275,3 +275,44 @@ func whereClauseUser(md common.MethodData, tableName string) (*common.CodeMessag
a := common.SimpleResponse(400, "you need to pass either querystring parameters name or id")
return &a, "", nil
}
type userLookupResponse struct {
common.ResponseBase
Users []lookupUser `json:"users"`
}
type lookupUser struct {
ID int `json:"id"`
Username string `json:"username"`
}
// UserLookupGET does a quick lookup of users beginning with the passed
// querystring value name.
func UserLookupGET(md common.MethodData) common.CodeMessager {
name := strings.NewReplacer(
"%", "\\%",
"_", "\\_",
).Replace(md.C.Query("name"))
if name == "" {
return common.SimpleResponse(400, "please provide an username to start searching")
}
name += "%"
rows, err := md.DB.Query("SELECT users.id, users.username FROM users WHERE username LIKE ? AND allowed = '1' LIMIT 25", name)
if err != nil {
md.Err(err)
return Err500
}
var r userLookupResponse
for rows.Next() {
var l lookupUser
err := rows.Scan(&l.ID, &l.Username)
if err != nil {
continue // can't be bothered to handle properly
}
r.Users = append(r.Users, l)
}
r.Code = 200
return r
}