Skip to content

Commit

Permalink
feat: add site setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dujiajun committed Sep 21, 2024
1 parent 98c777e commit 93b0f02
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 7 deletions.
77 changes: 77 additions & 0 deletions model/model/setting.go
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
}
13 changes: 7 additions & 6 deletions model/po/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 3 additions & 1 deletion repository/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
58 changes: 58 additions & 0 deletions repository/setting.go
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}
}

0 comments on commit 93b0f02

Please sign in to comment.