From 93b0f029a6ed22fab338d95cf11687af490a8d7a Mon Sep 17 00:00:00 2001 From: Du Jiajun Date: Sat, 21 Sep 2024 21:29:37 +0800 Subject: [PATCH] feat: add site setting --- model/model/setting.go | 77 ++++++++++++++++++++++++++++++++++++++++++ model/po/setting.go | 13 +++---- repository/migrate.go | 4 ++- repository/setting.go | 58 +++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 model/model/setting.go create mode 100644 repository/setting.go diff --git a/model/model/setting.go b/model/model/setting.go new file mode 100644 index 0000000..3fffa5c --- /dev/null +++ b/model/model/setting.go @@ -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 +} diff --git a/model/po/setting.go b/model/po/setting.go index a5c6fe1..2845218 100644 --- a/model/po/setting.go +++ b/model/po/setting.go @@ -2,13 +2,14 @@ package po import "gorm.io/gorm" -type SettingItemPO struct { +type SettingPO struct { gorm.Model - Key string - Type string - Value string + Key string `gorm:"index:uniq_setting,unique"` + Type string + Value string + UpdatedBy int64 // user id } -func (po *SettingItemPO) TableName() string { - return "setting_items" +func (po *SettingPO) TableName() string { + return "settings" } diff --git a/repository/migrate.go b/repository/migrate.go index 84fb880..c9639e5 100644 --- a/repository/migrate.go +++ b/repository/migrate.go @@ -10,7 +10,9 @@ func Migrate(db *gorm.DB) error { err := db.AutoMigrate(&po.UserPO{}, &po.BaseCoursePO{}, &po.CoursePO{}, &po.TeacherPO{}, &po.CourseCategoryPO{}, &po.OfferedCoursePO{}, &po.OfferedCourseTeacherPO{}, - &po.ReviewPO{}, &po.RatingPO{}, &po.TrainingPlanPO{}, &po.TrainingPlanCoursePO{}) + &po.TrainingPlanPO{}, &po.TrainingPlanCoursePO{}, + &po.ReviewPO{}, &po.RatingPO{}, + &po.SettingPO{}) if err != nil { return err } diff --git a/repository/setting.go b/repository/setting.go new file mode 100644 index 0000000..75c813b --- /dev/null +++ b/repository/setting.go @@ -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} +}