reduce code duplication
This commit is contained in:
parent
88644664e4
commit
ef56112711
|
@ -12,32 +12,29 @@ type usersBackend struct {
|
||||||
db *storm.DB
|
db *storm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st usersBackend) GetByID(id uint) (*users.User, error) {
|
func (st usersBackend) GetBy(i interface{}) (user *users.User, err error) {
|
||||||
user := &users.User{}
|
user = &users.User{}
|
||||||
err := st.db.One("ID", id, user)
|
|
||||||
if err == storm.ErrNotFound {
|
var arg string
|
||||||
return nil, errors.ErrNotExist
|
switch i.(type) {
|
||||||
|
case uint:
|
||||||
|
arg = "ID"
|
||||||
|
case string:
|
||||||
|
arg = "Username"
|
||||||
|
default:
|
||||||
|
return nil, errors.ErrInvalidDataType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = st.db.One(arg, i, user)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == storm.ErrNotFound {
|
||||||
|
return nil, errors.ErrNotExist
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, nil
|
return
|
||||||
}
|
|
||||||
|
|
||||||
func (st usersBackend) GetByUsername(username string) (*users.User, error) {
|
|
||||||
user := &users.User{}
|
|
||||||
err := st.db.One("Username", username, user)
|
|
||||||
if err == storm.ErrNotFound {
|
|
||||||
return nil, errors.ErrNotExist
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st usersBackend) Gets() ([]*users.User, error) {
|
func (st usersBackend) Gets() ([]*users.User, error) {
|
||||||
|
@ -82,7 +79,7 @@ func (st usersBackend) DeleteByID(id uint) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st usersBackend) DeleteByUsername(username string) error {
|
func (st usersBackend) DeleteByUsername(username string) error {
|
||||||
user, err := st.GetByUsername(username)
|
user, err := st.GetBy(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,7 @@ import (
|
||||||
|
|
||||||
// StorageBackend is the interface to implement for a users storage.
|
// StorageBackend is the interface to implement for a users storage.
|
||||||
type StorageBackend interface {
|
type StorageBackend interface {
|
||||||
GetByID(uint) (*User, error)
|
GetBy(interface{}) (*User, error)
|
||||||
GetByUsername(string) (*User, error)
|
|
||||||
Gets() ([]*User, error)
|
Gets() ([]*User, error)
|
||||||
Save(u *User) error
|
Save(u *User) error
|
||||||
Update(u *User, fields ...string) error
|
Update(u *User, fields ...string) error
|
||||||
|
@ -36,27 +35,13 @@ func NewStorage(back StorageBackend) *Storage {
|
||||||
// Get allows you to get a user by its name or username. The provided
|
// Get allows you to get a user by its name or username. The provided
|
||||||
// id must be a string for username lookup or a uint for id lookup. If id
|
// id must be a string for username lookup or a uint for id lookup. If id
|
||||||
// is neither, a ErrInvalidDataType will be returned.
|
// is neither, a ErrInvalidDataType will be returned.
|
||||||
func (s *Storage) Get(baseScope string, id interface{}) (*User, error) {
|
func (s *Storage) Get(baseScope string, id interface{}) (user *User, err error) {
|
||||||
var (
|
user, err = s.back.GetBy(id)
|
||||||
user *User
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
switch id.(type) {
|
|
||||||
case string:
|
|
||||||
user, err = s.back.GetByUsername(id.(string))
|
|
||||||
case uint:
|
|
||||||
user, err = s.back.GetByID(id.(uint))
|
|
||||||
default:
|
|
||||||
return nil, errors.ErrInvalidDataType
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user.Clean(baseScope)
|
user.Clean(baseScope)
|
||||||
return user, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets gets a list of all users.
|
// Gets gets a list of all users.
|
||||||
|
|
Loading…
Reference in New Issue