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:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user