mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
reduce code duplication
Former-commit-id: fa4946eff4f979e8fd3550a0b11f3ab147177b8f [formerly cd071c220157452a095325c049d801e99d2f8a94] [formerly 7a619be88fb162ddbf897387a31577fd3e94bfec [formerly ef56112711
]]
Former-commit-id: 692897eac98f1d8d4a916d0ef0aa342b994a186b [formerly 69d9f127de2deadbbcaa0174fc1a4613ce2cb93d]
Former-commit-id: 49c5aef77aa74ec8352368627c98b9e50d417228
This commit is contained in:
parent
6ab8779948
commit
b660404bfe
@ -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)
|
||||
if err == storm.ErrNotFound {
|
||||
return nil, errors.ErrNotExist
|
||||
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
|
||||
}
|
||||
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
Block a user