From 49dc57e3368c6b6557520b671c9f1c3b4fe26db8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 30 Apr 2014 23:48:01 -0400 Subject: [PATCH] Add /api/v1/users/search --- gogs.go | 2 +- models/issue.go | 7 +------ models/user.go | 19 ++++++++++++++++++ routers/api/v1/users.go | 40 ++++++++++++++++++++++++++++++++++++++ templates/base/footer.tmpl | 6 +++--- web.go | 4 ++++ 6 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 routers/api/v1/users.go diff --git a/gogs.go b/gogs.go index 3901b1e543..7f7267ecd6 100644 --- a/gogs.go +++ b/gogs.go @@ -35,7 +35,7 @@ func main() { CmdWeb, CmdServ, CmdUpdate, - CmdFix, + // CmdFix, } app.Flags = append(app.Flags, []cli.Flag{}...) app.Run(os.Args) diff --git a/models/issue.go b/models/issue.go index 26b8cecdd5..6a32312eed 100644 --- a/models/issue.go +++ b/models/issue.go @@ -70,12 +70,7 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, return nil, err } - if err = sess.Commit(); err != nil { - //sess.Rollback() - return nil, err - } - - return issue, nil + return issue, sess.Commit() } // GetIssueById returns issue object by given id. diff --git a/models/user.go b/models/user.go index 661eff2fdd..6751d11d49 100644 --- a/models/user.go +++ b/models/user.go @@ -409,6 +409,25 @@ func GetUserByEmail(email string) (*User, error) { return user, nil } +// SearchUserByName returns given number of users whose name contains keyword. +func SearchUserByName(key string, limit int) (us []*User, err error) { + // Prevent SQL inject. + key = strings.TrimSpace(key) + if len(key) == 0 { + return us, nil + } + + key = strings.Split(key, " ")[0] + if len(key) == 0 { + return us, nil + } + key = strings.ToLower(key) + + us = make([]*User, 0, limit) + err = orm.Limit(limit).Where("lower_name like '%" + key + "%'").Find(&us) + return us, err +} + // LoginUserPlain validates user by raw user name and password. func LoginUserPlain(uname, passwd string) (*User, error) { var u *User diff --git a/routers/api/v1/users.go b/routers/api/v1/users.go new file mode 100644 index 0000000000..a5ad8fbca0 --- /dev/null +++ b/routers/api/v1/users.go @@ -0,0 +1,40 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package v1 + +import ( + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/middleware" +) + +type user struct { + UserName string `json:"username"` + AvatarLink string `json:"avatar"` +} + +func SearchUser(ctx *middleware.Context) { + q := ctx.Query("q") + limit, err := base.StrTo(ctx.Query("limit")).Int() + if err != nil { + limit = 10 + } + + us, err := models.SearchUserByName(q, limit) + if err != nil { + ctx.JSON(500, nil) + return + } + + results := make([]*user, len(us)) + for i := range us { + results[i] = &user{us[i].Name, us[i].AvatarLink()} + } + + ctx.Render.JSON(200, map[string]interface{}{ + "ok": true, + "data": results, + }) +} diff --git a/templates/base/footer.tmpl b/templates/base/footer.tmpl index 30f068913c..23a1173f99 100644 --- a/templates/base/footer.tmpl +++ b/templates/base/footer.tmpl @@ -10,12 +10,12 @@

-
+
-
diff --git a/web.go b/web.go index 23fa10796c..962764af5d 100644 --- a/web.go +++ b/web.go @@ -75,7 +75,11 @@ func runWeb(*cli.Context) { m.Get("/help", routers.Help) m.Group("/api/v1", func(r martini.Router) { + // Miscellaneous. r.Post("/markdown", v1.Markdown) + + // Users. + r.Get("/users/search", v1.SearchUser) }) avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")