0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 23:33:15 -05:00

Add /api/v1/users/search

This commit is contained in:
Unknown 2014-04-30 23:48:01 -04:00
parent de46c06d2e
commit 49dc57e336
6 changed files with 68 additions and 10 deletions

View file

@ -35,7 +35,7 @@ func main() {
CmdWeb,
CmdServ,
CmdUpdate,
CmdFix,
// CmdFix,
}
app.Flags = append(app.Flags, []cli.Flag{}...)
app.Run(os.Args)

View file

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

View file

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

40
routers/api/v1/users.go Normal file
View 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,
})
}

View file

@ -10,12 +10,12 @@
</p>
</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>
</div>
<div class="col-md-5">
<p class="desc"></p>
<div class="col-md-4">
<p class="desc"><a href="http://gogits.org">Official Website</a></p>
</div>
</div>
</div>

4
web.go
View file

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