Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions dal/dao/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ import (
type Comment struct {
}

func (c *Comment) QueryCommentList(ctx context.Context, vid int64) ([]*daoModel.Comment, error) {
res := make([]*daoModel.Comment, 0)
if err := db.WithContext(ctx).
Model(daoModel.Comment{}).
Where("vid = ?", vid).Find(&res).Error; err != nil {
func (c *Comment) QueryCommentList(ctx context.Context, vid int64) (res []*daoModel.Comment, err error) {
res = make([]*daoModel.Comment, 0)
err = db.WithContext(ctx).Model(daoModel.Comment{}).Where("vid = ?", vid).Find(&res).Error
if err != nil {
Log.Errorf("select comment list err: %v, videoId: %d", err, vid)
return nil, err
}
return res, nil
return
}

func (c *Comment) AddComment(ctx context.Context, req *interaction.DouyinCommentActionRequest) (commentRet KitexModel.Comment, err error) {
func (c *Comment) AddComment(ctx context.Context, req *interaction.DouyinCommentActionRequest) (commentRet *KitexModel.Comment, err error) {
text := util.SensitiveMatch(*req.CommentText)
comment := daoModel.Comment{
Vid: req.VideoId,
Expand Down Expand Up @@ -55,7 +53,7 @@ func (c *Comment) AddComment(ctx context.Context, req *interaction.DouyinComment
FollowCount: &user.FollowCount,
FollowerCount: &user.FollowerCount,
}
commentRet = KitexModel.Comment{
commentRet = &KitexModel.Comment{
Id: comment.ID,
User: &userRet,
Content: text,
Expand Down
11 changes: 7 additions & 4 deletions dal/dao/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ type Like struct {

func (l *Like) GetFavoriteVideoListByUserId(ctx context.Context, id int64) (videoList []model.Video, err error) {
var userLikes []model.Like
videoList = make([]model.Video, 0)
// 根据 uid = id 和 action = 1 找到 对应的 vid
if err = db.WithContext(ctx).Model(model.Like{}).Select("vid").Where("uid = ? AND action = ?", id, 1).Find(&userLikes).Error; err != nil {
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
// 该用户没有点赞过的视频,不影响返回结果
Log.Infof("GetFavoriteVideoListByUserId err: %v", err)
return videoList, nil
Expand All @@ -31,9 +30,9 @@ func (l *Like) GetFavoriteVideoListByUserId(ctx context.Context, id int64) (vide
for _, userLike := range userLikes {
var video model.Video
if err = db.WithContext(ctx).Model(model.Video{}).Omit("created_at, updated_at, deleted_at").Where("id = ?", userLike.Vid).First(&video).Error; err != nil {
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
// 某一个点赞的视频未找到,不影响返回结果
Log.Infof("%v, video id: %d", err, userLike.Vid)
Log.Infof("%v, video not found, vid: %d", err, userLike.Vid)
continue
} else {
// 数据库出错
Expand Down Expand Up @@ -76,10 +75,12 @@ func (l *Like) CreateRecord(ctx context.Context, record *model.Like) (err error)
if err = tx.WithContext(ctx).Create(&record).Error; err != nil {
Log.Errorf("create record err: %v, uid: %v, vid: %v", err, record.UID, record.Vid)
tx.Rollback()
return
}
if err = tx.WithContext(ctx).Model(model.Video{}).Where("id = ?", record.Vid).Update("favorite_count", gorm.Expr("favorite_count + 1")).Error; err != nil {
Log.Errorf("update video favorite count err: %v, vid: %v", err, record.Vid)
tx.Rollback()
return
}
tx.Commit()
return
Expand All @@ -90,6 +91,7 @@ func (l *Like) UpdateRecord(ctx context.Context, record *model.Like) (err error)
if err = tx.WithContext(ctx).Save(&record).Error; err != nil {
Log.Errorf("update record err: %v, uid: %v, vid: %v", err, record.UID, record.Vid)
tx.Rollback()
return
}
count := 1
if !record.Action {
Expand All @@ -98,6 +100,7 @@ func (l *Like) UpdateRecord(ctx context.Context, record *model.Like) (err error)
if err = tx.WithContext(ctx).Model(model.Video{}).Where("id = ?", record.Vid).Update("favorite_count", gorm.Expr("favorite_count + ?", count)).Error; err != nil {
Log.Errorf("update video favorite count err: %v, vid: %v", err, record.Vid)
tx.Rollback()
return
}
tx.Commit()
return
Expand Down
3 changes: 2 additions & 1 deletion dal/dao/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"ByteTech-7355608/douyin-server/dal/dao/model"
. "ByteTech-7355608/douyin-server/pkg/configs"
"context"
"errors"

"gorm.io/gorm"
)
Expand All @@ -17,7 +18,7 @@ func (m *Message) GetLastMessageByUid(ctx context.Context, uida, uidb int64) (ms
tx.Where("uid = ? AND to_uid = ?", uida, uidb)
tx.Or("uid = ? AND to_uid = ?", uidb, uida)
if err = tx.Order("created_at desc").First(&msg).Error; err != nil {
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
return msg, nil
} else {
Log.Errorf("get last message by uid err : %v, from_id %v, to_id %v", err, uida, uidb)
Expand Down
14 changes: 7 additions & 7 deletions dal/dao/model/comment.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions dal/dao/model/like.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions dal/dao/model/message.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions dal/dao/model/relation.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 15 additions & 16 deletions dal/dao/model/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions dal/dao/model/video.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 8 additions & 26 deletions dal/dao/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,12 @@ import (
type Relation struct {
}

// IsUserFollowed 两个用户有是否关注 输入两个用户的Id a->b
func (r *Relation) IsUserFollowed(ctx context.Context, concernerID int64, concernedID int64) (isFollow bool, err error) {
relation := model.Relation{}
if err = db.WithContext(ctx).Model(model.Relation{}).Select("action").Where("concerner_id = ? AND concerned_id = ?", concernerID, concernedID).First(&relation).Error; err != nil {
if err == gorm.ErrRecordNotFound {
// relation 中不存在关注的关系,也可表示未关注
Log.Infof("IsUserFollowed err: %v, concerner_id: %d AND concerned_id: %d", err, concernerID, concernedID)
return false, nil
} else {
// 数据库出错
Log.Errorf("get follow relation err: %v, concerner_id: %d AND concerned_id: %d", err, concernerID, concernedID)
return false, err
}
}
return relation.Action, nil
}

// IsFollower a是否关注b
func (r *Relation) IsFollower(ctx context.Context, a, b int64) (follower bool, err error) {
var action bool
func (r *Relation) IsFollower(ctx context.Context, a, b int64) (action bool, err error) {
// TODO 建立唯一索引,提高查询效率
if err = db.WithContext(ctx).Model(model.Relation{}).Select("action").Where("concerner_id = ? AND concerned_id = ?", a, b).Find(&action).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Log.Infof("IsUserFollowed err: %v, concerner_id: %d AND concerned_id: %d", err, a, b)
return false, nil
}
Log.Errorf("query relation between %v and %v err: %v", a, b, err)
Expand All @@ -45,10 +28,9 @@ func (r *Relation) IsFollower(ctx context.Context, a, b int64) (follower bool, e
}

// GetFollowerListByUid 获取用户的粉丝id列表
func (r *Relation) GetFollowerListByUid(ctx context.Context, uid int64) (followeridlist []int64, err error) {
if err = db.WithContext(ctx).Model(model.Relation{}).Select("concerner_id").Where("concerned_id=? AND action = ?", uid, 1).Find(&followeridlist).Error; err != nil {
Log.Errorf("get followeridlist err: %v, userid:%v", err, uid)
return nil, err
func (r *Relation) GetFollowerListByUid(ctx context.Context, uid int64) (followerIdList []int64, err error) {
if err = db.WithContext(ctx).Model(model.Relation{}).Select("concerner_id").Where("concerned_id=? AND action = ?", uid, 1).Find(&followerIdList).Error; err != nil {
Log.Errorf("get followerIdList err: %v, userid:%v", err, uid)
}
return
}
Expand Down Expand Up @@ -99,7 +81,7 @@ func (r *Relation) UpdatedRelation(ctx context.Context, record *model.Relation,
// 查看关注列表
func (r *Relation) FollowList(ctx context.Context, id int64) (list []*model.User, err error) {
var user_ids []int64
err = db.Debug().Model(model.Relation{}).Select("concerned_id").Where("concerner_id=? AND action=1", id).Find(&user_ids).Error
err = db.WithContext(ctx).Model(model.Relation{}).Select("concerned_id").Where("concerner_id=? AND action=1", id).Find(&user_ids).Error
if err != nil {
Log.Errorf("get follow list fail,err:%v", err)
return
Expand All @@ -108,8 +90,8 @@ func (r *Relation) FollowList(ctx context.Context, id int64) (list []*model.User
var user *model.User
err = db.Where("id=?", i).First(&user).Error
if err != nil {
Log.Errorf("get userinfo fail,err:%v", err)
return
Log.Warnf("get userinfo fail,err:%v", err)
continue
}
list = append(list, user)
}
Expand Down
Loading