diff --git a/dal/dao/comment.go b/dal/dao/comment.go index add3dd5..c1f9b9b 100644 --- a/dal/dao/comment.go +++ b/dal/dao/comment.go @@ -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, @@ -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, diff --git a/dal/dao/like.go b/dal/dao/like.go index fdfd910..ba01d22 100644 --- a/dal/dao/like.go +++ b/dal/dao/like.go @@ -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 @@ -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 { // 数据库出错 @@ -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 @@ -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 { @@ -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 diff --git a/dal/dao/message.go b/dal/dao/message.go index b48a871..447cd1d 100644 --- a/dal/dao/message.go +++ b/dal/dao/message.go @@ -4,6 +4,7 @@ import ( "ByteTech-7355608/douyin-server/dal/dao/model" . "ByteTech-7355608/douyin-server/pkg/configs" "context" + "errors" "gorm.io/gorm" ) @@ -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) diff --git a/dal/dao/model/comment.gen.go b/dal/dao/model/comment.gen.go index 4f374ca..b6d76e1 100644 --- a/dal/dao/model/comment.gen.go +++ b/dal/dao/model/comment.gen.go @@ -5,21 +5,21 @@ package model import ( - "time" "gorm.io/plugin/soft_delete" + "time" ) const TableNameComment = "comment" // Comment mapped from table type Comment struct { - ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` - Vid int64 `gorm:"column:vid;not null" json:"vid"` - UID int64 `gorm:"column:uid;not null" json:"uid"` - Content string `gorm:"column:content;not null" json:"content"` + Vid int64 `gorm:"column:vid;not null" json:"vid"` + UID int64 `gorm:"column:uid;not null" json:"uid"` + Content string `gorm:"column:content;not null" json:"content"` } // TableName Comment's table name diff --git a/dal/dao/model/like.gen.go b/dal/dao/model/like.gen.go index a4039e5..011c3d2 100644 --- a/dal/dao/model/like.gen.go +++ b/dal/dao/model/like.gen.go @@ -5,21 +5,21 @@ package model import ( - "time" "gorm.io/plugin/soft_delete" + "time" ) const TableNameLike = "like" // Like mapped from table type Like struct { - ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` - UID int64 `gorm:"column:uid" json:"uid"` - Vid int64 `gorm:"column:vid" json:"vid"` - Action bool `gorm:"column:action" json:"action"` + UID int64 `gorm:"column:uid" json:"uid"` + Vid int64 `gorm:"column:vid" json:"vid"` + Action bool `gorm:"column:action" json:"action"` } // TableName Like's table name diff --git a/dal/dao/model/message.gen.go b/dal/dao/model/message.gen.go index dddd59f..f8b44d6 100644 --- a/dal/dao/model/message.gen.go +++ b/dal/dao/model/message.gen.go @@ -5,21 +5,21 @@ package model import ( - "time" "gorm.io/plugin/soft_delete" + "time" ) const TableNameMessage = "message" // Message mapped from table type Message struct { - ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` - UID int64 `gorm:"column:uid" json:"uid"` - ToUID int64 `gorm:"column:to_uid" json:"to_uid"` - Content string `gorm:"column:content" json:"content"` + UID int64 `gorm:"column:uid" json:"uid"` + ToUID int64 `gorm:"column:to_uid" json:"to_uid"` + Content string `gorm:"column:content" json:"content"` } // TableName Message's table name diff --git a/dal/dao/model/relation.gen.go b/dal/dao/model/relation.gen.go index bc809b6..7107923 100644 --- a/dal/dao/model/relation.gen.go +++ b/dal/dao/model/relation.gen.go @@ -5,21 +5,21 @@ package model import ( - "time" "gorm.io/plugin/soft_delete" + "time" ) const TableNameRelation = "relation" // Relation mapped from table type Relation struct { - ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` - DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` - ConcernerID int64 `gorm:"column:concerner_id;not null" json:"concerner_id"` - ConcernedID int64 `gorm:"column:concerned_id;not null" json:"concerned_id"` - Action bool `gorm:"column:action" json:"action"` + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` + ConcernerID int64 `gorm:"column:concerner_id;not null" json:"concerner_id"` + ConcernedID int64 `gorm:"column:concerned_id;not null" json:"concerned_id"` + Action bool `gorm:"column:action" json:"action"` } // TableName Relation's table name diff --git a/dal/dao/model/user.gen.go b/dal/dao/model/user.gen.go index 508e3b9..8774a7b 100644 --- a/dal/dao/model/user.gen.go +++ b/dal/dao/model/user.gen.go @@ -5,29 +5,28 @@ package model import ( - "time" "gorm.io/plugin/soft_delete" + "time" ) const TableNameUser = "user" // User mapped from table type User struct { - ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` - DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` - Username string `gorm:"column:username;not null" json:"username"` - Password string `gorm:"column:password;not null" json:"password"` - FollowCount int64 `gorm:"column:follow_count" json:"follow_count"` - FollowerCount int64 `gorm:"column:follower_count" json:"follower_count"` - TotalFavorited int64 `gorm:"column:total_favorited" json:"total_favorited"` - WorkCount int64 `gorm:"column:work_count" json:"work_count"` - FavoriteCount int64 `gorm:"column:favorite_count" json:"favorite_count"` - Avatar string `gorm:"column:avatar" json:"avatar"` - Signature string `gorm:"column:signature" json:"signature"` - BackgroundImage string `gorm:"column:background_image" json:"background_image"` - + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` + Username string `gorm:"column:username;not null" json:"username"` + Password string `gorm:"column:password;not null" json:"password"` + FollowCount int64 `gorm:"column:follow_count" json:"follow_count"` + FollowerCount int64 `gorm:"column:follower_count" json:"follower_count"` + TotalFavorited int64 `gorm:"column:total_favorited" json:"total_favorited"` + WorkCount int64 `gorm:"column:work_count" json:"work_count"` + FavoriteCount int64 `gorm:"column:favorite_count" json:"favorite_count"` + Avatar string `gorm:"column:avatar" json:"avatar"` + Signature string `gorm:"column:signature" json:"signature"` + BackgroundImage string `gorm:"column:background_image" json:"background_image"` } // TableName User's table name diff --git a/dal/dao/model/video.gen.go b/dal/dao/model/video.gen.go index 76aaf2d..f5cf580 100644 --- a/dal/dao/model/video.gen.go +++ b/dal/dao/model/video.gen.go @@ -5,24 +5,24 @@ package model import ( - "time" "gorm.io/plugin/soft_delete" + "time" ) const TableNameVideo = "video" // Video mapped from table