/
favorite.go
160 lines (138 loc) · 5.09 KB
/
favorite.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package mysql
import (
"context"
"github.com/132982317/profstik/pkg/errno"
"gorm.io/gorm"
)
type FavoriteVideoRelation struct {
Video Video `gorm:"foreignkey:VideoID;" json:"video,omitempty"`
VideoID uint `gorm:"index:idx_videoid;not null" json:"video_id"`
User User `gorm:"foreignkey:UserID;" json:"user,omitempty"`
UserID uint `gorm:"index:idx_userid;not null" json:"user_id"`
}
type FavoriteCommentRelation struct {
Comment Comment `gorm:"foreignkey:CommentID;" json:"comment,omitempty"`
CommentID uint `gorm:"column:comment_id;index:idx_commentid;not null" json:"video_id"`
User User `gorm:"foreignkey:UserID;" json:"user,omitempty"`
UserID uint `gorm:"column:user_id;index:idx_userid;not null" json:"user_id"`
}
func (FavoriteVideoRelation) TableName() string {
return "user_favorite_videos"
}
func (FavoriteCommentRelation) TableName() string {
return "user_favorite_comments"
}
func CreateVideoFavorite(ctx context.Context, userID int64, videoID int64, authorID int64) error {
err := GetDB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
//1. 新增点赞数据
err := tx.Create(&FavoriteVideoRelation{UserID: uint(userID), VideoID: uint(videoID)}).Error
if err != nil {
return err
}
//2.改变 video 表中的 favorite count
res := tx.Model(&Video{}).Where("id = ?", videoID).Update("favorite_count", gorm.Expr("favorite_count + ?", 1))
if res.Error != nil {
return res.Error
}
if res.RowsAffected != 1 {
// 影响的数据条数不是1
return errno.ErrDatabase
}
//3.改变 user 表中的 favorite count
res = tx.Model(&User{}).Where("id = ?", userID).Update("favorite_count", gorm.Expr("favorite_count + ?", 1))
if res.Error != nil {
return err
}
if res.RowsAffected != 1 {
return errno.ErrDatabase
}
//4.改变 user 表中的 total_favorited
res = tx.Model(&User{}).Where("id = ?", authorID).Update("total_favorited", gorm.Expr("total_favorited + ?", 1))
if res.Error != nil {
return err
}
if res.RowsAffected != 1 {
return errno.ErrDatabase
}
return nil
})
return err
}
func GetFavoriteVideoRelationByUserVideoID(ctx context.Context, userID int64, videoID int64) (*FavoriteVideoRelation, error) {
FavoriteVideoRelation := new(FavoriteVideoRelation)
if err := GetDB().WithContext(ctx).First(&FavoriteVideoRelation, "user_id = ? and video_id = ?", userID, videoID).Error; err == nil {
return FavoriteVideoRelation, nil
} else if err == gorm.ErrRecordNotFound {
return nil, nil
} else {
return nil, err
}
}
func DelFavoriteByUserVideoID(ctx context.Context, userID int64, videoID int64, authorID int64) error {
err := GetDB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// 在事务中执行一些 db 操作(从这里开始,您应该使用 'tx' 而不是 'db')
FavoriteVideoRelation := new(FavoriteVideoRelation)
if err := tx.Where("user_id = ? and video_id = ?", userID, videoID).First(&FavoriteVideoRelation).Error; err != nil {
return err
} else if err == gorm.ErrRecordNotFound {
return nil
}
//1. 删除点赞数据
err := tx.Unscoped().Where("user_id = ? and video_id = ?", userID, videoID).Delete(&FavoriteVideoRelation).Error
if err != nil {
return err
}
//2.改变 video 表中的 favorite count
res := tx.Model(&Video{}).Where("id = ?", videoID).Update("favorite_count", gorm.Expr("favorite_count - ?", 1))
if res.Error != nil {
return res.Error
}
if res.RowsAffected != 1 {
// 影响数据条数不是1
return errno.ErrDatabase
}
//3.改变 user 表中的 favorite count
res = tx.Model(&User{}).Where("id = ?", userID).Update("favorite_count", gorm.Expr("favorite_count - ?", 1))
if res.Error != nil {
return err
}
if res.RowsAffected != 1 {
return errno.ErrDatabase
}
//4.改变 user 表中的 total_favorited
res = tx.Model(&User{}).Where("id = ?", authorID).Update("total_favorited", gorm.Expr("total_favorited - ?", 1))
if res.Error != nil {
return err
}
if res.RowsAffected != 1 {
return errno.ErrDatabase
}
return nil
})
return err
}
func GetFavoriteListByUserID(ctx context.Context, userID int64) ([]*FavoriteVideoRelation, error) {
var FavoriteVideoRelationList []*FavoriteVideoRelation
err := GetDB().WithContext(ctx).Where("user_id = ?", userID).Find(&FavoriteVideoRelationList).Error
if err != nil {
return nil, err
}
return FavoriteVideoRelationList, nil
}
func GetAllFavoriteList(ctx context.Context) ([]*FavoriteVideoRelation, error) {
var FavoriteVideoRelationList []*FavoriteVideoRelation
if err := GetDB().WithContext(ctx).Find(&FavoriteVideoRelationList).Error; err != nil {
return nil, err
}
return FavoriteVideoRelationList, nil
}
func GetFavoriteCommentRelationByUserCommentID(ctx context.Context, userID int64, commentID int64) (*FavoriteCommentRelation, error) {
FavoriteCommentRelation := new(FavoriteCommentRelation)
if err := GetDB().WithContext(ctx).First(&FavoriteCommentRelation, "user_id = ? and comment_id = ?", userID, commentID).Error; err == nil {
return FavoriteCommentRelation, nil
} else if err == gorm.ErrRecordNotFound {
return nil, nil
} else {
return nil, err
}
}