mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-21 08:31:27 -05:00
refactor: small optimize for sql query (#940)
* refactor: small optimize for sql query * fix: get owner name if Searcher is not nil or user star page.
This commit is contained in:
parent
837d346090
commit
9d2b830275
3 changed files with 30 additions and 32 deletions
|
@ -1184,25 +1184,23 @@ func CountUserRepositories(userID int64, private bool) int64 {
|
|||
}
|
||||
|
||||
// Repositories returns all repositories
|
||||
func Repositories(opts *SearchRepoOptions) (_ []*Repository, err error) {
|
||||
func Repositories(opts *SearchRepoOptions) (_ RepositoryList, err error) {
|
||||
if len(opts.OrderBy) == 0 {
|
||||
opts.OrderBy = "id ASC"
|
||||
}
|
||||
|
||||
repos := make([]*Repository, 0, opts.PageSize)
|
||||
return repos, x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(opts.OrderBy).Find(&repos)
|
||||
}
|
||||
repos := make(RepositoryList, 0, opts.PageSize)
|
||||
|
||||
// RepositoriesWithUsers returns number of repos in given page.
|
||||
func RepositoriesWithUsers(opts *SearchRepoOptions) (_ []*Repository, err error) {
|
||||
repos, err := Repositories(opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Repositories: %v", err)
|
||||
if err = x.
|
||||
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
||||
OrderBy(opts.OrderBy).
|
||||
Find(&repos); err != nil {
|
||||
return nil, fmt.Errorf("Repo: %v", err)
|
||||
}
|
||||
|
||||
for i := range repos {
|
||||
if err = repos[i].GetOwner(); err != nil {
|
||||
return nil, err
|
||||
if opts.Searcher != nil || opts.Starred {
|
||||
if err = repos.loadAttributes(x); err != nil {
|
||||
return nil, fmt.Errorf("LoadAttributes: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1716,7 +1714,7 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
|
|||
}
|
||||
|
||||
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
|
||||
func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, err error) {
|
||||
func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, err error) {
|
||||
if len(opts.OrderBy) == 0 {
|
||||
opts.OrderBy = "updated_unix DESC"
|
||||
}
|
||||
|
@ -1739,9 +1737,19 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository,
|
|||
}
|
||||
}
|
||||
|
||||
return repos, sess.
|
||||
if err = sess.
|
||||
OrderBy(opts.OrderBy).
|
||||
Find(&repos)
|
||||
Find(&repos); err != nil {
|
||||
return nil, fmt.Errorf("Repo: %v", err)
|
||||
}
|
||||
|
||||
if opts.Searcher != nil || opts.Starred {
|
||||
if err = repos.loadAttributes(x); err != nil {
|
||||
return nil, fmt.Errorf("LoadAttributes: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
func getRepositoryCount(e Engine, u *User) (int64, error) {
|
||||
|
@ -1848,7 +1856,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
|
|||
return nil, 0, fmt.Errorf("Repo: %v", err)
|
||||
}
|
||||
|
||||
if opts.Starred {
|
||||
if opts.Searcher != nil || opts.Starred {
|
||||
if err = repos.loadAttributes(x); err != nil {
|
||||
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ 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) {
|
||||
func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos RepositoryList, err error) {
|
||||
if len(orderBy) == 0 {
|
||||
orderBy = "updated_unix DESC"
|
||||
}
|
||||
|
@ -95,10 +95,8 @@ func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string)
|
|||
return
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
if err = repo.GetOwner(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = repos.loadAttributes(x); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
|
|
|
@ -6,16 +6,15 @@ package routers
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Unknwon/paginater"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/routers/user"
|
||||
|
||||
"github.com/Unknwon/paginater"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -55,7 +54,7 @@ func Home(ctx *context.Context) {
|
|||
// RepoSearchOptions when calling search repositories
|
||||
type RepoSearchOptions struct {
|
||||
Counter func(bool) int64
|
||||
Ranger func(*models.SearchRepoOptions) ([]*models.Repository, error)
|
||||
Ranger func(*models.SearchRepoOptions) (models.RepositoryList, error)
|
||||
Searcher *models.User
|
||||
Private bool
|
||||
PageSize int
|
||||
|
@ -132,13 +131,6 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
|||
ctx.Data["Keyword"] = keyword
|
||||
ctx.Data["Total"] = count
|
||||
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
|
||||
|
||||
for _, repo := range repos {
|
||||
if err = repo.GetOwner(); err != nil {
|
||||
ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.Data["Repos"] = repos
|
||||
|
||||
ctx.HTML(200, opts.TplName)
|
||||
|
|
Loading…
Reference in a new issue