模板可切换,模板引擎模块化有问题
This commit is contained in:
42
models/options.go
Normal file
42
models/options.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Option 对应 typecho_options 数据表
|
||||
// 用于存储系统配置项
|
||||
type Option struct {
|
||||
Name string `gorm:"column:name;type:varchar(32);primaryKey"` // 配置名称(主键)
|
||||
Value string `gorm:"column:value;type:text"` // 配置值
|
||||
User int `gorm:"column:user;type:int;default:0"` // 用户ID(0表示全局配置)
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (Option) TableName() string {
|
||||
return "typecho_options"
|
||||
}
|
||||
|
||||
// GetOptionValue 根据name和user获取配置值
|
||||
// 返回:配置值,错误(无记录时返回gorm.ErrRecordNotFound)
|
||||
func GetOptionValue(db *gorm.DB, name string, user int) (string, error) {
|
||||
|
||||
var option Option
|
||||
result := db.Where("name = ? AND user = ?", name, user).First(&option)
|
||||
if result.Error != nil {
|
||||
return "", result.Error // 可能返回gorm.ErrRecordNotFound或其他数据库错误
|
||||
}
|
||||
return option.Value, nil
|
||||
}
|
||||
|
||||
// SetOptionValue 根据name和user设置配置值(存在则更新,不存在则新增)
|
||||
func SetOptionValue(db *gorm.DB, name string, user int, value string) error {
|
||||
// 构造要更新/插入的配置项
|
||||
option := Option{
|
||||
Name: name,
|
||||
User: user,
|
||||
Value: value,
|
||||
}
|
||||
|
||||
// 使用GORM的Save方法(自动判断是否存在,存在则更新,不存在则插入)
|
||||
result := db.Save(&option)
|
||||
return result.Error
|
||||
}
|
||||
Reference in New Issue
Block a user