refactor: 自定义业务错误
This commit is contained in:
@@ -2,35 +2,38 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/Wei-Shaw/sub2api/internal/model"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// SettingRepository 系统设置数据访问层
|
||||
type SettingRepository struct {
|
||||
type settingRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewSettingRepository 创建系统设置仓库实例
|
||||
func NewSettingRepository(db *gorm.DB) *SettingRepository {
|
||||
return &SettingRepository{db: db}
|
||||
func NewSettingRepository(db *gorm.DB) service.SettingRepository {
|
||||
return &settingRepository{db: db}
|
||||
}
|
||||
|
||||
// Get 根据Key获取设置值
|
||||
func (r *SettingRepository) Get(ctx context.Context, key string) (*model.Setting, error) {
|
||||
func (r *settingRepository) Get(ctx context.Context, key string) (*model.Setting, error) {
|
||||
var setting model.Setting
|
||||
err := r.db.WithContext(ctx).Where("key = ?", key).First(&setting).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, translatePersistenceError(err, service.ErrSettingNotFound, nil)
|
||||
}
|
||||
return &setting, nil
|
||||
}
|
||||
|
||||
// GetValue 获取设置值字符串
|
||||
func (r *SettingRepository) GetValue(ctx context.Context, key string) (string, error) {
|
||||
func (r *settingRepository) GetValue(ctx context.Context, key string) (string, error) {
|
||||
setting, err := r.Get(ctx, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -39,7 +42,7 @@ func (r *SettingRepository) GetValue(ctx context.Context, key string) (string, e
|
||||
}
|
||||
|
||||
// Set 设置值(存在则更新,不存在则创建)
|
||||
func (r *SettingRepository) Set(ctx context.Context, key, value string) error {
|
||||
func (r *settingRepository) Set(ctx context.Context, key, value string) error {
|
||||
setting := &model.Setting{
|
||||
Key: key,
|
||||
Value: value,
|
||||
@@ -53,7 +56,7 @@ func (r *SettingRepository) Set(ctx context.Context, key, value string) error {
|
||||
}
|
||||
|
||||
// GetMultiple 批量获取设置
|
||||
func (r *SettingRepository) GetMultiple(ctx context.Context, keys []string) (map[string]string, error) {
|
||||
func (r *settingRepository) GetMultiple(ctx context.Context, keys []string) (map[string]string, error) {
|
||||
var settings []model.Setting
|
||||
err := r.db.WithContext(ctx).Where("key IN ?", keys).Find(&settings).Error
|
||||
if err != nil {
|
||||
@@ -68,7 +71,7 @@ func (r *SettingRepository) GetMultiple(ctx context.Context, keys []string) (map
|
||||
}
|
||||
|
||||
// SetMultiple 批量设置值
|
||||
func (r *SettingRepository) SetMultiple(ctx context.Context, settings map[string]string) error {
|
||||
func (r *settingRepository) SetMultiple(ctx context.Context, settings map[string]string) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for key, value := range settings {
|
||||
setting := &model.Setting{
|
||||
@@ -88,7 +91,7 @@ func (r *SettingRepository) SetMultiple(ctx context.Context, settings map[string
|
||||
}
|
||||
|
||||
// GetAll 获取所有设置
|
||||
func (r *SettingRepository) GetAll(ctx context.Context) (map[string]string, error) {
|
||||
func (r *settingRepository) GetAll(ctx context.Context) (map[string]string, error) {
|
||||
var settings []model.Setting
|
||||
err := r.db.WithContext(ctx).Find(&settings).Error
|
||||
if err != nil {
|
||||
@@ -103,6 +106,6 @@ func (r *SettingRepository) GetAll(ctx context.Context) (map[string]string, erro
|
||||
}
|
||||
|
||||
// Delete 删除设置
|
||||
func (r *SettingRepository) Delete(ctx context.Context, key string) error {
|
||||
func (r *settingRepository) Delete(ctx context.Context, key string) error {
|
||||
return r.db.WithContext(ctx).Where("key = ?", key).Delete(&model.Setting{}).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user