- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles. - Updated handlers to utilize context in method calls, improving error handling and performance. - Cleaned up Dockerfile by removing unnecessary whitespace.
45 lines
1.4 KiB
Go
45 lines
1.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// systemConfigRepository SystemConfigRepository的实现
|
|
type systemConfigRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewSystemConfigRepository 创建SystemConfigRepository实例
|
|
func NewSystemConfigRepository(db *gorm.DB) SystemConfigRepository {
|
|
return &systemConfigRepository{db: db}
|
|
}
|
|
|
|
func (r *systemConfigRepository) GetByKey(ctx context.Context, key string) (*model.SystemConfig, error) {
|
|
var config model.SystemConfig
|
|
err := r.db.WithContext(ctx).Where("key = ?", key).First(&config).Error
|
|
return handleNotFoundResult(&config, err)
|
|
}
|
|
|
|
func (r *systemConfigRepository) GetPublic(ctx context.Context) ([]model.SystemConfig, error) {
|
|
var configs []model.SystemConfig
|
|
err := r.db.WithContext(ctx).Where("is_public = ?", true).Find(&configs).Error
|
|
return configs, err
|
|
}
|
|
|
|
func (r *systemConfigRepository) GetAll(ctx context.Context) ([]model.SystemConfig, error) {
|
|
var configs []model.SystemConfig
|
|
err := r.db.WithContext(ctx).Find(&configs).Error
|
|
return configs, err
|
|
}
|
|
|
|
func (r *systemConfigRepository) Update(ctx context.Context, config *model.SystemConfig) error {
|
|
return r.db.WithContext(ctx).Save(config).Error
|
|
}
|
|
|
|
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
|
|
}
|