Skip to content

feat: Add search bar on user profile page. #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1577,10 +1577,14 @@ func GetRepositoryByID(id int64) (*Repository, error) {
}

// GetUserRepositories returns a list of repositories of given user.
func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Repository, error) {
func GetUserRepositories(userID int64, private bool, page, pageSize int, orderBy string) ([]*Repository, error) {
if len(orderBy) == 0 {
orderBy = "updated_unix DESC"
}

sess := x.
Where("owner_id = ?", userID).
Desc("updated_unix")
OrderBy(orderBy)
if !private {
sess.And("is_private=?", false)
}
Expand Down
2 changes: 1 addition & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func (u *User) GetOrganizationCount() (int64, error) {

// GetRepositories returns repositories that user owns, including private repositories.
func (u *User) GetRepositories(page, pageSize int) (err error) {
u.Repos, err = GetUserRepositories(u.ID, true, page, pageSize)
u.Repos, err = GetUserRepositories(u.ID, true, page, pageSize, "")
return err
}

Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func Search(ctx *context.APIContext) {
// ListMyRepos list all my repositories
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
func ListMyRepos(ctx *context.APIContext) {
ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos)
ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos, "")
if err != nil {
ctx.Error(500, "GetRepositories", err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func showOrgProfile(ctx *context.Context) {
ctx.Data["Repos"] = repos
} else {
showPrivate := ctx.IsSigned && ctx.User.IsAdmin
repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum)
repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum, "")
if err != nil {
ctx.Handle(500, "GetRepositories", err)
return
Expand Down
64 changes: 59 additions & 5 deletions routers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,66 @@ func Profile(ctx *context.Context) {
page = 1
}

ctx.Data["Repos"], err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum)
if err != nil {
ctx.Handle(500, "GetRepositories", err)
return
var (
repos []*models.Repository
count int64
err error
orderBy string
)
switch ctx.Query("sort") {
case "newest":
orderBy = "created_unix DESC"
case "oldest":
orderBy = "created_unix ASC"
case "recentupdate":
orderBy = "updated_unix DESC"
case "leastupdate":
orderBy = "updated_unix ASC"
case "reversealphabetically":
orderBy = "name DESC"
case "alphabetically":
orderBy = "name ASC"
default:
orderBy = "updated_unix DESC"
}

keyword := ctx.Query("q")
if len(keyword) == 0 {
repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
if err != nil {
ctx.Handle(500, "GetRepositories", err)
return
}
ctx.Data["Repos"] = repos
ctx.Data["Page"] = paginater.New(ctxUser.NumRepos, setting.UI.User.RepoPagingNum, page, 5)
ctx.Data["Total"] = ctxUser.NumRepos
} else {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
OwnerID: ctxUser.ID,
OrderBy: orderBy,
Private: ctx.IsSigned && ctx.User.ID == ctxUser.ID,
Page: page,
PageSize: setting.UI.User.RepoPagingNum,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
return
}

ctx.Data["Repos"] = repos
ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the hard-coded 5 here ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ go doc github.com/Unknwon/paginater.New
func New(total, pagingNum, current, numPages int) *Paginater
    New initialize a new pagination calculation and returns a Paginater as
    result.

What's the meaning of numPages ? How's that different from total/pagingNum ?
@unknwon ? Maybe the documentation on that could be improved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the README.md of the paginater:

        // Arguments:
        // - Total number of rows
        // - Number of rows in one page
        // - Current page number 
        // - Number of page links
        p := paginater.New(45, 10, 3, 3)

I still don't understand what Number of page links means though.
Is it the expected number of pages ? As that could be computed as ceil(Total number of rows / Number of rows in one page), right ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think I got it: it's the number of page links you want to be visible in a template. Sounds a bit wrong to mix output control and flow control to me, but this is not the place to discuss this. Still maybe it's good to have a configuration in settings about the number of pager links ? As I see that hard-coded 5 in many places even only in this file...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@strk I think this may be a bug. I change the Number of page links to 2. It still show five items in a template.

ctx.Data["Total"] = count
}
ctx.Data["Page"] = paginater.New(ctxUser.NumRepos, setting.UI.User.RepoPagingNum, page, 5)

// set default sort value.
if ctx.Query("sort") == "" {
ctx.Data["SortType"] = "recentupdate"
} else {
ctx.Data["SortType"] = ctx.Query("sort")
}

ctx.Data["Keyword"] = keyword
}

ctx.HTML(200, tplProfile)
Expand Down
1 change: 1 addition & 0 deletions templates/user/profile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
{{template "explore/repo_list" .}}
</div>
{{else}}
{{template "explore/search" .}}
{{template "explore/repo_list" .}}
{{template "base/paginate" .}}
{{end}}
Expand Down