-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.go
113 lines (97 loc) · 3.04 KB
/
follow.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package db
import (
"context"
"errors"
"github.com/benxinm/tiktok/pkg/constants"
"github.com/benxinm/tiktok/pkg/myerrors"
"gorm.io/gorm"
"time"
)
type Follow struct {
Id int64
UserId int64
ToUserId int64
Status int64 //保证数据不进行删除 0-关注 1-取关
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func IsFollow(ctx context.Context, uid int64, tuid int64) (bool, error) {
var model *Follow
err := DB.WithContext(ctx).Model(&Follow{}).Where("user_id= ? AND to_user_id = ?", uid, tuid).First(model).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { // db中也查不到
return false, gorm.ErrRecordNotFound
}
return false, err
}
return model.Status == 1, nil
}
func FollowAction(ctx context.Context, follow *Follow) error {
resp := new(Follow)
err := DB.WithContext(ctx).Model(&Follow{}).Where("user_id= ? AND to_user_id = ?", follow.UserId, follow.ToUserId).First(&resp).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
follow.Id = SF.NextVal()
follow.Status = 0
return DB.WithContext(ctx).Create(follow).Error
} else if err != nil {
return err
}
err = DB.WithContext(ctx).Model(&Follow{}).Where("user_id= ? AND to_user_id = ?", follow.UserId, follow.ToUserId).Update("status", 0).Error
if err != nil {
return err
}
return nil
}
func UnFollowAction(ctx context.Context, follow *Follow) error {
_, err := IsFollow(ctx, follow.UserId, follow.ToUserId)
if errors.Is(err, gorm.ErrRecordNotFound) {
return myerrors.FollowNotFoundError
} else if err != nil {
return err
}
err = DB.WithContext(ctx).Model(&Follow{}).Where("user_id= ? AND to_user_id = ?", follow.UserId, follow.ToUserId).Update("status", 1).Error
if err != nil {
return err
}
return nil
}
func FollowList(ctx context.Context, uid int64) ([]int64, error) {
var followList []int64
err := DB.WithContext(ctx).Table(constants.FollowTableName).Select("to_user_id").Where("user_id = ? AND status = ?", uid, 0).Find(&followList).Error
if err != nil {
return nil, err
}
if len(followList) == 0 { // db中也查不到
return nil, gorm.ErrRecordNotFound
}
return followList, nil
}
func FollowerList(ctx context.Context, uid int64) (*[]int64, error) {
var followerList []int64
err := DB.WithContext(ctx).Table(constants.FollowTableName).Select("user_id").Where("to_user_id = ? AND status = ?", uid, 0).Find(&followerList).Error
if err != nil {
return nil, err
}
if len(followerList) == 0 {
return nil, gorm.ErrRecordNotFound
}
return &followerList, nil
}
//TODO FriendList idea
func FollowCount(ctx context.Context, uid int64) (int64, error) {
var count int64
err := DB.WithContext(ctx).Model(&Follow{}).Where("user_id = ? AND status = ?", uid, 0).Count(&count).Error
if err != nil {
return -1, err
}
return count, nil
}
func FollowerCount(ctx context.Context, uid int64) (int64, error) {
var count int64
err := DB.WithContext(ctx).Model(&Follow{}).Where("to_user_id = ? AND status = ?", uid, 0).Count(&count).Error
if err != nil {
return -1, err
}
return count, nil
}