Skip to content

Commit 49dc57e

Browse files
committed
Add /api/v1/users/search
1 parent de46c06 commit 49dc57e

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

gogs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
CmdWeb,
3636
CmdServ,
3737
CmdUpdate,
38-
CmdFix,
38+
// CmdFix,
3939
}
4040
app.Flags = append(app.Flags, []cli.Flag{}...)
4141
app.Run(os.Args)

models/issue.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int,
7070
return nil, err
7171
}
7272

73-
if err = sess.Commit(); err != nil {
74-
//sess.Rollback()
75-
return nil, err
76-
}
77-
78-
return issue, nil
73+
return issue, sess.Commit()
7974
}
8075

8176
// GetIssueById returns issue object by given id.

models/user.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,25 @@ func GetUserByEmail(email string) (*User, error) {
409409
return user, nil
410410
}
411411

412+
// SearchUserByName returns given number of users whose name contains keyword.
413+
func SearchUserByName(key string, limit int) (us []*User, err error) {
414+
// Prevent SQL inject.
415+
key = strings.TrimSpace(key)
416+
if len(key) == 0 {
417+
return us, nil
418+
}
419+
420+
key = strings.Split(key, " ")[0]
421+
if len(key) == 0 {
422+
return us, nil
423+
}
424+
key = strings.ToLower(key)
425+
426+
us = make([]*User, 0, limit)
427+
err = orm.Limit(limit).Where("lower_name like '%" + key + "%'").Find(&us)
428+
return us, err
429+
}
430+
412431
// LoginUserPlain validates user by raw user name and password.
413432
func LoginUserPlain(uname, passwd string) (*User, error) {
414433
var u *User

routers/api/v1/users.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package v1
6+
7+
import (
8+
"github.com/gogits/gogs/models"
9+
"github.com/gogits/gogs/modules/base"
10+
"github.com/gogits/gogs/modules/middleware"
11+
)
12+
13+
type user struct {
14+
UserName string `json:"username"`
15+
AvatarLink string `json:"avatar"`
16+
}
17+
18+
func SearchUser(ctx *middleware.Context) {
19+
q := ctx.Query("q")
20+
limit, err := base.StrTo(ctx.Query("limit")).Int()
21+
if err != nil {
22+
limit = 10
23+
}
24+
25+
us, err := models.SearchUserByName(q, limit)
26+
if err != nil {
27+
ctx.JSON(500, nil)
28+
return
29+
}
30+
31+
results := make([]*user, len(us))
32+
for i := range us {
33+
results[i] = &user{us[i].Name, us[i].AvatarLink()}
34+
}
35+
36+
ctx.Render.JSON(200, map[string]interface{}{
37+
"ok": true,
38+
"data": results,
39+
})
40+
}

templates/base/footer.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
</p>
1111
</div>
1212

13-
<div class="col-md-1" style="margin: -5px;">
13+
<div class="col-md-2" style="margin: -5px;">
1414
<a target="_blank" href="https://github.com/gogits/gogs"><i class="fa fa-github fa-2x"></i></a>
1515
</div>
1616

17-
<div class="col-md-5">
18-
<p class="desc"></p>
17+
<div class="col-md-4">
18+
<p class="desc"><a href="http://gogits.org">Official Website</a></p>
1919
</div>
2020
</div>
2121
</div>

web.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ func runWeb(*cli.Context) {
7575
m.Get("/help", routers.Help)
7676

7777
m.Group("/api/v1", func(r martini.Router) {
78+
// Miscellaneous.
7879
r.Post("/markdown", v1.Markdown)
80+
81+
// Users.
82+
r.Get("/users/search", v1.SearchUser)
7983
})
8084

8185
avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")

0 commit comments

Comments
 (0)