2025-11-28 23:30:49 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"carrotskin/internal/model"
|
2025-12-03 15:27:12 +08:00
|
|
|
"context"
|
2025-12-02 22:52:33 +08:00
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2025-11-28 23:30:49 +08:00
|
|
|
)
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
// systemConfigRepository SystemConfigRepository的实现
|
|
|
|
|
type systemConfigRepository struct {
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewSystemConfigRepository 创建SystemConfigRepository实例
|
|
|
|
|
func NewSystemConfigRepository(db *gorm.DB) SystemConfigRepository {
|
|
|
|
|
return &systemConfigRepository{db: db}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *systemConfigRepository) GetByKey(ctx context.Context, key string) (*model.SystemConfig, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var config model.SystemConfig
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("key = ?", key).First(&config).Error
|
2025-12-02 22:52:33 +08:00
|
|
|
return handleNotFoundResult(&config, err)
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *systemConfigRepository) GetPublic(ctx context.Context) ([]model.SystemConfig, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var configs []model.SystemConfig
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("is_public = ?", true).Find(&configs).Error
|
2025-12-02 10:33:19 +08:00
|
|
|
return configs, err
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *systemConfigRepository) GetAll(ctx context.Context) ([]model.SystemConfig, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var configs []model.SystemConfig
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Find(&configs).Error
|
2025-12-02 10:33:19 +08:00
|
|
|
return configs, err
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *systemConfigRepository) Update(ctx context.Context, config *model.SystemConfig) error {
|
|
|
|
|
return r.db.WithContext(ctx).Save(config).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *systemConfigRepository) UpdateValue(ctx context.Context, key, value string) error {
|
|
|
|
|
return r.db.WithContext(ctx).Model(&model.SystemConfig{}).Where("key = ?", key).Update("value", value).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|