Skip to content

Commit

Permalink
feat(endpoint): support pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
crazybolillo committed Aug 18, 2024
1 parent 838c4cc commit af4659b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
15 changes: 12 additions & 3 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ definitions:
items:
$ref: '#/definitions/handler.listEndpointEntry'
type: array
retrieved:
type: integer
total:
type: integer
type: object
host: localhost:8080
info:
Expand Down Expand Up @@ -113,10 +117,15 @@ paths:
/endpoints:
get:
parameters:
- default: 15
description: Limit the amount of endpoints returned
- default: 0
description: Zero based page to fetch
in: query
name: page
type: integer
- default: 10
description: Max amount of results to be returned
in: query
name: limit
name: pageSize
type: integer
produces:
- application/json
Expand Down
33 changes: 22 additions & 11 deletions internal/handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"github.com/crazybolillo/eryth/internal/db"
"github.com/crazybolillo/eryth/internal/query"
"github.com/crazybolillo/eryth/internal/sqlc"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -41,6 +42,8 @@ type listEndpointEntry struct {
}

type listEndpointsResponse struct {
Total int64 `json:"total"`
Retrieved int `json:"retrieved"`
Endpoints []listEndpointEntry `json:"endpoints"`
}

Expand Down Expand Up @@ -148,27 +151,27 @@ func (e *Endpoint) get(w http.ResponseWriter, r *http.Request) {
}

// @Summary List existing endpoints.
// @Param limit query int false "Limit the amount of endpoints returned" default(15)
// @Param page query int false "Zero based page to fetch" default(0)
// @Param pageSize query int false "Max amount of results to be returned" default(10)
// @Produce json
// @Success 200 {object} listEndpointsResponse
// @Failure 400
// @Failure 500
// @Tags endpoints
// @Router /endpoints [get]
func (e *Endpoint) list(w http.ResponseWriter, r *http.Request) {
qlim := r.URL.Query().Get("limit")
limit := 15
if qlim != "" {
conv, err := strconv.Atoi(qlim)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
limit = conv
page, err := query.GetIntOr(r.URL.Query(), "page", 0)
pageSize, err := query.GetIntOr(r.URL.Query(), "pageSize", 10)
if err != nil || page < 0 || pageSize < 0 {
w.WriteHeader(http.StatusBadRequest)
return
}

queries := sqlc.New(e.Conn)
rows, err := queries.ListEndpoints(r.Context(), int32(limit))
rows, err := queries.ListEndpoints(r.Context(), sqlc.ListEndpointsParams{
Limit: int32(pageSize),
Offset: int32(page * pageSize),
})
if err != nil {
slog.Error("Query execution failed", slog.String("path", r.URL.Path), slog.String("msg", err.Error()))
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -177,6 +180,12 @@ func (e *Endpoint) list(w http.ResponseWriter, r *http.Request) {
if rows == nil {
rows = []sqlc.ListEndpointsRow{}
}
total, err := queries.CountEndpoints(r.Context())
if err != nil {
slog.Error("Query execution failed", slog.String("path", r.URL.Path), slog.String("msg", err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}

endpoints := make([]listEndpointEntry, len(rows))
for idx := range len(rows) {
Expand All @@ -190,6 +199,8 @@ func (e *Endpoint) list(w http.ResponseWriter, r *http.Request) {
}
}
response := listEndpointsResponse{
Total: total,
Retrieved: len(rows),
Endpoints: endpoints,
}
content, err := json.Marshal(response)
Expand Down
20 changes: 20 additions & 0 deletions internal/query/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package query

import (
"net/url"
"strconv"
)

func GetIntOr(values url.Values, name string, defaultValue int) (int, error) {
value := values.Get(name)
if value == "" {
return defaultValue, nil
}

intValue, err := strconv.Atoi(value)
if err != nil {
return defaultValue, err
}

return intValue, nil
}
22 changes: 19 additions & 3 deletions internal/sqlc/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ FROM
LEFT JOIN
ery_extension ee
ON ee.endpoint_id = pe.sid
LIMIT $1;
LIMIT $1 OFFSET $2;

-- name: NewExtension :exec
INSERT INTO ery_extension
Expand Down Expand Up @@ -64,4 +64,7 @@ INNER JOIN
INNER JOIN
ps_aors aor ON aor.id = pe.id
WHERE
pe.sid = $1;
pe.sid = $1;

-- name: CountEndpoints :one
SELECT COUNT(*) FROM ps_endpoints;

0 comments on commit af4659b

Please sign in to comment.