-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package model | ||
|
||
import ( | ||
"strconv" | ||
|
||
"jcourse_go/model/po" | ||
) | ||
|
||
type SettingType = string | ||
|
||
const ( | ||
SettingTypeString SettingType = "string" | ||
SettingTypeInt SettingType = "int" | ||
SettingTypeBool SettingType = "bool" | ||
) | ||
|
||
type Setting interface { | ||
ToPO() po.SettingPO | ||
FromPO(po.SettingPO) error | ||
} | ||
|
||
type StringSetting struct { | ||
Key string | ||
Value string | ||
} | ||
|
||
func (s *StringSetting) ToPO() po.SettingPO { | ||
return po.SettingPO{ | ||
Key: s.Key, | ||
Value: s.Value, | ||
Type: SettingTypeString, | ||
} | ||
} | ||
|
||
func (s *StringSetting) FromPO(po po.SettingPO) error { | ||
s.Key = po.Key | ||
s.Value = po.Value | ||
return nil | ||
} | ||
|
||
type IntSetting struct { | ||
Key string | ||
Value int64 | ||
} | ||
|
||
func (s *IntSetting) ToPO() po.SettingPO { | ||
return po.SettingPO{ | ||
Key: s.Key, | ||
Value: strconv.FormatInt(s.Value, 10), | ||
Type: SettingTypeInt, | ||
} | ||
} | ||
|
||
func (s *IntSetting) FromPO(po po.SettingPO) (err error) { | ||
s.Key = po.Key | ||
s.Value, err = strconv.ParseInt(po.Value, 10, 64) | ||
return | ||
} | ||
|
||
type BoolSetting struct { | ||
Key string | ||
Value bool | ||
} | ||
|
||
func (s *BoolSetting) ToPO() po.SettingPO { | ||
return po.SettingPO{ | ||
Key: s.Key, | ||
Value: strconv.FormatBool(s.Value), | ||
Type: SettingTypeBool, | ||
} | ||
} | ||
|
||
func (s *BoolSetting) FromPO(po po.SettingPO) (err error) { | ||
s.Key = po.Key | ||
s.Value, err = strconv.ParseBool(po.Value) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
|
||
"jcourse_go/model/model" | ||
"jcourse_go/model/po" | ||
) | ||
|
||
func GetSettingFromPO(po po.SettingPO) (s model.Setting, err error) { | ||
switch po.Type { | ||
case model.SettingTypeString: | ||
s = &model.StringSetting{} | ||
case model.SettingTypeInt: | ||
s = &model.IntSetting{} | ||
case model.SettingTypeBool: | ||
s = &model.BoolSetting{} | ||
} | ||
if s == nil { | ||
return nil, errors.New("unknown setting type") | ||
} | ||
err = s.FromPO(po) | ||
return | ||
} | ||
|
||
type ISettingQuery interface { | ||
GetSetting(ctx context.Context, key string) (model.Setting, error) | ||
SetSetting(ctx context.Context, userID int64, setting model.Setting) error | ||
} | ||
|
||
type SettingQuery struct { | ||
db *gorm.DB | ||
} | ||
|
||
func (s *SettingQuery) SetSetting(ctx context.Context, userID int64, setting model.Setting) error { | ||
settingPO := setting.ToPO() | ||
settingPO.UpdatedBy = userID | ||
err := s.db.WithContext(ctx).Model(&po.SettingPO{}). | ||
Clauses(clause.OnConflict{UpdateAll: true}).Create(&settingPO).Error | ||
return err | ||
} | ||
|
||
func (s *SettingQuery) GetSetting(ctx context.Context, key string) (model.Setting, error) { | ||
res := po.SettingPO{} | ||
err := s.db.WithContext(ctx).Model(&po.SettingPO{}).Where("key = ?", key).Find(&res).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
return GetSettingFromPO(res) | ||
} | ||
|
||
func NewSettingQuery(db *gorm.DB) ISettingQuery { | ||
return &SettingQuery{db: db} | ||
} |