-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
53 lines (49 loc) · 1.24 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package dal
import (
"context"
"github.com/canuran/go-home/config"
"github.com/canuran/go-home/generate/model"
"github.com/canuran/go-home/generate/query"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func GetConfig(ctx context.Context, configKey string) (*model.Config, error) {
db, ctx := config.GetDB(ctx)
c := query.Use(db).Config
do, err := c.Where(c.Config.Eq(configKey)).First()
if err == gorm.ErrRecordNotFound {
return nil, nil
}
if err != nil {
logrus.Errorf("GetConfig err:%s", err.Error())
return nil, err
}
return do, nil
}
func SaveConfig(ctx context.Context, configDO *model.Config) error {
if configDO == nil || len(configDO.Config) < 1 {
return nil
}
db, ctx := config.GetDB(ctx)
c := query.Use(db).Config
err := c.Clauses(config.ConflictUpdateAll).Create(configDO)
if err != nil {
logrus.Errorf("SaveConfig err:%s", err.Error())
return err
}
return nil
}
func UpdateConfig(ctx context.Context, configDO *model.Config) error {
if configDO == nil || len(configDO.Config) < 1 {
return nil
}
db, ctx := config.GetDB(ctx)
c := query.Use(db).Config
_, err := c.Where(c.Config.Eq(configDO.Config)).
Updates(configDO)
if err != nil {
logrus.Errorf("UpdateConfig err:%s", err.Error())
return err
}
return nil
}