mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
943818f7d4
将 OpenFlare 与平台业务的数据访问从 model 与 apps 直连迁入 repository, model 仅保留实体与无 IO 规则;补充 code-check 架构守卫与开发规范。
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
db "github.com/Rain-kl/Wavelet/internal/infra/persistence"
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ListAdminSystemConfigs returns all configs, optionally filtered by type.
|
|
func ListAdminSystemConfigs(ctx context.Context, configType string) ([]model.SystemConfig, error) {
|
|
query := db.DB(ctx).Order("created_at DESC")
|
|
if configType != "" {
|
|
query = query.Where("type = ?", configType)
|
|
}
|
|
var configs []model.SystemConfig
|
|
if err := query.Find(&configs).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return configs, nil
|
|
}
|
|
|
|
// GetAdminSystemConfigByKey loads a config directly from PostgreSQL.
|
|
func GetAdminSystemConfigByKey(ctx context.Context, key string) (model.SystemConfig, error) {
|
|
var config model.SystemConfig
|
|
if err := db.DB(ctx).Where("key = ?", key).First(&config).Error; err != nil {
|
|
return model.SystemConfig{}, err
|
|
}
|
|
return config, nil
|
|
}
|
|
|
|
// SystemConfigExists reports whether a config key already exists.
|
|
func SystemConfigExists(ctx context.Context, key string) (bool, error) {
|
|
var existing model.SystemConfig
|
|
err := db.DB(ctx).Where("key = ?", key).First(&existing).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// CreateSystemConfig persists a new system config row.
|
|
func CreateSystemConfig(ctx context.Context, config *model.SystemConfig) error {
|
|
return db.DB(ctx).Create(config).Error
|
|
}
|
|
|
|
// UpdateSystemConfigFields applies partial updates to a system config row.
|
|
func UpdateSystemConfigFields(ctx context.Context, config *model.SystemConfig, updates map[string]any) error {
|
|
return UpdateSystemConfigFieldsTx(db.DB(ctx), config, updates)
|
|
}
|
|
|
|
// UpdateSystemConfigFieldsTx applies partial updates within an existing transaction.
|
|
func UpdateSystemConfigFieldsTx(tx *gorm.DB, config *model.SystemConfig, updates map[string]any) error {
|
|
return tx.Model(config).Updates(updates).Error
|
|
}
|
|
|
|
// SaveOrUpdateSystemConfig creates or updates a config row and invalidates cache.
|
|
func SaveOrUpdateSystemConfig(ctx context.Context, key, value string) error {
|
|
var sc model.SystemConfig
|
|
err := db.DB(ctx).Where("key = ?", key).First(&sc).Error
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return err
|
|
}
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
sc = model.SystemConfig{
|
|
Key: key,
|
|
Value: value,
|
|
Type: configTypeSystem,
|
|
Visibility: model.ConfigVisibilityHidden,
|
|
}
|
|
if err := db.DB(ctx).Create(&sc).Error; err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
sc.Value = value
|
|
if err := db.DB(ctx).Save(&sc).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return InvalidateSystemConfigCache(ctx, key)
|
|
}
|