refactor AccountFilter into databasePackage

This commit is contained in:
dhax 2019-01-01 19:59:20 +01:00
parent 489137b24e
commit 7c19e9d87c
3 changed files with 32 additions and 30 deletions

View file

@ -7,7 +7,8 @@ import (
"strconv"
"github.com/dhax/go-base/auth/pwdless"
"github.com/go-ozzo/ozzo-validation"
"github.com/dhax/go-base/database"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-chi/chi"
"github.com/go-chi/render"
@ -20,7 +21,7 @@ var (
// AccountStore defines database operations for account management.
type AccountStore interface {
List(f pwdless.AccountFilter) ([]pwdless.Account, int, error)
List(f database.AccountFilter) ([]pwdless.Account, int, error)
Create(*pwdless.Account) error
Get(id int) (*pwdless.Account, error)
Update(*pwdless.Account) error
@ -100,7 +101,7 @@ func newAccountListResponse(a []pwdless.Account, count int) *accountListResponse
}
func (rs *AccountResource) list(w http.ResponseWriter, r *http.Request) {
f := pwdless.NewAccountFilter(r.URL.Query())
f := database.NewAccountFilter(r.URL.Query())
al, count, err := rs.Store.List(f)
if err != nil {
render.Render(w, r, ErrRender(err))

View file

@ -1,7 +1,6 @@
package pwdless
import (
"net/url"
"strings"
"time"
@ -60,7 +59,7 @@ func (a *Account) Validate() error {
)
}
// CanLogin returns true if is user is allowed to login.
// CanLogin returns true if user is allowed to login.
func (a *Account) CanLogin() bool {
return a.Active
}
@ -73,27 +72,3 @@ func (a *Account) Claims() jwtauth.Claims {
"roles": a.Roles,
}
}
// AccountFilter provides pagination and filtering options on accounts.
type AccountFilter struct {
orm.Pager
Filters url.Values
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))
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
}

View file

@ -2,11 +2,13 @@ package database
import (
"errors"
"net/url"
"github.com/dhax/go-base/auth/jwt"
"github.com/dhax/go-base/auth/pwdless"
"github.com/dhax/go-base/models"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
)
var (
@ -26,8 +28,32 @@ func NewAdmAccountStore(db *pg.DB) *AdmAccountStore {
}
}
// AccountFilter provides pagination and filtering options on accounts.
type AccountFilter struct {
orm.Pager
Filters url.Values
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))
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 pwdless.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).