fix: Service 层移除直接持有的 *gorm.DB,引入 Repository Transaction 方法

- ReactionStore/FollowStore/EnergyStore/FavoriteStore 接口新增 Transaction 方法
- EnergyStore.Transaction 支持跨仓库事务(EnergyStore + FundStore)
- ReactionRepo/FollowRepo/FavoriteRepo/EnergyRepo 实现 Transaction 方法
- ReactionRepo 新增 GetPostLikesCount 方法,消除 Service 直查 DB
- ReactionService/FollowService/FavoriteService/EnergyService 移除 db 字段
- EnergyRepo 通过 SetFundStore 注入 FundStore 用于跨仓库事务
- 更新 deps_extra.go 构造函数调用
This commit is contained in:
2026-06-02 16:46:44 +08:00
parent 1cb7a832f1
commit 3df7b2e672
14 changed files with 94 additions and 92 deletions

View File

@ -12,7 +12,8 @@ import (
// EnergyRepo 域能数据访问
type EnergyRepo struct {
db *gorm.DB
db *gorm.DB
fundStore service.FundStore // 用于 Transaction 时创建事务范围的 FundStore
}
// NewEnergyRepo 构造函数
@ -20,11 +21,28 @@ func NewEnergyRepo(db *gorm.DB) *EnergyRepo {
return &EnergyRepo{db: db}
}
// SetFundStore 设置公户仓储引用(用于跨仓库事务)
func (r *EnergyRepo) SetFundStore(s service.FundStore) {
r.fundStore = s
}
// WithTx 基于给定事务连接创建新的 EnergyRepo确保事务内操作原子性
func (r *EnergyRepo) WithTx(tx *gorm.DB) service.EnergyStore {
return &EnergyRepo{db: tx}
}
// Transaction 在事务内执行业务逻辑,自动管理 EnergyStore 和 FundStore 的事务范围
func (r *EnergyRepo) Transaction(fn func(txEnergy service.EnergyStore, txFund service.FundStore) error) error {
return r.db.Transaction(func(tx *gorm.DB) error {
txEnergy := r.WithTx(tx)
var txFund service.FundStore
if r.fundStore != nil {
txFund = r.fundStore.WithTx(tx)
}
return fn(txEnergy, txFund)
})
}
// FindUserByID 查找用户(获取能量余额等)
func (r *EnergyRepo) FindUserByID(userID uint) (*model.User, error) {
var user model.User

View File

@ -22,6 +22,13 @@ func (r *FavoriteRepo) WithTx(tx *gorm.DB) service.FavoriteStore {
return &FavoriteRepo{db: tx}
}
// Transaction 在事务内执行业务逻辑
func (r *FavoriteRepo) Transaction(fn func(tx service.FavoriteStore) error) error {
return r.db.Transaction(func(tx *gorm.DB) error {
return fn(r.WithTx(tx))
})
}
// ======================== Folder CRUD ========================
// CreateFolder 创建收藏夹

View File

@ -22,6 +22,13 @@ func (r *FollowRepo) WithTx(tx *gorm.DB) service.FollowStore {
return &FollowRepo{db: tx}
}
// Transaction 在事务内执行业务逻辑
func (r *FollowRepo) Transaction(fn func(tx service.FollowStore) error) error {
return r.db.Transaction(func(tx *gorm.DB) error {
return fn(r.WithTx(tx))
})
}
// Create 创建关注
func (r *FollowRepo) Create(follow *model.UserFollow) error {
return r.db.Create(follow).Error

View File

@ -22,6 +22,22 @@ func (r *ReactionRepo) WithTx(tx *gorm.DB) service.ReactionStore {
return &ReactionRepo{db: tx}
}
// Transaction 在事务内执行业务逻辑
func (r *ReactionRepo) Transaction(fn func(tx service.ReactionStore) error) error {
return r.db.Transaction(func(tx *gorm.DB) error {
return fn(r.WithTx(tx))
})
}
// GetPostLikesCount 查询文章点赞数
func (r *ReactionRepo) GetPostLikesCount(postID uint) (int, error) {
var post model.Post
if err := r.db.Select("likes_count").First(&post, postID).Error; err != nil {
return 0, err
}
return post.LikesCount, nil
}
// FindLike 查找点赞记录
func (r *ReactionRepo) FindLike(userID, postID uint) (*model.PostLike, error) {
var like model.PostLike