Feat: model/policy

This commit is contained in:
HFO4
2019-11-14 14:18:10 +08:00
parent 41e0dec74c
commit 4309653160
7 changed files with 239 additions and 36 deletions

View File

@@ -8,18 +8,16 @@ import (
// Group 用户组模型
type Group struct {
gorm.Model
Name string
Policies string
MaxStorage uint64
SpeedLimit int
ShareEnabled bool
RangeTransferEnabled bool
WebDAVEnabled bool
Aria2Option string
Color string
Name string
Policies string
MaxStorage uint64
ShareEnabled bool
WebDAVEnabled bool
Aria2Option string
Color string
// 数据库忽略字段
PolicyList []int `gorm:"-"`
PolicyList []uint `gorm:"-"`
}
// GetGroupByID 用ID获取用户组
@@ -35,3 +33,16 @@ func (group *Group) AfterFind() (err error) {
err = json.Unmarshal([]byte(group.Policies), &group.PolicyList)
return err
}
// BeforeSave Save用户前的钩子
func (group *Group) BeforeSave() (err error) {
err = group.SerializePolicyList()
return err
}
//SerializePolicyList 将序列后的可选策略列表写入数据库字段
func (group *Group) SerializePolicyList() (err error) {
optionsValue, err := json.Marshal(&group.PolicyList)
group.Policies = string(optionsValue)
return err
}