diff options
author | Bo-Yi Wu <appleboy.tw@gmail.com> | 2017-02-14 15:28:22 +0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-14 15:28:22 +0800 |
commit | 23aba523b551d20ee2bbc694ae630989bcdeaccd (patch) | |
tree | 61f0aa0e9a50dcbc924b9d4cacdf3e90936ab7a4 /models | |
parent | 7eb8daffa3181f6bae7aac95d536c68c6bed0e33 (diff) | |
download | gitea-23aba523b551d20ee2bbc694ae630989bcdeaccd.tar.gz gitea-23aba523b551d20ee2bbc694ae630989bcdeaccd.zip |
feat: support search bar on star tab of user profile. (#917)
* feat: support search bar on star tab of user profile.
* fix: update testing.
* fix: Using loadAttributes
* fix: remove empty line.
* remove LOWER
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Diffstat (limited to 'models')
-rw-r--r-- | models/repo.go | 30 | ||||
-rw-r--r-- | models/star.go | 4 | ||||
-rw-r--r-- | models/star_test.go | 4 |
3 files changed, 29 insertions, 9 deletions
diff --git a/models/repo.go b/models/repo.go index 7a6eaea215..d83c5fd6ab 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1778,13 +1778,15 @@ type SearchRepoOptions struct { Searcher *User //ID of the person who's seeking OrderBy string Private bool // Include private repositories in results + Starred bool Page int PageSize int // Can be smaller than or equal to setting.ExplorePagingNum } // SearchRepositoryByName takes keyword and part of repository name to search, // it returns results in given range and number of total results. -func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int64, _ error) { +func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) { + var sess *xorm.Session if len(opts.Keyword) == 0 { return repos, 0, nil } @@ -1796,9 +1798,17 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int repos = make([]*Repository, 0, opts.PageSize) + if opts.Starred && opts.OwnerID > 0 { + sess = x. + Join("INNER", "star", "star.repo_id = repository.id"). + Where("star.uid = ?", opts.OwnerID). + And("lower_name LIKE ?", "%"+opts.Keyword+"%") + } else { + sess = x.Where("lower_name LIKE ?", "%"+opts.Keyword+"%") + } + // Append conditions - sess := x.Where("LOWER(lower_name) LIKE ?", "%"+opts.Keyword+"%") - if opts.OwnerID > 0 { + if !opts.Starred && opts.OwnerID > 0 { sess.And("owner_id = ?", opts.OwnerID) } if !opts.Private { @@ -1831,10 +1841,20 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int return nil, 0, fmt.Errorf("Count: %v", err) } - return repos, count, sess. + if err = sess. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). OrderBy(opts.OrderBy). - Find(&repos) + Find(&repos); err != nil { + return nil, 0, fmt.Errorf("Repo: %v", err) + } + + if opts.Starred { + if err = repos.loadAttributes(x); err != nil { + return nil, 0, fmt.Errorf("LoadAttributes: %v", err) + } + } + + return repos, count, nil } // DeleteRepositoryArchives deletes all repositories' archives. diff --git a/models/star.go b/models/star.go index 39f6eac1f0..7525c8a6fa 100644 --- a/models/star.go +++ b/models/star.go @@ -73,12 +73,12 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) { // GetStarredRepos returns the repos the user starred. func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos []*Repository, err error) { if len(orderBy) == 0 { - orderBy = "star.id" + orderBy = "updated_unix DESC" } sess := x. Join("INNER", "star", "star.repo_id = repository.id"). Where("star.uid = ?", u.ID). - Desc(orderBy) + OrderBy(orderBy) if !private { sess = sess.And("is_private = ?", false) diff --git a/models/star_test.go b/models/star_test.go index d6a83199b7..714f5df5cd 100644 --- a/models/star_test.go +++ b/models/star_test.go @@ -61,8 +61,8 @@ func TestUser_GetStarredRepos(t *testing.T) { starred, err = user.GetStarredRepos(true, 1, 10, "") assert.NoError(t, err) assert.Len(t, starred, 2) - assert.Equal(t, int64(4), starred[0].ID) - assert.Equal(t, int64(2), starred[1].ID) + assert.Equal(t, int64(2), starred[0].ID) + assert.Equal(t, int64(4), starred[1].ID) } func TestUser_GetStarredRepos2(t *testing.T) { |