mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 11:06:23 +00:00
- Replaced static config with dynamic site info context. - Updated color scheme handling in various components to use site info. - Removed deprecated config file and integrated site info fetching. - Enhanced user preference page to allow color scheme selection. - Adjusted blog and console components to reflect new site info structure. - Improved error handling and fallback mechanisms for site info retrieval.
51 lines
868 B
Go
51 lines
868 B
Go
package repo
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/snowykami/neo-blog/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type kvRepo struct{}
|
|
|
|
var KV = &kvRepo{}
|
|
|
|
func (k *kvRepo) GetKV(key string, defaultValue ...any) (any, error) {
|
|
var kv = &model.KV{}
|
|
err := GetDB().First(kv, "key = ?", key).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
if len(defaultValue) > 0 {
|
|
return defaultValue[0], nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if kv.Value == nil {
|
|
if len(defaultValue) > 0 {
|
|
return defaultValue[0], nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
if v, ok := kv.Value["value"]; ok {
|
|
return v, nil
|
|
}
|
|
|
|
if len(defaultValue) > 0 {
|
|
return defaultValue[0], nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (k *kvRepo) SetKV(key string, value any) error {
|
|
kv := &model.KV{
|
|
Key: key,
|
|
Value: map[string]any{"value": value},
|
|
}
|
|
return GetDB().Save(kv).Error
|
|
}
|