big ole refactor
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/oslstats/internal/validation"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
@@ -19,6 +22,41 @@ type OrderOpts struct {
|
||||
Label string
|
||||
}
|
||||
|
||||
func GetPageOpts(s *hws.Server, w http.ResponseWriter, r *http.Request) (*PageOpts, bool) {
|
||||
var getter validation.Getter
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
getter = validation.NewQueryGetter(r)
|
||||
case "POST":
|
||||
var ok bool
|
||||
getter, ok = validation.ParseFormOrError(s, w, r)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
return getPageOpts(s, w, r, getter), true
|
||||
}
|
||||
|
||||
func getPageOpts(s *hws.Server, w http.ResponseWriter, r *http.Request, g validation.Getter) *PageOpts {
|
||||
page := g.Int("page").Optional().Min(1).Value
|
||||
perPage := g.Int("per_page").Optional().Min(1).Max(100).Value
|
||||
order := g.String("order").TrimSpace().ToUpper().Optional().AllowedValues([]string{"ASC", "DESC"}).Value
|
||||
orderBy := g.String("order_by").TrimSpace().Optional().ToLower().Value
|
||||
valid := g.ValidateAndError(s, w, r)
|
||||
if !valid {
|
||||
return nil
|
||||
}
|
||||
pageOpts := &PageOpts{
|
||||
Page: page,
|
||||
PerPage: perPage,
|
||||
Order: bun.Order(order),
|
||||
OrderBy: orderBy,
|
||||
}
|
||||
return pageOpts
|
||||
}
|
||||
|
||||
func setPageOpts(q *bun.SelectQuery, p, d *PageOpts, totalitems int) (*bun.SelectQuery, *PageOpts) {
|
||||
if p == nil {
|
||||
p = new(PageOpts)
|
||||
|
||||
Reference in New Issue
Block a user