parent
6311bf1dd7
commit
8e92ba0f9e
@ -1,48 +1,39 @@ |
|||||||
package ghost |
package ghost |
||||||
|
|
||||||
import ( |
// Params are generics query argument
|
||||||
"strconv" |
type Params struct { |
||||||
|
Limit int |
||||||
"github.com/valyala/fasthttp" |
Page int |
||||||
) |
|
||||||
|
|
||||||
// QueryParam is the generic query param applier
|
|
||||||
type QueryParam interface { |
|
||||||
Apply(headers *fasthttp.RequestHeader, args *fasthttp.Args) |
|
||||||
} |
} |
||||||
|
|
||||||
// QueryLimit returns limit param query
|
// Modifier function takes params and makes some changes
|
||||||
func QueryLimit(limit int) queryLimit { |
type Modifier func(params Params) Params |
||||||
return queryLimit{limit: limit} |
|
||||||
} |
|
||||||
|
|
||||||
// QueryPage returns page param query
|
|
||||||
func QueryPage(page int) queryPage { |
|
||||||
return queryPage{page: page} |
|
||||||
} |
|
||||||
|
|
||||||
// queryLimit implements the limit param query applier
|
// Modifiers is a list of modifier
|
||||||
type queryLimit struct{ limit int } |
type Modifiers []Modifier |
||||||
|
|
||||||
// Apply applies the limit argument to the query
|
// Apply function modifies params
|
||||||
func (ql queryLimit) Apply(headers *fasthttp.RequestHeader, args *fasthttp.Args) { |
func (ms Modifiers) Apply(params Params) Params { |
||||||
|
|
||||||
if ql.limit == 0 { |
for _, m := range ms { |
||||||
return |
params = m(params) |
||||||
} |
} |
||||||
|
|
||||||
args.Add("limit", strconv.Itoa(ql.limit)) |
return params |
||||||
} |
} |
||||||
|
|
||||||
// queryPage implements the page param query applier
|
// WithLimit modifier setups the limit
|
||||||
type queryPage struct{ page int } |
func WithLimit(limit int) Modifier { |
||||||
|
return func(params Params) Params { |
||||||
// Apply applies the page argument to the query
|
params.Limit = limit |
||||||
func (qp queryPage) Apply(headers *fasthttp.RequestHeader, args *fasthttp.Args) { |
return params |
||||||
|
|
||||||
if qp.page < 2 { |
|
||||||
return |
|
||||||
} |
} |
||||||
|
} |
||||||
|
|
||||||
args.Add("page", strconv.Itoa(qp.page)) |
// WithPage modifier setups the page
|
||||||
|
func WithPage(page int) Modifier { |
||||||
|
return func(params Params) Params { |
||||||
|
params.Page = page |
||||||
|
return params |
||||||
|
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue