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

@ -113,6 +113,8 @@ type EnergyStore interface {
ListAllEnergyLogs(energyType string, offset, limit int) ([]model.EnergyLog, int64, error)
// WithTx 基于给定事务连接创建新的 EnergyStore确保事务内操作原子性
WithTx(tx *gorm.DB) EnergyStore
// Transaction 在事务内执行业务逻辑,自动管理 WithTx 和 FundStore 的事务范围
Transaction(fn func(txEnergy EnergyStore, txFund FundStore) error) error
}
// energyStore 向后兼容别名
@ -172,10 +174,13 @@ type ReactionStore interface {
IncrPostLikes(postID uint, delta int) error
IncrPostDislikes(postID uint, delta int) error
GetPostAuthorID(postID uint) (uint, error)
GetPostLikesCount(postID uint) (int, error)
UpsertDailyLikeSummary(authorUID uint, date string, likerUID string) error
GetUnnotifiedSummaries(userID uint) ([]model.DailyLikeSummary, error)
MarkSummaryNotified(id uint) error
WithTx(tx *gorm.DB) ReactionStore
// Transaction 在事务内执行业务逻辑,自动管理 WithTx
Transaction(fn func(tx ReactionStore) error) error
}
// FollowStore FollowService 所需的最小仓储接口ISP
@ -191,6 +196,8 @@ type FollowStore interface {
IncrFollowingCount(userID uint, delta int) error
GetFollowListPublic(userID uint) (bool, error)
WithTx(tx *gorm.DB) FollowStore
// Transaction 在事务内执行业务逻辑,自动管理 WithTx
Transaction(fn func(tx FollowStore) error) error
}
// TrendPoint 趋势数据点