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 @@
-