move auth related models into auth package
This commit is contained in:
parent
2a9667a616
commit
6b7b5f2ae9
12 changed files with 159 additions and 151 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/dhax/go-base/auth"
|
||||
"github.com/dhax/go-base/models"
|
||||
"github.com/go-pg/pg"
|
||||
)
|
||||
|
|
@ -18,8 +19,8 @@ func NewAccountStore(db *pg.DB) *AccountStore {
|
|||
}
|
||||
|
||||
// Get an account by ID.
|
||||
func (s *AccountStore) Get(id int) (*models.Account, error) {
|
||||
a := models.Account{ID: id}
|
||||
func (s *AccountStore) Get(id int) (*auth.Account, error) {
|
||||
a := auth.Account{ID: id}
|
||||
err := s.db.Model(&a).
|
||||
Where("account.id = ?id").
|
||||
Column("account.*", "Profile", "Token").
|
||||
|
|
@ -28,7 +29,7 @@ func (s *AccountStore) Get(id int) (*models.Account, error) {
|
|||
}
|
||||
|
||||
// Update an account.
|
||||
func (s *AccountStore) Update(a *models.Account) error {
|
||||
func (s *AccountStore) Update(a *auth.Account) error {
|
||||
_, err := s.db.Model(a).
|
||||
Column("email", "name").
|
||||
Update()
|
||||
|
|
@ -36,9 +37,9 @@ func (s *AccountStore) Update(a *models.Account) error {
|
|||
}
|
||||
|
||||
// Delete an account.
|
||||
func (s *AccountStore) Delete(a *models.Account) error {
|
||||
func (s *AccountStore) Delete(a *auth.Account) error {
|
||||
err := s.db.RunInTransaction(func(tx *pg.Tx) error {
|
||||
if _, err := tx.Model(&models.Token{}).
|
||||
if _, err := tx.Model(&auth.Token{}).
|
||||
Where("account_id = ?", a.ID).
|
||||
Delete(); err != nil {
|
||||
return err
|
||||
|
|
@ -54,7 +55,7 @@ func (s *AccountStore) Delete(a *models.Account) error {
|
|||
}
|
||||
|
||||
// UpdateToken updates a jwt refresh token.
|
||||
func (s *AccountStore) UpdateToken(t *models.Token) error {
|
||||
func (s *AccountStore) UpdateToken(t *auth.Token) error {
|
||||
_, err := s.db.Model(t).
|
||||
Column("identifier").
|
||||
Update()
|
||||
|
|
@ -62,7 +63,7 @@ func (s *AccountStore) UpdateToken(t *models.Token) error {
|
|||
}
|
||||
|
||||
// DeleteToken deletes a jwt refresh token.
|
||||
func (s *AccountStore) DeleteToken(t *models.Token) error {
|
||||
func (s *AccountStore) DeleteToken(t *auth.Token) error {
|
||||
err := s.db.Delete(t)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package database
|
|||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/dhax/go-base/auth"
|
||||
"github.com/dhax/go-base/models"
|
||||
"github.com/go-pg/pg"
|
||||
)
|
||||
|
|
@ -25,8 +26,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 models.AccountFilter) (*[]models.Account, int, error) {
|
||||
var a []models.Account
|
||||
func (s *AdmAccountStore) List(f auth.AccountFilter) (*[]auth.Account, int, error) {
|
||||
var a []auth.Account
|
||||
count, err := s.db.Model(&a).
|
||||
Apply(f.Filter).
|
||||
SelectAndCount()
|
||||
|
|
@ -37,7 +38,7 @@ func (s *AdmAccountStore) List(f models.AccountFilter) (*[]models.Account, int,
|
|||
}
|
||||
|
||||
// Create creates a new account.
|
||||
func (s *AdmAccountStore) Create(a *models.Account) error {
|
||||
func (s *AdmAccountStore) Create(a *auth.Account) error {
|
||||
count, _ := s.db.Model(a).
|
||||
Where("email = ?email").
|
||||
Count()
|
||||
|
|
@ -61,22 +62,22 @@ func (s *AdmAccountStore) Create(a *models.Account) error {
|
|||
}
|
||||
|
||||
// Get account by ID.
|
||||
func (s *AdmAccountStore) Get(id int) (*models.Account, error) {
|
||||
a := models.Account{ID: id}
|
||||
func (s *AdmAccountStore) Get(id int) (*auth.Account, error) {
|
||||
a := auth.Account{ID: id}
|
||||
err := s.db.Select(&a)
|
||||
return &a, err
|
||||
}
|
||||
|
||||
// Update account.
|
||||
func (s *AdmAccountStore) Update(a *models.Account) error {
|
||||
func (s *AdmAccountStore) Update(a *auth.Account) error {
|
||||
err := s.db.Update(a)
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete account.
|
||||
func (s *AdmAccountStore) Delete(a *models.Account) error {
|
||||
func (s *AdmAccountStore) Delete(a *auth.Account) error {
|
||||
err := s.db.RunInTransaction(func(tx *pg.Tx) error {
|
||||
if _, err := tx.Model(&models.Token{}).
|
||||
if _, err := tx.Model(&auth.Token{}).
|
||||
Where("account_id = ?", a.ID).
|
||||
Delete(); err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package database
|
|||
import (
|
||||
"time"
|
||||
|
||||
"github.com/dhax/go-base/models"
|
||||
"github.com/dhax/go-base/auth"
|
||||
"github.com/go-pg/pg"
|
||||
)
|
||||
|
||||
|
|
@ -20,8 +20,8 @@ func NewAuthStore(db *pg.DB) *AuthStore {
|
|||
}
|
||||
|
||||
// GetByID returns an account by ID.
|
||||
func (s *AuthStore) GetByID(id int) (*models.Account, error) {
|
||||
a := models.Account{ID: id}
|
||||
func (s *AuthStore) GetByID(id int) (*auth.Account, error) {
|
||||
a := auth.Account{ID: id}
|
||||
err := s.db.Model(&a).
|
||||
Column("account.*").
|
||||
Where("id = ?id").
|
||||
|
|
@ -30,8 +30,8 @@ func (s *AuthStore) GetByID(id int) (*models.Account, error) {
|
|||
}
|
||||
|
||||
// GetByEmail returns an account by email.
|
||||
func (s *AuthStore) GetByEmail(e string) (*models.Account, error) {
|
||||
a := models.Account{Email: e}
|
||||
func (s *AuthStore) GetByEmail(e string) (*auth.Account, error) {
|
||||
a := auth.Account{Email: e}
|
||||
err := s.db.Model(&a).
|
||||
Column("id", "active", "email", "name").
|
||||
Where("email = ?email").
|
||||
|
|
@ -40,8 +40,8 @@ func (s *AuthStore) GetByEmail(e string) (*models.Account, error) {
|
|||
}
|
||||
|
||||
// GetByRefreshToken returns an account and refresh token by token identifier.
|
||||
func (s *AuthStore) GetByRefreshToken(t string) (*models.Account, *models.Token, error) {
|
||||
token := models.Token{Token: t}
|
||||
func (s *AuthStore) GetByRefreshToken(t string) (*auth.Account, *auth.Token, error) {
|
||||
token := auth.Token{Token: t}
|
||||
err := s.db.Model(&token).
|
||||
Where("token = ?token").
|
||||
First()
|
||||
|
|
@ -49,7 +49,7 @@ func (s *AuthStore) GetByRefreshToken(t string) (*models.Account, *models.Token,
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
a := models.Account{ID: token.AccountID}
|
||||
a := auth.Account{ID: token.AccountID}
|
||||
err = s.db.Model(&a).
|
||||
Column("account.*").
|
||||
Where("id = ?id").
|
||||
|
|
@ -59,7 +59,7 @@ func (s *AuthStore) GetByRefreshToken(t string) (*models.Account, *models.Token,
|
|||
}
|
||||
|
||||
// UpdateAccount upates account data related to authentication.
|
||||
func (s *AuthStore) UpdateAccount(a *models.Account) error {
|
||||
func (s *AuthStore) UpdateAccount(a *auth.Account) error {
|
||||
_, err := s.db.Model(a).
|
||||
Column("last_login").
|
||||
Update()
|
||||
|
|
@ -67,7 +67,7 @@ func (s *AuthStore) UpdateAccount(a *models.Account) error {
|
|||
}
|
||||
|
||||
// SaveRefreshToken creates or updates a refresh token.
|
||||
func (s *AuthStore) SaveRefreshToken(t *models.Token) error {
|
||||
func (s *AuthStore) SaveRefreshToken(t *auth.Token) error {
|
||||
var err error
|
||||
if t.ID == 0 {
|
||||
err = s.db.Insert(t)
|
||||
|
|
@ -78,14 +78,14 @@ func (s *AuthStore) SaveRefreshToken(t *models.Token) error {
|
|||
}
|
||||
|
||||
// DeleteRefreshToken deletes a refresh token.
|
||||
func (s *AuthStore) DeleteRefreshToken(t *models.Token) error {
|
||||
func (s *AuthStore) DeleteRefreshToken(t *auth.Token) error {
|
||||
err := s.db.Delete(t)
|
||||
return err
|
||||
}
|
||||
|
||||
// PurgeExpiredToken deletes expired refresh token.
|
||||
func (s *AuthStore) PurgeExpiredToken() error {
|
||||
_, err := s.db.Model(&models.Token{}).
|
||||
_, err := s.db.Model(&auth.Token{}).
|
||||
Where("expiry < ?", time.Now()).
|
||||
Delete()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue