Files
neo-blog/internal/repo/kv.go
Snowykami f501948f91 Refactor site configuration and color scheme management
- 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.
2025-09-26 00:25:34 +08:00

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
}