Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ListParams): address zero-indexing error in ListParams #101

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/handlers/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (h *HistoryHandlers) LogHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *HistoryHandlers) logHandler(w http.ResponseWriter, r *http.Request) {
p := util.PageFromRequest(r)
params := &core.LogParams{
ListParams: core.ListParamsFromRequest(r),
Path: datastore.NewKey(r.URL.Path[len("/history/"):]),
Expand Down
41 changes: 21 additions & 20 deletions core/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,40 @@ type GetParams struct {
Hash string
}

// ListParams is the general input for any sort of Paginated Request
// ListParams define limits & offsets, not pages & page sizes.
// TODO - rename this to PageParams.
type ListParams struct {
OrderBy string
Limit int
Offset int
}

// ListParamsFromRequest extracts ListParams from an http.Request pointer
func ListParamsFromRequest(r *http.Request) ListParams {
lp := &ListParams{}
var pageIndex int
if i, err := util.ReqParamInt("pageSize", r); err == nil {
lp.Limit = i
// NewListParams creates a ListParams from page & pagesize, pages are 1-indexed
// (the first element is 1, not 0), NewListParams performs the conversion
func NewListParams(orderBy string, page, pageSize int) ListParams {
if page < 1 {
page = 1
}
if i, err := util.ReqParamInt("page", r); err == nil {
pageIndex = i
if pageSize <= 0 {
pageSize = DEFAULT_PAGE_SIZE
}
if pageIndex < 0 {
pageIndex = 0
return ListParams{
Limit: pageSize,
Offset: (page - 1) * pageSize,
}
lp.Clean()
lp.Offset = pageIndex * lp.Limit
// lp.OrderBy defaults to empty string
return *lp

}

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

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