Skip to content

Commit

Permalink
fix!: reduce search page limit to 20
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Nov 25, 2024
1 parent 02803c9 commit 0986a40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/search/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type ReponseSubject struct {
//nolint:funlen
func (c *client) Handle(ctx echo.Context) error {
auth := accessor.GetFromCtx(ctx)
q, err := req.GetPageQuery(ctx, defaultLimit, maxLimit)
q, err := req.GetPageQuerySoftLimit(ctx, defaultLimit, maxLimit)
if err != nil {
return err
}
Expand Down
36 changes: 36 additions & 0 deletions web/req/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ func (q PageQuery) Check(count int64) error {
return nil
}

// GetPageQuerySoftLimit apply soft limit on query without error.
func GetPageQuerySoftLimit(c echo.Context, defaultLimit int, maxLimit int) (PageQuery, error) {
q := PageQuery{Limit: defaultLimit}
var err error

raw := c.QueryParam("limit")
if raw != "" {
q.Limit, err = strconv.Atoi(raw)
if err != nil {
return q, res.BadRequest("can't parse query args limit as int: " + strconv.Quote(raw))
}

if q.Limit <= 0 {
return q, res.BadRequest("limit should be greater than zero")
}

if q.Limit > maxLimit {
q.Limit = maxLimit
}
}

raw = c.QueryParam("offset")
if raw != "" {
q.Offset, err = strconv.Atoi(raw)
if err != nil {
return q, res.BadRequest("can't parse query args offset as int: " + strconv.Quote(raw))
}

if q.Offset < 0 {
return q, res.BadRequest("offset should be greater than or equal to 0")
}
}

return q, nil
}

func GetPageQuery(c echo.Context, defaultLimit int, maxLimit int) (PageQuery, error) {
q := PageQuery{Limit: defaultLimit}
var err error
Expand Down

0 comments on commit 0986a40

Please sign in to comment.