diff --git a/api/handlers/history.go b/api/handlers/history.go index f8fab21cb..31bced472 100644 --- a/api/handlers/history.go +++ b/api/handlers/history.go @@ -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/"):]), diff --git a/core/params.go b/core/params.go index 6bd3056fd..43429916a 100644 --- a/core/params.go +++ b/core/params.go @@ -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