Add /api/v1/users/search
This commit is contained in:
parent
de46c06d2e
commit
49dc57e336
6 changed files with 68 additions and 10 deletions
2
gogs.go
2
gogs.go
|
@ -35,7 +35,7 @@ func main() {
|
||||||
CmdWeb,
|
CmdWeb,
|
||||||
CmdServ,
|
CmdServ,
|
||||||
CmdUpdate,
|
CmdUpdate,
|
||||||
CmdFix,
|
// CmdFix,
|
||||||
}
|
}
|
||||||
app.Flags = append(app.Flags, []cli.Flag{}...)
|
app.Flags = append(app.Flags, []cli.Flag{}...)
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
|
|
|
@ -70,12 +70,7 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = sess.Commit(); err != nil {
|
return issue, sess.Commit()
|
||||||
//sess.Rollback()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return issue, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetIssueById returns issue object by given id.
|
// GetIssueById returns issue object by given id.
|
||||||
|
|
|
@ -409,6 +409,25 @@ func GetUserByEmail(email string) (*User, error) {
|
||||||
return user, nil
|
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.
|
// LoginUserPlain validates user by raw user name and password.
|
||||||
func LoginUserPlain(uname, passwd string) (*User, error) {
|
func LoginUserPlain(uname, passwd string) (*User, error) {
|
||||||
var u *User
|
var u *User
|
||||||
|
|
40
routers/api/v1/users.go
Normal file
40
routers/api/v1/users.go
Normal file
|
@ -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,
|
||||||
|
})
|
||||||
|
}
|
|
@ -10,12 +10,12 @@
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-md-1" style="margin: -5px;">
|
<div class="col-md-2" style="margin: -5px;">
|
||||||
<a target="_blank" href="https://github.com/gogits/gogs"><i class="fa fa-github fa-2x"></i></a>
|
<a target="_blank" href="https://github.com/gogits/gogs"><i class="fa fa-github fa-2x"></i></a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-md-5">
|
<div class="col-md-4">
|
||||||
<p class="desc"></p>
|
<p class="desc"><a href="http://gogits.org">Official Website</a></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
4
web.go
4
web.go
|
@ -75,7 +75,11 @@ func runWeb(*cli.Context) {
|
||||||
m.Get("/help", routers.Help)
|
m.Get("/help", routers.Help)
|
||||||
|
|
||||||
m.Group("/api/v1", func(r martini.Router) {
|
m.Group("/api/v1", func(r martini.Router) {
|
||||||
|
// Miscellaneous.
|
||||||
r.Post("/markdown", v1.Markdown)
|
r.Post("/markdown", v1.Markdown)
|
||||||
|
|
||||||
|
// Users.
|
||||||
|
r.Get("/users/search", v1.SearchUser)
|
||||||
})
|
})
|
||||||
|
|
||||||
avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
|
avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
|
||||||
|
|
Loading…
Reference in a new issue