Skip to content

Commit

Permalink
fix: updated pagination to rely on core struct
Browse files Browse the repository at this point in the history
- first pass complete
  - Limit and Offset ListParams obtained by api/handlers' List functions/methods using `ListParamsFromRequest()`
  - `params_test.go` added, handles a few cases of invalid limits and offsets
- partially complete
- added support for accepting an 'orderBy' request query param just now but have not yet updated core/params_test and api/handlers functions to use this value yet
  • Loading branch information
Thomas Osterbind committed Nov 6, 2017
1 parent 2eadc89 commit cdba451
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 368 deletions.
21 changes: 12 additions & 9 deletions api/handlers/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,22 @@ func (h *DatasetHandlers) ZipDatasetHandler(w http.ResponseWriter, r *http.Reque
}

func (h *DatasetHandlers) listDatasetsHandler(w http.ResponseWriter, r *http.Request) {
p := util.PageFromRequest(r)
lp := core.ListParamsFromRequest(r)
res := []*repo.DatasetRef{}
args := &core.ListParams{
Limit: p.Limit(),
Offset: p.Offset(),
OrderBy: "created",
Limit: lp.Limit,
Offset: lp.Offset,
OrderBy: "created", //TODO: should there be a global default orderby?
}
if err := h.List(args, &res); err != nil {
h.log.Infof("error listing datasets: %s", err.Error())
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}

if err := util.WritePageResponse(w, res, r, p); err != nil {
// TODO: need to update util.WritePageResponse to take a
// core.ListParams rather than a util.Page struct
// for time being I added an empty util.Page struct
if err := util.WritePageResponse(w, res, r, util.Page{}); err != nil {
h.log.Infof("error list datasests response: %s", err.Error())
}
}
Expand Down Expand Up @@ -225,7 +227,8 @@ func (h *DatasetHandlers) deleteDatasetHandler(w http.ResponseWriter, r *http.Re
}

func (h *DatasetHandlers) getStructuredDataHandler(w http.ResponseWriter, r *http.Request) {
page := util.PageFromRequest(r)
//page := util.PageFromRequest(r)
listParams := core.ListParamsFromRequest(r)

all, err := util.ReqParamBool("all", r)
if err != nil {
Expand All @@ -241,8 +244,8 @@ func (h *DatasetHandlers) getStructuredDataHandler(w http.ResponseWriter, r *htt
Format: dataset.JsonDataFormat,
Path: datastore.NewKey(r.URL.Path[len("/data"):]),
Objects: objectRows,
Limit: page.Limit(),
Offset: page.Offset(),
Limit: listParams.Limit,
Offset: listParams.Offset,
All: all,
}
data := &core.StructuredData{}
Expand Down
26 changes: 16 additions & 10 deletions api/handlers/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,30 @@ func (d *PeerHandlers) ConnectionsHandler(w http.ResponseWriter, r *http.Request
}

func (h *PeerHandlers) listPeersHandler(w http.ResponseWriter, r *http.Request) {
p := util.PageFromRequest(r)
//p := util.PageFromRequest(r)
lp := core.ListParamsFromRequest(r)
res := []*profile.Profile{}
args := &core.ListParams{
Limit: p.Limit(),
Offset: p.Offset(),
OrderBy: "created",
Limit: lp.Limit,
Offset: lp.Offset,
OrderBy: "created", //TODO: should there be a global default for OrderBy?
}
if err := h.List(args, &res); err != nil {
h.log.Infof("list peers: %s", err.Error())
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
util.WritePageResponse(w, res, r, p)
// TODO: need to update util.WritePageResponse to take a
// core.ListParams rather than a util.Page struct
// for time being I added an empty util.Page struct
util.WritePageResponse(w, res, r, util.Page{})
}

func (h *PeerHandlers) listConnectionsHandler(w http.ResponseWriter, r *http.Request) {
limit := 0
//limit := 0
listParams := core.ListParamsFromRequest(r)
peers := []string{}
if err := h.ConnectedPeers(&limit, &peers); err != nil {
if err := h.ConnectedPeers(&listParams.Limit, &peers); err != nil {
h.log.Infof("error showing connected peers: %s", err.Error())
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
Expand Down Expand Up @@ -142,11 +147,12 @@ func (h *PeerHandlers) getPeerHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *PeerHandlers) peerNamespaceHandler(w http.ResponseWriter, r *http.Request) {
page := util.PageFromRequest(r)
//page := util.PageFromRequest(r)
listParams := core.ListParamsFromRequest(r)
args := &core.NamespaceParams{
PeerId: r.URL.Path[len("/peernamespace/"):],
Limit: page.Limit(),
Offset: page.Offset(),
Limit: listParams.Limit,
Offset: listParams.Offset,
}
res := []*repo.DatasetRef{}
if err := h.GetNamespace(args, &res); err != nil {
Expand Down
9 changes: 5 additions & 4 deletions api/handlers/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ func (d *QueryHandlers) ListHandler(w http.ResponseWriter, r *http.Request) {
// }

func (h *QueryHandlers) listQueriesHandler(w http.ResponseWriter, r *http.Request) {
p := util.PageFromRequest(r)
//p := util.PageFromRequest(r)
lp := core.ListParamsFromRequest(r)
res := []*repo.DatasetRef{}
args := &core.ListParams{
Limit: p.Limit(),
Offset: p.Offset(),
OrderBy: "created",
Limit: lp.Limit,
Offset: lp.Offset,
OrderBy: "created", //TODO: should there be a global default for OrderBy?
}
err := h.List(args, &res)
if err != nil {
Expand Down
171 changes: 0 additions & 171 deletions core/c.out

This file was deleted.

Loading

0 comments on commit cdba451

Please sign in to comment.