Files
VLoop/backend/internal/video/comment_repo.go
huanghaosheng c210e75996 style(backend): 清理幂等写入路径的注释
注释只保留一行级约束。
长段理由移入设计文档。
验证:go test -count=1 ./... 全绿。
2026-10-31 00:03:53 +08:00

144 lines
3.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package video
import (
"context"
"time"
"feedsystem_video_go/internal/notification"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
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
}
// CreateIdempotentWithPopularity 由 uk_comments_author_key 判重。
// 热度增量与插入必须同事务,只在 created=true 时执行。
// 注意:DSN 不能开 clientFoundRows,否则 RowsAffected 恒为 1,判重失效。
func (r *CommentRepository) CreateIdempotentWithPopularity(ctx context.Context, c *Comment, delta int64) (bool, error) {
var created bool
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
res := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(c)
if res.Error != nil {
return res.Error
}
if res.RowsAffected == 0 {
return nil
}
created = true
if delta == 0 {
return nil
}
return tx.Model(&Video{}).
Where("id = ?", c.VideoID).
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + ?, 0)", delta)).Error
})
if err != nil {
return false, err
}
return created, nil
}
func (r *CommentRepository) DeleteComment(ctx context.Context, comment *Comment) error {
return r.db.WithContext(ctx).Delete(comment).Error
}
// DeleteByID 返回是否真的删除了一行,供调用方门控热度回退。
func (r *CommentRepository) DeleteByID(ctx context.Context, id uint) (bool, error) {
if id == 0 {
return false, nil
}
res := r.db.WithContext(ctx).Delete(&Comment{}, id)
if res.Error != nil {
return false, res.Error
}
return res.RowsAffected > 0, nil
}
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
}
func (r *CommentRepository) CreateMentions(ctx context.Context, mentions []Mention) error {
if len(mentions) == 0 {
return nil
}
now := time.Now()
rows := make([]notification.Notification, 0, len(mentions))
for _, m := range mentions {
rows = append(rows, notification.Notification{
RecipientID: m.RecipientID,
SenderID: m.SenderID,
Type: "mention",
TargetID: m.TargetID,
Content: m.Content,
CreatedAt: now,
DedupeKey: nilIfEmpty(m.DedupeKey),
})
}
return r.db.WithContext(ctx).
Clauses(clause.OnConflict{DoNothing: true}).
Create(&rows).Error
}
// FindAccountIDs 一次查询解析一批用户名;不存在的用户名不出现在结果里。
func (r *CommentRepository) FindAccountIDs(ctx context.Context, usernames []string) (map[string]uint, error) {
out := make(map[string]uint, len(usernames))
if len(usernames) == 0 {
return out, nil
}
var rows []struct {
ID uint
Username string
}
if err := r.db.WithContext(ctx).
Table("accounts").
Select("id, username").
Where("username IN ?", usernames).
Scan(&rows).Error; err != nil {
return nil, err
}
for _, row := range rows {
out[row.Username] = row.ID
}
return out, nil
}