refactor: Update service and repository methods to use context

- 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.
This commit is contained in:
lan
2025-12-03 15:27:12 +08:00
parent 4824a997dd
commit 0bcd9336c4
32 changed files with 833 additions and 497 deletions

View File

@@ -2,6 +2,7 @@ package repository
import (
"carrotskin/internal/model"
"context"
"gorm.io/gorm"
)
@@ -16,28 +17,28 @@ func NewSystemConfigRepository(db *gorm.DB) SystemConfigRepository {
return &systemConfigRepository{db: db}
}
func (r *systemConfigRepository) GetByKey(key string) (*model.SystemConfig, error) {
func (r *systemConfigRepository) GetByKey(ctx context.Context, key string) (*model.SystemConfig, error) {
var config model.SystemConfig
err := r.db.Where("key = ?", key).First(&config).Error
err := r.db.WithContext(ctx).Where("key = ?", key).First(&config).Error
return handleNotFoundResult(&config, err)
}
func (r *systemConfigRepository) GetPublic() ([]model.SystemConfig, error) {
func (r *systemConfigRepository) GetPublic(ctx context.Context) ([]model.SystemConfig, error) {
var configs []model.SystemConfig
err := r.db.Where("is_public = ?", true).Find(&configs).Error
err := r.db.WithContext(ctx).Where("is_public = ?", true).Find(&configs).Error
return configs, err
}
func (r *systemConfigRepository) GetAll() ([]model.SystemConfig, error) {
func (r *systemConfigRepository) GetAll(ctx context.Context) ([]model.SystemConfig, error) {
var configs []model.SystemConfig
err := r.db.Find(&configs).Error
err := r.db.WithContext(ctx).Find(&configs).Error
return configs, err
}
func (r *systemConfigRepository) Update(config *model.SystemConfig) error {
return r.db.Save(config).Error
func (r *systemConfigRepository) Update(ctx context.Context, config *model.SystemConfig) error {
return r.db.WithContext(ctx).Save(config).Error
}
func (r *systemConfigRepository) UpdateValue(key, value string) error {
return r.db.Model(&model.SystemConfig{}).Where("key = ?", key).Update("value", value).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
}