perf:改变写入逻辑
This commit is contained in:
@@ -22,6 +22,13 @@ func (vr *VideoRepository) CreateVideo(ctx context.Context, video *Video) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (vr *VideoRepository) CreateMsg(ctx context.Context, Msg *OutboxMsg) error {
|
||||||
|
if err := vr.db.WithContext(ctx).Create(Msg).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (vr *VideoRepository) DeleteVideo(ctx context.Context, id uint) error {
|
func (vr *VideoRepository) DeleteVideo(ctx context.Context, id uint) error {
|
||||||
if err := vr.db.WithContext(ctx).Delete(&Video{}, id).Error; err != nil {
|
if err := vr.db.WithContext(ctx).Delete(&Video{}, id).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import (
|
|||||||
|
|
||||||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||||||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VideoService struct {
|
type VideoService struct {
|
||||||
@@ -41,10 +43,28 @@ func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
|
|||||||
if video.CoverURL == "" {
|
if video.CoverURL == "" {
|
||||||
return errors.New("cover url is required")
|
return errors.New("cover url is required")
|
||||||
}
|
}
|
||||||
if err := vs.repo.CreateVideo(ctx, video); err != nil {
|
|
||||||
|
//事务保证视频写入库和消息写入本地消息表的一致性
|
||||||
|
err := vs.repo.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := tx.Create(video).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
Msg := OutboxMsg{
|
||||||
|
VideoID: video.ID,
|
||||||
|
EventType: "video_published",
|
||||||
|
Status: "pending",
|
||||||
|
CreateTime: video.CreateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Create(Msg).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vs *VideoService) Delete(ctx context.Context, id uint, authorID uint) error {
|
func (vs *VideoService) Delete(ctx context.Context, id uint, authorID uint) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user