mini_user_follow_logic.go
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package user
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserFollowLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserFollowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserFollowLogic {
return &MiniUserFollowLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserFollowLogic) MiniUserFollow(req *types.FollowRequest) (err error) {
var (
conn = l.svcCtx.DefaultDBConn()
user *domain.User
targetUser *domain.User
userFollow *domain.UserFollow
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
return xerr.NewErrMsgErr("用户不存在", err)
}
if targetUser, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId); err != nil {
return xerr.NewErrMsgErr("关注的用户不存在", err)
}
if userFollow, err = l.svcCtx.UserFollowRepository.FindOneUserFollowing(l.ctx, conn, user.Id, targetUser.Id); err == nil && userFollow != nil {
return xerr.NewErrMsgErr("用户已关注", err)
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
if err = user.Follow(targetUser); err != nil {
return err
}
if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil {
return err
}
if targetUser, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, targetUser); err != nil {
return err
}
userFollow = &domain.UserFollow{
FromUserId: user.Id,
ToUserId: targetUser.Id,
LastReadAt: time.Now().Unix(),
}
if userFollow, err = l.svcCtx.UserFollowRepository.Insert(ctx, conn, userFollow); err != nil {
return err
}
return nil
}, true); err != nil {
return xerr.NewErrMsgErr("关注用户失败", err)
}
return nil
}