FollowUser 关注用户,并更新相关计数器
(ctx context.Context, followerID, followeeID int64)
| 94 | |
| 95 | // FollowUser 关注用户,并更新相关计数器 |
| 96 | func (r *relationDAO) FollowUser(ctx context.Context, followerID, followeeID int64) error { |
| 97 | now := r.getCurrentTime() |
| 98 | |
| 99 | err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 100 | // 创建Relation记录 |
| 101 | newRelation := &Relation{ |
| 102 | FollowerID: followerID, |
| 103 | FolloweeID: followeeID, |
| 104 | Status: FollowStatus, |
| 105 | CreatedAt: now, |
| 106 | UpdatedAt: now, |
| 107 | } |
| 108 | if err := tx.Create(newRelation).Error; err != nil { |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | // 更新关注者的关注数 |
| 113 | if err := r.updateRelationCount(tx, followerID, "followee_count", 1, now); err != nil { |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | // 更新被关注者的粉丝数 |
| 118 | if err := r.updateRelationCount(tx, followeeID, "follower_count", 1, now); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | return nil |
| 123 | }) |
| 124 | |
| 125 | if err != nil { |
| 126 | r.l.Error("failed to follow user", zap.Error(err)) |
| 127 | return err |
| 128 | } |
| 129 | |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | // CancelFollowUser 取消关注用户,并更新相关计数器 |
| 134 | func (r *relationDAO) CancelFollowUser(ctx context.Context, followerID, followeeID int64) error { |
nothing calls this directly
no test coverage detected