package dao import ( "time" "gorm.io/gorm" ) // 帖子评论和回复表 type PostComment struct { ID uint64 `gorm:"primaryKey;not null;comment:'评论id'"` CreatedAt time.Time `gorm:"type:datetime(0)"` UserId int32 `gorm:"not null;comment:'评论者'"` PostId uint64 `gorm:"index;not null;comment:'帖子'"` Content string `gorm:"type:varchar(1024);not null;comment:'评论内容'"` // CommentId uint64 `gorm:"comment:'评论上级ID,作为回复某个评论时使用'"` } func PostComment_Insert(data *PostComment) (uint64, error) { if err := s.db.Create(data).Error; err != nil { return 0, err } return uint64(data.ID), nil } // 根据帖子id,查询所有的评论 func PostComment_QueryListByPostId(postId uint64) ([]*PostComment, error) { var list []*PostComment if err := s.db.Where("post_id = ?", postId).Find(&list).Error; err != nil && err != gorm.ErrRecordNotFound { return nil, err } return list, nil } func PostComment_Delete(commentId uint64) error { data := PostComment{ID: commentId} return s.db.Delete(&data).Error }