reduce code duplication
This commit is contained in:
parent
88644664e4
commit
ef56112711
|
@ -12,32 +12,29 @@ type usersBackend struct {
|
|||
db *storm.DB
|
||||
}
|
||||
|
||||
func (st usersBackend) GetByID(id uint) (*users.User, error) {
|
||||
user := &users.User{}
|
||||
err := st.db.One("ID", id, user)
|
||||
func (st usersBackend) GetBy(i interface{}) (user *users.User, err error) {
|
||||
user = &users.User{}
|
||||
|
||||
var arg string
|
||||
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 == storm.ErrNotFound {
|
||||
return nil, errors.ErrNotExist
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
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
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
user, err := st.GetByUsername(username)
|
||||
user, err := st.GetBy(username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -9,8 +9,7 @@ import (
|
|||
|
||||
// StorageBackend is the interface to implement for a users storage.
|
||||
type StorageBackend interface {
|
||||
GetByID(uint) (*User, error)
|
||||
GetByUsername(string) (*User, error)
|
||||
GetBy(interface{}) (*User, error)
|
||||
Gets() ([]*User, error)
|
||||
Save(u *User) 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
|
||||
// id must be a string for username lookup or a uint for id lookup. If id
|
||||
// is neither, a ErrInvalidDataType will be returned.
|
||||
func (s *Storage) Get(baseScope string, id interface{}) (*User, error) {
|
||||
var (
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Storage) Get(baseScope string, id interface{}) (user *User, err error) {
|
||||
user, err = s.back.GetBy(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
user.Clean(baseScope)
|
||||
return user, err
|
||||
return
|
||||
}
|
||||
|
||||
// Gets gets a list of all users.
|
||||
|
|
Loading…
Reference in New Issue