-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
settingsrv.go
103 lines (80 loc) · 2.77 KB
/
settingsrv.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Pipe - A small and beautiful blogging platform written in golang.
// Copyright (C) 2017-2018, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package service
import (
"sync"
"github.com/b3log/pipe/cache"
"github.com/b3log/pipe/model"
)
// Setting service.
var Setting = &settingService{
mutex: &sync.Mutex{},
}
type settingService struct {
mutex *sync.Mutex
}
func (srv *settingService) GetSetting(category, name string, blogID uint64) *model.Setting {
ret := cache.Setting.Get(category, name, blogID)
if nil != ret {
return ret
}
ret = &model.Setting{}
if err := db.Where("`category` = ? AND `name` = ? AND `blog_id` = ?", category, name, blogID).Find(ret).Error; nil != err {
return nil
}
cache.Setting.Put(ret)
return ret
}
func (srv *settingService) GetCategorySettings(category string, blogID uint64) []*model.Setting {
var ret []*model.Setting
if err := db.Where("`category` = ? AND `blog_id` = ?", category, blogID).Find(&ret).Error; nil != err {
return nil
}
return ret
}
func (srv *settingService) GetAllSettings(blogID uint64) []*model.Setting {
var ret []*model.Setting
if err := db.Where("`category` != ? AND `blog_id` = ?", model.SettingCategoryStatistic, blogID).Find(&ret).Error; nil != err {
return nil
}
return ret
}
func (srv *settingService) GetSettings(category string, names []string, blogID uint64) map[string]*model.Setting {
ret := map[string]*model.Setting{}
var settings []*model.Setting
if err := db.Where("`category` = ? AND `name` IN (?) AND `blog_id` = ?", category, names, blogID).Find(&settings).Error; nil != err {
return nil
}
for _, setting := range settings {
ret[setting.Name] = setting
}
return ret
}
func (srv *settingService) UpdateSettings(category string, settings []*model.Setting, blogID uint64) error {
srv.mutex.Lock()
defer srv.mutex.Unlock()
tx := db.Begin()
for _, setting := range settings {
if err := tx.Model(&model.Setting{}).Where("`category` = ? AND `name` = ? AND `blog_id` = ?",
category, setting.Name, blogID).Select("value").Updates(map[string]interface{}{"value": setting.Value}).Error; nil != err {
tx.Rollback()
return err
}
cache.Setting.Put(setting)
}
tx.Commit()
return nil
}