2025-12-16 18:51:50 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
|
|
|
|
type Comment struct {
|
|
|
|
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
|
|
|
|
Username string `gorm:"index" json:"username"`
|
|
|
|
|
|
VideoID uint `gorm:"index" json:"video_id"`
|
2026-10-30 13:21:09 +08:00
|
|
|
|
AuthorID uint `gorm:"index;uniqueIndex:uk_comments_author_key,priority:1" json:"author_id"`
|
2025-12-16 18:51:50 +08:00
|
|
|
|
Content string `gorm:"type:text" json:"content"`
|
|
|
|
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
2026-10-30 13:21:09 +08:00
|
|
|
|
|
|
|
|
|
|
// IdempotencyKey 是"这次发评论的意图"的稳定标识(P0-4)。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 与 AuthorID 组成唯一索引 uk_comments_author_key:同一次用户意图
|
|
|
|
|
|
// 无论被写入几次(MQ 假失败后的 DB 兜底、worker 重复投递、客户端
|
|
|
|
|
|
// 重试),在数据库里都只会留下一行。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 必须是指针:MySQL 的唯一索引不约束 NULL,多个 NULL 可以共存。
|
|
|
|
|
|
// 若用 string + 空串,存量行与"没带 request_id"的行会全部撞在
|
|
|
|
|
|
// 同一个 '' 上,第二条无键评论就会被判成重复。
|
|
|
|
|
|
//
|
|
|
|
|
|
// json:"-" 让它不出现在任何 API 响应里:它是去重机制的内部标识,
|
|
|
|
|
|
// 不是用户可见的数据。
|
|
|
|
|
|
IdempotencyKey *string `gorm:"column:idempotency_key;type:varchar(64);uniqueIndex:uk_comments_author_key,priority:2" json:"-"`
|
2025-12-16 18:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type PublishCommentRequest struct {
|
|
|
|
|
|
VideoID uint `json:"video_id"`
|
|
|
|
|
|
Content string `json:"content"`
|
2026-10-30 13:21:09 +08:00
|
|
|
|
|
|
|
|
|
|
// RequestID 由客户端为"一次发送动作"生成,重试时复用同一个值。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 为空时服务端会生成一个(单次请求内仍然幂等,但客户端重试无法被识别)。
|
|
|
|
|
|
// 校验规则见 idempotency.go:8–64 个 [0-9a-zA-Z_-] 字符,非法值返回 400
|
|
|
|
|
|
// 而不是静默截断或忽略。
|
|
|
|
|
|
RequestID string `json:"request_id"`
|
2025-12-16 18:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type DeleteCommentRequest struct {
|
|
|
|
|
|
CommentID uint `json:"comment_id"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type GetAllCommentsRequest struct {
|
|
|
|
|
|
VideoID uint `json:"video_id"`
|
|
|
|
|
|
}
|