Skip to content

Commit

Permalink
added Limit method to Engine struct; use engine variable when provide…
Browse files Browse the repository at this point in the history
…d; fixed gitignore
  • Loading branch information
hilariocoelho committed Dec 21, 2019
1 parent b6ed97d commit d0f7898
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
5 changes: 3 additions & 2 deletions models/issue_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ func FindIssueReactions(issue *Issue, listOptions ListOptions) (ReactionList, er

func findReactions(e Engine, opts FindReactionsOptions) ([]*Reaction, error) {
var reactions []*Reaction
sess := e.Where(opts.toConds())
sess := e
if opts.Page != 0 {
sess = opts.setSessionPagination(sess)
sess = opts.setEnginePagination(e)
}

return reactions, sess.
Where(opts.toConds()).
In("reaction.`type`", setting.UI.Reactions).
Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id").
Find(&reactions)
Expand Down
16 changes: 7 additions & 9 deletions models/issue_tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,17 @@ func (opts *FindTrackedTimesOptions) ToSession(e Engine) *xorm.Session {
sess = e.Join("INNER", "issue", "issue.id = tracked_time.issue_id")
}

if opts.Page != 0 {
if sess == nil {
sess = opts.getPaginatedSession()
} else {
sess = opts.setSessionPagination(sess)
}
if sess == nil {
sess = e.Where(opts.ToCond())
} else {
sess = sess.Where(opts.ToCond())
}

if sess == nil {
return e.Where(opts.ToCond())
if opts.Page != 0 {
sess = opts.setSessionPagination(sess)
}

return sess.Where(opts.ToCond())
return sess
}

// GetTrackedTimes returns all tracked times that fit to the given options.
Expand Down
6 changes: 6 additions & 0 deletions models/list_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func (opts ListOptions) setSessionPagination(sess *xorm.Session) *xorm.Session {
return sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}

func (opts ListOptions) setEnginePagination(e Engine) Engine {
opts.setDefaultValues()

return e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}

func (opts ListOptions) setDefaultValues() {
if opts.PageSize <= 0 || opts.PageSize > setting.UI.ExplorePagingNum {
opts.PageSize = setting.UI.ExplorePagingNum
Expand Down
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Engine interface {
SQL(interface{}, ...interface{}) *xorm.Session
Where(interface{}, ...interface{}) *xorm.Session
Asc(colNames ...string) *xorm.Session
Limit(limit int, start ...int) *xorm.Session
}

var (
Expand Down
16 changes: 6 additions & 10 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,17 +760,13 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
}

func getUserTeams(e Engine, userID int64, listOptions ListOptions) (teams []*Team, err error) {
if listOptions.Page == 0 {
return teams, e.
Join("INNER", "team_user", "team_user.team_id = team.id").
Where("team_user.uid=?", userID).
Find(&teams)
}

return teams, listOptions.getPaginatedSession().
sess := e.
Join("INNER", "team_user", "team_user.team_id = team.id").
Where("team_user.uid=?", userID).
Find(&teams)
Where("team_user.uid=?", userID)
if listOptions.Page != 0 {
sess = listOptions.setSessionPagination(sess)
}
return teams, sess.Find(&teams)
}

func getUserOrgTeams(e Engine, orgID, userID int64) (teams []*Team, err error) {
Expand Down
3 changes: 1 addition & 2 deletions models/repo_collaboration.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func (repo *Repository) getCollaborations(e Engine, listOptions ListOptions) ([]
if listOptions.Page == 0 {
return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
}

return collaborations, listOptions.getPaginatedSession().Find(&collaborations, &Collaboration{RepoID: repo.ID})
return collaborations, listOptions.setEnginePagination(e).Find(&collaborations, &Collaboration{RepoID: repo.ID})
}

// Collaborator represents a user with collaboration details.
Expand Down
3 changes: 0 additions & 3 deletions options/gitignore/Matlab
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ codegen/
# Simulink autosave extension
*.autosave

# Simulink cache files
*.slxc

# Octave session info
octave-workspace

0 comments on commit d0f7898

Please sign in to comment.