@@ -24,13 +24,14 @@ type ISettingRepo interface {
2424 DelMonitorBase (timeForDelete time.Time ) error
2525 DelMonitorIO (timeForDelete time.Time ) error
2626 DelMonitorNet (timeForDelete time.Time ) error
27+ UpdateOrCreate (key , value string ) error
2728}
2829
2930func NewISettingRepo () ISettingRepo {
3031 return & SettingRepo {}
3132}
3233
33- func (u * SettingRepo ) GetList (opts ... DBOption ) ([]model.Setting , error ) {
34+ func (s * SettingRepo ) GetList (opts ... DBOption ) ([]model.Setting , error ) {
3435 var settings []model.Setting
3536 db := global .DB .Model (& model.Setting {})
3637 for _ , opt := range opts {
@@ -40,15 +41,15 @@ func (u *SettingRepo) GetList(opts ...DBOption) ([]model.Setting, error) {
4041 return settings , err
4142}
4243
43- func (u * SettingRepo ) Create (key , value string ) error {
44+ func (s * SettingRepo ) Create (key , value string ) error {
4445 setting := & model.Setting {
4546 Key : key ,
4647 Value : value ,
4748 }
4849 return global .DB .Create (setting ).Error
4950}
5051
51- func (u * SettingRepo ) Get (opts ... DBOption ) (model.Setting , error ) {
52+ func (s * SettingRepo ) Get (opts ... DBOption ) (model.Setting , error ) {
5253 var settings model.Setting
5354 db := global .DB .Model (& model.Setting {})
5455 for _ , opt := range opts {
@@ -58,7 +59,7 @@ func (u *SettingRepo) Get(opts ...DBOption) (model.Setting, error) {
5859 return settings , err
5960}
6061
61- func (u * SettingRepo ) GetValueByKey (key string ) (string , error ) {
62+ func (s * SettingRepo ) GetValueByKey (key string ) (string , error ) {
6263 var setting model.Setting
6364 if err := global .DB .Model (& model.Setting {}).Where ("key = ?" , key ).First (& setting ).Error ; err != nil {
6465 global .LOG .Errorf ("load %s from db setting failed, err: %v" , key , err )
@@ -67,31 +68,35 @@ func (u *SettingRepo) GetValueByKey(key string) (string, error) {
6768 return setting .Value , nil
6869}
6970
70- func (c * SettingRepo ) WithByKey (key string ) DBOption {
71+ func (s * SettingRepo ) WithByKey (key string ) DBOption {
7172 return func (g * gorm.DB ) * gorm.DB {
7273 return g .Where ("key = ?" , key )
7374 }
7475}
7576
76- func (u * SettingRepo ) Update (key , value string ) error {
77+ func (s * SettingRepo ) Update (key , value string ) error {
7778 return global .DB .Model (& model.Setting {}).Where ("key = ?" , key ).Updates (map [string ]interface {}{"value" : value }).Error
7879}
7980
80- func (u * SettingRepo ) CreateMonitorBase (model model.MonitorBase ) error {
81+ func (s * SettingRepo ) CreateMonitorBase (model model.MonitorBase ) error {
8182 return global .MonitorDB .Create (& model ).Error
8283}
83- func (u * SettingRepo ) BatchCreateMonitorIO (ioList []model.MonitorIO ) error {
84+ func (s * SettingRepo ) BatchCreateMonitorIO (ioList []model.MonitorIO ) error {
8485 return global .MonitorDB .CreateInBatches (ioList , len (ioList )).Error
8586}
86- func (u * SettingRepo ) BatchCreateMonitorNet (ioList []model.MonitorNetwork ) error {
87+ func (s * SettingRepo ) BatchCreateMonitorNet (ioList []model.MonitorNetwork ) error {
8788 return global .MonitorDB .CreateInBatches (ioList , len (ioList )).Error
8889}
89- func (u * SettingRepo ) DelMonitorBase (timeForDelete time.Time ) error {
90+ func (s * SettingRepo ) DelMonitorBase (timeForDelete time.Time ) error {
9091 return global .MonitorDB .Where ("created_at < ?" , timeForDelete ).Delete (& model.MonitorBase {}).Error
9192}
92- func (u * SettingRepo ) DelMonitorIO (timeForDelete time.Time ) error {
93+ func (s * SettingRepo ) DelMonitorIO (timeForDelete time.Time ) error {
9394 return global .MonitorDB .Where ("created_at < ?" , timeForDelete ).Delete (& model.MonitorIO {}).Error
9495}
95- func (u * SettingRepo ) DelMonitorNet (timeForDelete time.Time ) error {
96+ func (s * SettingRepo ) DelMonitorNet (timeForDelete time.Time ) error {
9697 return global .MonitorDB .Where ("created_at < ?" , timeForDelete ).Delete (& model.MonitorNetwork {}).Error
9798}
99+
100+ func (s * SettingRepo ) UpdateOrCreate (key , value string ) error {
101+ return global .DB .Model (& model.Setting {}).Where ("key = ?" , key ).Assign (model.Setting {Key : key , Value : value }).FirstOrCreate (& model.Setting {}).Error
102+ }
0 commit comments