| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package logic
- import (
- "context"
- "slowwild/internal/svc"
- "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
- "github.com/zeromicro/go-zero/core/logx"
- "gorm.io/gorm"
- "slowwild/internal/errorx"
- "slowwild/internal/model"
- )
- type UserFollowLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewUserFollowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserFollowLogic {
- return &UserFollowLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- // 关注用户
- func (l *UserFollowLogic) UserFollow(in *slowwildserver.FollowUserReq) (*slowwildserver.FollowUserRes, error) {
- if in.UserId <= 0 || in.FollowUserId <= 0 {
- return nil, errorx.ErrInvalidParam
- }
- // 检查是否已经关注
- followMap, err := l.svcCtx.UserFollowModel.CheckMutualFollows(l.ctx, in.UserId, []int64{in.FollowUserId})
- if err != nil {
- return nil, errorx.ErrUserQueryFailed
- }
- if followMap[in.FollowUserId] {
- return nil, errorx.NewCodeError(10010, "已经关注该用户")
- }
- // 开启事务
- err = l.svcCtx.UserModel.Transaction(l.ctx, func(tx *gorm.DB) error {
- userModel := model.NewUserModel(tx)
- userFollowModel := model.NewUserFollowModel(tx)
- // 创建关注关系
- err := userFollowModel.Follow(l.ctx, in.UserId, in.FollowUserId)
- if err != nil {
- return err
- }
- // 增加当前用户的关注数
- err = userModel.IncrementFollowCount(l.ctx, in.UserId)
- if err != nil {
- return err
- }
- // 增加目标用户的粉丝数
- err = userModel.IncrementFansCount(l.ctx, in.FollowUserId)
- if err != nil {
- return err
- }
- return nil
- })
- if err != nil {
- l.Logger.Errorf("关注用户失败: %v", err)
- return nil, errorx.NewCodeError(10011, "关注用户失败")
- }
- return &slowwildserver.FollowUserRes{}, nil
- }
|