Skip to content

Commit

Permalink
api: refactor pagination (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Jun 19, 2024
1 parent 7926087 commit 1d286ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
46 changes: 42 additions & 4 deletions api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@ const (
MaxOffchainFileSize = 1024 * 1024 * 1 // 1MB
)

const (
ParamPage = "page"
)

func (a *API) enableElectionHandlers() error {
if err := a.Endpoint.RegisterMethod(
"/elections/page/{page}",
"/elections/page/{page}", // legacy endpoint
"GET",
apirest.MethodAccessTypePublic,
a.electionFullListByPageHandler,
); err != nil {
return err
}
if err := a.Endpoint.RegisterMethod(
"/elections",
"GET",
apirest.MethodAccessTypePublic,
a.electionFullListHandler,
Expand Down Expand Up @@ -124,17 +136,17 @@ func (a *API) enableElectionHandlers() error {
return nil
}

// electionFullListHandler
// electionFullListByPageHandler
//
// @Summary List elections
// @Summary List elections (legacy params design)
// @Description Get a list of elections summaries.
// @Tags Elections
// @Accept json
// @Produce json
// @Param page path number true "Page "
// @Success 200 {object} ElectionSummary
// @Router /elections/page/{page} [get]
func (a *API) electionFullListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
func (a *API) electionFullListByPageHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
page := 0
if ctx.URLParam("page") != "" {
var err error
Expand All @@ -143,6 +155,32 @@ func (a *API) electionFullListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCo
return ErrCantParsePageNumber.With(ctx.URLParam("page"))
}
}
return a.electionFullListByPage(ctx, page)
}

// electionFullListHandler
//
// @Summary List elections
// @Description Get a list of elections summaries.
// @Tags Elections
// @Accept json
// @Produce json
// @Param page query number false "Page "
// @Success 200 {object} ElectionSummary
// @Router /elections [get]
func (a *API) electionFullListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
page := 0
if p := ctx.QueryParam(ParamPage); p != "" {
var err error
page, err = strconv.Atoi(p)
if err != nil {
return ErrCantParsePageNumber.With(p)
}
}
return a.electionFullListByPage(ctx, page)
}

func (a *API) electionFullListByPage(ctx *httprouter.HTTPContext, page int) error {
elections, err := a.indexer.ProcessList(nil, page*MaxPageSize, MaxPageSize, "", 0, 0, "", false)
if err != nil {
return ErrCantFetchElectionList.WithErr(err)
Expand Down
6 changes: 6 additions & 0 deletions httprouter/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (h *HTTPContext) URLParam(key string) string {
return chi.URLParam(h.Request, key)
}

// QueryParam is a wrapper around go-chi to get the value of a query string parameter (like "?key=value").
// If key is not present, returns the empty string.
func (h *HTTPContext) QueryParam(key string) string {
return h.Request.URL.Query().Get(key)
}

// Send replies the request with the provided message.
func (h *HTTPContext) Send(msg []byte, httpStatusCode int) error {
defer func() {
Expand Down

0 comments on commit 1d286ca

Please sign in to comment.