diff options
author | Unknwon <u@gogs.io> | 2016-03-11 15:33:12 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-03-11 15:33:12 -0500 |
commit | 2bf8494332592b3c57f9a12a26b9abd356fb3f15 (patch) | |
tree | e46c61ba30018ebbafcda6b97b36d3ffece3cfb0 /routers | |
parent | df2bdf7ea36ad0729e0326ca799d8896d7f96946 (diff) | |
download | gitea-2bf8494332592b3c57f9a12a26b9abd356fb3f15.tar.gz gitea-2bf8494332592b3c57f9a12a26b9abd356fb3f15.zip |
#13 finish user and repository search
Both are possible on explore and admin panel
Diffstat (limited to 'routers')
-rw-r--r-- | routers/admin/orgs.go | 23 | ||||
-rw-r--r-- | routers/admin/repos.go | 21 | ||||
-rw-r--r-- | routers/admin/users.go | 20 | ||||
-rw-r--r-- | routers/api/v1/admin/orgs.go | 2 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 24 | ||||
-rw-r--r-- | routers/api/v1/user/user.go | 27 | ||||
-rw-r--r-- | routers/home.go | 105 | ||||
-rw-r--r-- | routers/org/org.go | 2 |
8 files changed, 131 insertions, 93 deletions
diff --git a/routers/admin/orgs.go b/routers/admin/orgs.go index 829e78e83c..395243a5f7 100644 --- a/routers/admin/orgs.go +++ b/routers/admin/orgs.go @@ -5,12 +5,11 @@ package admin import ( - "github.com/Unknwon/paginater" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers" ) const ( @@ -22,22 +21,6 @@ func Organizations(ctx *context.Context) { ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminOrganizations"] = true - total := models.CountOrganizations() - page := ctx.QueryInt("page") - if page <= 1 { - page = 1 - } - ctx.Data["Page"] = paginater.New(int(total), setting.AdminOrgPagingNum, page, 5) - - orgs, err := models.Organizations(page, setting.AdminOrgPagingNum) - - if err != nil { - ctx.Handle(500, "Organizations", err) - return - } - - ctx.Data["Orgs"] = orgs - ctx.Data["Total"] = total - - ctx.HTML(200, ORGS) + routers.RenderUserSearch(ctx, models.USER_TYPE_ORGANIZATION, models.CountOrganizations, models.Organizations, + setting.AdminOrgPagingNum, "id ASC", ORGS) } diff --git a/routers/admin/repos.go b/routers/admin/repos.go index 47b6032a65..e78941ab4c 100644 --- a/routers/admin/repos.go +++ b/routers/admin/repos.go @@ -5,13 +5,12 @@ package admin import ( - "github.com/Unknwon/paginater" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers" ) const ( @@ -23,22 +22,8 @@ func Repos(ctx *context.Context) { ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminRepositories"] = true - total := models.CountRepositories() - page := ctx.QueryInt("page") - if page <= 1 { - page = 1 - } - ctx.Data["Page"] = paginater.New(int(total), setting.AdminRepoPagingNum, page, 5) - - repos, err := models.RepositoriesWithUsers(page, setting.AdminRepoPagingNum) - if err != nil { - ctx.Handle(500, "RepositoriesWithUsers", err) - return - } - ctx.Data["Repos"] = repos - - ctx.Data["Total"] = total - ctx.HTML(200, REPOS) + routers.RenderRepoSearch(ctx, models.CountRepositories, models.Repositories, + setting.AdminRepoPagingNum, "id ASC", REPOS) } func DeleteRepo(ctx *context.Context) { diff --git a/routers/admin/users.go b/routers/admin/users.go index 274effc4b6..00028a2fdd 100644 --- a/routers/admin/users.go +++ b/routers/admin/users.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/Unknwon/com" - "github.com/Unknwon/paginater" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -17,6 +16,7 @@ import ( "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/mailer" "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers" ) const ( @@ -30,22 +30,8 @@ func Users(ctx *context.Context) { ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminUsers"] = true - total := models.CountUsers() - page := ctx.QueryInt("page") - if page <= 1 { - page = 1 - } - ctx.Data["Page"] = paginater.New(int(total), setting.AdminUserPagingNum, page, 5) - - users, err := models.Users(page, setting.AdminUserPagingNum) - if err != nil { - ctx.Handle(500, "Users", err) - return - } - ctx.Data["Users"] = users - - ctx.Data["Total"] = total - ctx.HTML(200, USERS) + routers.RenderUserSearch(ctx, models.USER_TYPE_INDIVIDUAL, models.CountUsers, models.Users, + setting.AdminUserPagingNum, "id ASC", USERS) } func NewUser(ctx *context.Context) { diff --git a/routers/api/v1/admin/orgs.go b/routers/api/v1/admin/orgs.go index fbfcd400ae..f212c4f517 100644 --- a/routers/api/v1/admin/orgs.go +++ b/routers/api/v1/admin/orgs.go @@ -27,7 +27,7 @@ func CreateOrg(ctx *context.Context, form api.CreateOrgOption) { Website: form.Website, Location: form.Location, IsActive: true, - Type: models.ORGANIZATION, + Type: models.USER_TYPE_ORGANIZATION, } if err := models.CreateOrganization(org, u); err != nil { if models.IsErrUserAlreadyExist(err) || diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index fc7a9cd442..e3d8368010 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -21,21 +21,21 @@ import ( // https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories func Search(ctx *context.Context) { - opt := models.SearchOption{ - Keyword: path.Base(ctx.Query("q")), - Uid: com.StrTo(ctx.Query("uid")).MustInt64(), - Limit: com.StrTo(ctx.Query("limit")).MustInt(), + opts := &models.SearchRepoOptions{ + Keyword: path.Base(ctx.Query("q")), + OwnerID: com.StrTo(ctx.Query("uid")).MustInt64(), + PageSize: com.StrTo(ctx.Query("limit")).MustInt(), } - if opt.Limit == 0 { - opt.Limit = 10 + if opts.PageSize == 0 { + opts.PageSize = 10 } // Check visibility. - if ctx.IsSigned && opt.Uid > 0 { - if ctx.User.Id == opt.Uid { - opt.Private = true + if ctx.IsSigned && opts.OwnerID > 0 { + if ctx.User.Id == opts.OwnerID { + opts.Private = true } else { - u, err := models.GetUserByID(opt.Uid) + u, err := models.GetUserByID(opts.OwnerID) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, @@ -44,13 +44,13 @@ func Search(ctx *context.Context) { return } if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) { - opt.Private = true + opts.Private = true } // FIXME: how about collaborators? } } - repos, err := models.SearchRepositoryByName(opt) + repos, _, err := models.SearchRepositoryByName(opts) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index d08fd20241..71056894a5 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -15,15 +15,16 @@ import ( // https://github.com/gogits/go-gogs-client/wiki/Users#search-users func Search(ctx *context.Context) { - opt := models.SearchOption{ - Keyword: ctx.Query("q"), - Limit: com.StrTo(ctx.Query("limit")).MustInt(), + opts := &models.SearchUserOptions{ + Keyword: ctx.Query("q"), + Type: models.USER_TYPE_INDIVIDUAL, + PageSize: com.StrTo(ctx.Query("limit")).MustInt(), } - if opt.Limit == 0 { - opt.Limit = 10 + if opts.PageSize == 0 { + opts.PageSize = 10 } - us, err := models.SearchUserByName(opt) + users, _, err := models.SearchUserByName(opts) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, @@ -32,16 +33,16 @@ func Search(ctx *context.Context) { return } - results := make([]*api.User, len(us)) - for i := range us { + results := make([]*api.User, len(users)) + for i := range users { results[i] = &api.User{ - ID: us[i].Id, - UserName: us[i].Name, - AvatarUrl: us[i].AvatarLink(), - FullName: us[i].FullName, + ID: users[i].Id, + UserName: users[i].Name, + AvatarUrl: users[i].AvatarLink(), + FullName: users[i].FullName, } if ctx.IsSigned { - results[i].Email = us[i].Email + results[i].Email = users[i].Email } } diff --git a/routers/home.go b/routers/home.go index 298b4f409b..0836dd5060 100644 --- a/routers/home.go +++ b/routers/home.go @@ -19,6 +19,7 @@ import ( const ( HOME base.TplName = "home" EXPLORE_REPOS base.TplName = "explore/repos" + EXPLORE_USERS base.TplName = "explore/users" ) func Home(ctx *context.Context) { @@ -43,23 +44,44 @@ func Home(ctx *context.Context) { ctx.HTML(200, HOME) } -func Explore(ctx *context.Context) { - ctx.Data["Title"] = ctx.Tr("explore") - ctx.Data["PageIsExplore"] = true - ctx.Data["PageIsExploreRepositories"] = true - +func RenderRepoSearch(ctx *context.Context, + counter func() int64, ranger func(int, int) ([]*models.Repository, error), + pagingNum int, orderBy string, tplName base.TplName) { page := ctx.QueryInt("page") if page <= 1 { page = 1 } - ctx.Data["Page"] = paginater.New(int(models.CountPublicRepositories()), setting.ExplorePagingNum, page, 5) + var ( + repos []*models.Repository + count int64 + err error + ) - repos, err := models.GetRecentUpdatedRepositories(page) - if err != nil { - ctx.Handle(500, "GetRecentUpdatedRepositories", err) - return + keyword := ctx.Query("q") + if len(keyword) == 0 { + repos, err = ranger(page, pagingNum) + if err != nil { + ctx.Handle(500, "ranger", err) + return + } + count = counter() + } else { + repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ + Keyword: keyword, + OrderBy: orderBy, + Page: page, + PageSize: pagingNum, + }) + if err != nil { + ctx.Handle(500, "SearchRepositoryByName", err) + return + } } + ctx.Data["Keyword"] = keyword + ctx.Data["Total"] = count + ctx.Data["Page"] = paginater.New(int(count), pagingNum, page, 5) + for _, repo := range repos { if err = repo.GetOwner(); err != nil { ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err)) @@ -68,7 +90,68 @@ func Explore(ctx *context.Context) { } ctx.Data["Repos"] = repos - ctx.HTML(200, EXPLORE_REPOS) + ctx.HTML(200, tplName) +} + +func ExploreRepos(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("explore") + ctx.Data["PageIsExplore"] = true + ctx.Data["PageIsExploreRepositories"] = true + + RenderRepoSearch(ctx, models.CountPublicRepositories, models.GetRecentUpdatedRepositories, + setting.ExplorePagingNum, "updated_unix DESC", EXPLORE_REPOS) +} + +func RenderUserSearch(ctx *context.Context, userType models.UserType, + counter func() int64, ranger func(int, int) ([]*models.User, error), + pagingNum int, orderBy string, tplName base.TplName) { + page := ctx.QueryInt("page") + if page <= 1 { + page = 1 + } + + var ( + users []*models.User + count int64 + err error + ) + + keyword := ctx.Query("q") + if len(keyword) == 0 { + users, err = ranger(page, pagingNum) + if err != nil { + ctx.Handle(500, "ranger", err) + return + } + count = counter() + } else { + users, count, err = models.SearchUserByName(&models.SearchUserOptions{ + Keyword: keyword, + Type: userType, + OrderBy: orderBy, + Page: page, + PageSize: pagingNum, + }) + if err != nil { + ctx.Handle(500, "SearchUserByName", err) + return + } + } + ctx.Data["Keyword"] = keyword + ctx.Data["Total"] = count + ctx.Data["Page"] = paginater.New(int(count), pagingNum, page, 5) + ctx.Data["Users"] = users + + ctx.HTML(200, tplName) +} + +func ExploreUsers(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("explore") + ctx.Data["PageIsExplore"] = true + ctx.Data["PageIsExploreUsers"] = true + + RenderUserSearch(ctx, models.USER_TYPE_INDIVIDUAL, models.CountUsers, models.Users, + setting.ExplorePagingNum, "updated_unix DESC", EXPLORE_USERS) } func NotFound(ctx *context.Context) { diff --git a/routers/org/org.go b/routers/org/org.go index 1257c108d0..1a7800d955 100644 --- a/routers/org/org.go +++ b/routers/org/org.go @@ -33,7 +33,7 @@ func CreatePost(ctx *context.Context, form auth.CreateOrgForm) { org := &models.User{ Name: form.OrgName, IsActive: true, - Type: models.ORGANIZATION, + Type: models.USER_TYPE_ORGANIZATION, } if err := models.CreateOrganization(org, ctx.User); err != nil { |