2017-08-19 11:35:44 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/asdine/storm"
|
|
|
|
fm "github.com/hacdias/filemanager"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UsersStore struct {
|
|
|
|
DB *storm.DB
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:21:36 +00:00
|
|
|
func (u UsersStore) Get(id int, builder fm.FSBuilder) (*fm.User, error) {
|
2017-08-19 11:35:44 +00:00
|
|
|
var us *fm.User
|
|
|
|
err := u.DB.One("ID", id, us)
|
|
|
|
if err == storm.ErrNotFound {
|
2017-08-20 07:42:38 +00:00
|
|
|
return nil, fm.ErrNotExist
|
2017-08-19 11:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:21:36 +00:00
|
|
|
us.FileSystem = builder(us.Scope)
|
|
|
|
return us, nil
|
2017-08-19 11:35:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-20 08:21:36 +00:00
|
|
|
func (u UsersStore) Gets(builder fm.FSBuilder) ([]*fm.User, error) {
|
2017-08-19 11:35:44 +00:00
|
|
|
var us []*fm.User
|
|
|
|
err := u.DB.All(us)
|
2017-08-20 08:21:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return us, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range us {
|
|
|
|
user.FileSystem = builder(user.Scope)
|
|
|
|
}
|
|
|
|
|
2017-08-19 11:35:44 +00:00
|
|
|
return us, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UsersStore) Update(us *fm.User, fields ...string) error {
|
|
|
|
if len(fields) == 0 {
|
|
|
|
return u.Save(us)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range fields {
|
|
|
|
val := reflect.ValueOf(us).Elem().FieldByName(field).Interface()
|
|
|
|
if err := u.DB.UpdateField(us, field, val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UsersStore) Save(us *fm.User) error {
|
|
|
|
return u.DB.Save(us)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UsersStore) Delete(id int) error {
|
|
|
|
return u.DB.DeleteStruct(&fm.User{ID: id})
|
|
|
|
}
|