updates go-pg/pg to v7

This commit is contained in:
dhax 2019-01-13 22:37:56 +01:00
parent 5737e75cc4
commit 8cc5d6241d
5 changed files with 59 additions and 103 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/dhax/go-base/models"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"github.com/go-pg/pg/urlvalues"
)
var (
@ -30,33 +31,35 @@ func NewAdmAccountStore(db *pg.DB) *AdmAccountStore {
// AccountFilter provides pagination and filtering options on accounts.
type AccountFilter struct {
orm.Pager
Filters url.Values
Order []string
Pager *urlvalues.Pager
Filter *urlvalues.Filter
Order []string
}
// Filter applies an AccountFilter on an orm.Query.
func (f *AccountFilter) Filter(q *orm.Query) (*orm.Query, error) {
q = q.Apply(f.Pager.Paginate)
q = q.Apply(orm.URLFilters(f.Filters))
// NewAccountFilter returns an AccountFilter with options parsed from request url values.
func NewAccountFilter(params interface{}) (*AccountFilter, error) {
p := urlvalues.Values(params.(url.Values))
f := &AccountFilter{
Pager: urlvalues.NewPager(p),
Filter: urlvalues.NewFilter(p),
Order: p["order"],
}
return f, nil
}
// Apply applies an AccountFilter on an orm.Query.
func (f *AccountFilter) Apply(q *orm.Query) (*orm.Query, error) {
q = q.Apply(f.Pager.Pagination)
q = q.Apply(f.Filter.Filters)
q = q.Order(f.Order...)
return q, nil
}
// NewAccountFilter returns an AccountFilter with options parsed from request url values.
func NewAccountFilter(v url.Values) AccountFilter {
var f AccountFilter
f.SetURLValues(v)
f.Filters = v
f.Order = v["order"]
return f
}
// List applies a filter and returns paginated array of matching results and total count.
func (s *AdmAccountStore) List(f AccountFilter) ([]pwdless.Account, int, error) {
func (s *AdmAccountStore) List(f *AccountFilter) ([]pwdless.Account, int, error) {
a := []pwdless.Account{}
count, err := s.db.Model(&a).
Apply(f.Filter).
Apply(f.Apply).
SelectAndCount()
if err != nil {
return nil, 0, err