refactor auth pkg into libraries

This commit is contained in:
dhax 2017-10-31 19:10:09 +01:00
parent 521f081ba0
commit aaf0a0928d
26 changed files with 592 additions and 504 deletions

View file

@ -3,7 +3,8 @@ package database
import (
"errors"
"github.com/dhax/go-base/auth"
"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"
)
@ -26,8 +27,8 @@ func NewAdmAccountStore(db *pg.DB) *AdmAccountStore {
}
// List applies a filter and returns paginated array of matching results and total count.
func (s *AdmAccountStore) List(f auth.AccountFilter) ([]auth.Account, int, error) {
a := []auth.Account{}
func (s *AdmAccountStore) List(f pwdless.AccountFilter) ([]pwdless.Account, int, error) {
a := []pwdless.Account{}
count, err := s.db.Model(&a).
Apply(f.Filter).
SelectAndCount()
@ -38,7 +39,7 @@ func (s *AdmAccountStore) List(f auth.AccountFilter) ([]auth.Account, int, error
}
// Create creates a new account.
func (s *AdmAccountStore) Create(a *auth.Account) error {
func (s *AdmAccountStore) Create(a *pwdless.Account) error {
count, _ := s.db.Model(a).
Where("email = ?email").
Count()
@ -62,22 +63,22 @@ func (s *AdmAccountStore) Create(a *auth.Account) error {
}
// Get account by ID.
func (s *AdmAccountStore) Get(id int) (*auth.Account, error) {
a := auth.Account{ID: id}
func (s *AdmAccountStore) Get(id int) (*pwdless.Account, error) {
a := pwdless.Account{ID: id}
err := s.db.Select(&a)
return &a, err
}
// Update account.
func (s *AdmAccountStore) Update(a *auth.Account) error {
func (s *AdmAccountStore) Update(a *pwdless.Account) error {
err := s.db.Update(a)
return err
}
// Delete account.
func (s *AdmAccountStore) Delete(a *auth.Account) error {
func (s *AdmAccountStore) Delete(a *pwdless.Account) error {
err := s.db.RunInTransaction(func(tx *pg.Tx) error {
if _, err := tx.Model(&auth.Token{}).
if _, err := tx.Model(&jwt.Token{}).
Where("account_id = ?", a.ID).
Delete(); err != nil {
return err