Skip to content

Commit

Permalink
refactor(ListParams): Added a Clean method to ListParams
Browse files Browse the repository at this point in the history
Merge pull request #100 from qri-io/more_pagination
  • Loading branch information
osterbit authored Nov 7, 2017
2 parents 969c95b + 926c7a7 commit 2959f18
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions core/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,31 @@ type ListParams struct {

// ListParamsFromRequest extracts ListParams from an http.Request pointer
func ListParamsFromRequest(r *http.Request) ListParams {
var lp ListParams
lp := &ListParams{}
var pageIndex int
if i, err := util.ReqParamInt("pageSize", r); err == nil {
lp.Limit = i
}
if lp.Limit <= 0 {
lp.Limit = DEFAULT_PAGE_SIZE
}
if i, err := util.ReqParamInt("page", r); err == nil {
pageIndex = i
}
if pageIndex < 0 {
pageIndex = 0
}
lp.Clean()
lp.Offset = pageIndex * lp.Limit
// lp.OrderBy defaults to empty string
return lp
return *lp

}

func (lp *ListParams) Clean() {
if lp.Limit <= 0 {
lp.Limit = DEFAULT_PAGE_SIZE
}
if lp.Offset < 0 {
lp.Offset = 0
}
}

// Page converts a ListParams struct to a util.Page struct
Expand Down

0 comments on commit 2959f18

Please sign in to comment.