Browse Source

models/repo.go RenderRepoSearch

returns all accessible repositories based on logged in userid
pull/4289/head
raricha 8 years ago
parent
commit
2ee8852a4b
  1. 19
      models/repo.go
  2. 12
      routers/home.go

19
models/repo.go

@ -1072,14 +1072,14 @@ func CountUserRepositories(userID int64, private bool) int64 {
return countRepositories(userID, private) return countRepositories(userID, private)
} }
func Repositories(page, pageSize int) (_ []*Repository, err error) { func Repositories(page, pageSize int, _ int64) (_ []*Repository, err error) {
repos := make([]*Repository, 0, pageSize) repos := make([]*Repository, 0, pageSize)
return repos, x.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&repos) return repos, x.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&repos)
} }
// RepositoriesWithUsers returns number of repos in given page. // RepositoriesWithUsers returns number of repos in given page.
func RepositoriesWithUsers(page, pageSize int) (_ []*Repository, err error) { func RepositoriesWithUsers(page, pageSize int) (_ []*Repository, err error) {
repos, err := Repositories(page, pageSize) repos, err := Repositories(page, pageSize, -1)
if err != nil { if err != nil {
return nil, fmt.Errorf("Repositories: %v", err) return nil, fmt.Errorf("Repositories: %v", err)
} }
@ -1553,9 +1553,18 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
} }
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err error) { func GetRecentUpdatedRepositories(page, pageSize int, userID int64) (repos []*Repository, err error) {
return repos, x.Limit(pageSize, (page-1)*pageSize). repos, _, err = SearchRepositoryByName(&SearchRepoOptions{
Where("is_private=?", false).Limit(pageSize).Desc("updated_unix").Find(&repos) Keyword: "%",
UserID: userID,
OrderBy: "updated_unix DESC",
Private: false,
Page: page,
PageSize: pageSize,
})
return repos, err
//return repos, x.Limit(pageSize, (page-1)*pageSize).
// Where("is_private=?", false).Limit(pageSize).Desc("updated_unix").Find(&repos)
} }
func getRepositoryCount(e Engine, u *User) (int64, error) { func getRepositoryCount(e Engine, u *User) (int64, error) {

12
routers/home.go

@ -47,7 +47,7 @@ func Home(ctx *context.Context) {
type RepoSearchOptions struct { type RepoSearchOptions struct {
Counter func(bool) int64 Counter func(bool) int64
Ranger func(int, int) ([]*models.Repository, error) Ranger func(int, int, int64) ([]*models.Repository, error)
Private bool Private bool
PageSize int PageSize int
OrderBy string OrderBy string
@ -59,6 +59,10 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
if page <= 0 { if page <= 0 {
page = 1 page = 1
} }
var ctxUserID int64
if ctx.IsSigned {
ctxUserID = ctx.User.ID
}
var ( var (
repos []*models.Repository repos []*models.Repository
@ -68,17 +72,13 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
keyword := ctx.Query("q") keyword := ctx.Query("q")
if len(keyword) == 0 { if len(keyword) == 0 {
repos, err = opts.Ranger(page, opts.PageSize) repos, err = opts.Ranger(page, opts.PageSize, ctxUserID)
if err != nil { if err != nil {
ctx.Handle(500, "opts.Ranger", err) ctx.Handle(500, "opts.Ranger", err)
return return
} }
count = opts.Counter(opts.Private) count = opts.Counter(opts.Private)
} else { } else {
var ctxUserID int64
if ctx.IsSigned {
ctxUserID = ctx.User.ID
}
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword, Keyword: keyword,
UserID: ctxUserID, UserID: ctxUserID,

Loading…
Cancel
Save