From 48a7eea0f9dfa3c7f2f739bd0cbd9a3910125110 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Sat, 25 Apr 2026 17:10:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Worker=20=E9=98=9F=E5=88=97=E8=A1=A5?= =?UTF-8?q?=E5=85=85=20DLX=20args=20+=20GetAllComments=20=E5=8A=A0?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E5=92=8C=E5=88=86=E9=A1=B5=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/cmd/worker/main.go | 9 ++- backend/internal/video/comment_repo.go | 106 +++++++++++++------------ 2 files changed, 60 insertions(+), 55 deletions(-) diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index de91005..f67875c 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -9,6 +9,7 @@ import ( "feedsystem_video_go/internal/social" "feedsystem_video_go/internal/video" "feedsystem_video_go/internal/worker" + mqrabbit "feedsystem_video_go/internal/middleware/rabbitmq" "log" "os" "os/signal" @@ -194,7 +195,7 @@ func declareSocialTopology(ch *amqp.Channel) error { false, false, false, - nil, + amqp.Table{"x-dead-letter-exchange": mqrabbit.DLXExchange}, ) if err != nil { return err @@ -231,7 +232,7 @@ func declarePopularityTopology(ch *amqp.Channel) error { false, false, false, - nil, + amqp.Table{"x-dead-letter-exchange": mqrabbit.DLXExchange}, ) if err != nil { return err @@ -265,7 +266,7 @@ func declareLikeTopology(ch *amqp.Channel) error { false, false, false, - nil, + amqp.Table{"x-dead-letter-exchange": mqrabbit.DLXExchange}, ) if err != nil { return err @@ -299,7 +300,7 @@ func declareCommentTopology(ch *amqp.Channel) error { false, false, false, - nil, + amqp.Table{"x-dead-letter-exchange": mqrabbit.DLXExchange}, ) if err != nil { return err diff --git a/backend/internal/video/comment_repo.go b/backend/internal/video/comment_repo.go index aa010bd..7de1515 100644 --- a/backend/internal/video/comment_repo.go +++ b/backend/internal/video/comment_repo.go @@ -1,51 +1,55 @@ -package video - -import ( - "context" - - "gorm.io/gorm" -) - -type CommentRepository struct { - db *gorm.DB -} - -func NewCommentRepository(db *gorm.DB) *CommentRepository { - return &CommentRepository{db: db} -} - -func (r *CommentRepository) CreateComment(ctx context.Context, comment *Comment) error { - return r.db.WithContext(ctx).Create(comment).Error -} - -func (r *CommentRepository) DeleteComment(ctx context.Context, comment *Comment) error { - return r.db.WithContext(ctx).Delete(comment).Error -} - -func (r *CommentRepository) GetAllComments(ctx context.Context, videoID uint) ([]Comment, error) { - var comments []Comment - err := r.db.WithContext(ctx).Where("video_id = ?", videoID).Find(&comments).Error - return comments, err -} - -func (r *CommentRepository) IsExist(ctx context.Context, id uint) (bool, error) { - var comment Comment - if err := r.db.WithContext(ctx).First(&comment, id).Error; err != nil { - if err == gorm.ErrRecordNotFound { - return false, nil - } - return false, err - } - return true, nil -} - -func (r *CommentRepository) GetByID(ctx context.Context, id uint) (*Comment, error) { - var comment Comment - if err := r.db.WithContext(ctx).First(&comment, id).Error; err != nil { - if err == gorm.ErrRecordNotFound { - return nil, nil - } - return nil, err - } - return &comment, nil -} +package video + +import ( + "context" + + "gorm.io/gorm" +) + +type CommentRepository struct { + db *gorm.DB +} + +func NewCommentRepository(db *gorm.DB) *CommentRepository { + return &CommentRepository{db: db} +} + +func (r *CommentRepository) CreateComment(ctx context.Context, comment *Comment) error { + return r.db.WithContext(ctx).Create(comment).Error +} + +func (r *CommentRepository) DeleteComment(ctx context.Context, comment *Comment) error { + return r.db.WithContext(ctx).Delete(comment).Error +} + +func (r *CommentRepository) GetAllComments(ctx context.Context, videoID uint) ([]Comment, error) { + var comments []Comment + err := r.db.WithContext(ctx). + Where("video_id = ?", videoID). + Order("created_at asc"). + Limit(200). + Find(&comments).Error + return comments, err +} + +func (r *CommentRepository) IsExist(ctx context.Context, id uint) (bool, error) { + var comment Comment + if err := r.db.WithContext(ctx).First(&comment, id).Error; err != nil { + if err == gorm.ErrRecordNotFound { + return false, nil + } + return false, err + } + return true, nil +} + +func (r *CommentRepository) GetByID(ctx context.Context, id uint) (*Comment, error) { + var comment Comment + if err := r.db.WithContext(ctx).First(&comment, id).Error; err != nil { + if err == gorm.ErrRecordNotFound { + return nil, nil + } + return nil, err + } + return &comment, nil +}