comment.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package models
  2. import (
  3. "time"
  4. "github.com/lifei6671/godoc/conf"
  5. "github.com/astaxie/beego/orm"
  6. "errors"
  7. )
  8. //Comment struct
  9. type Comment struct {
  10. CommentId int `orm:"pk;auto;unique;column(comment_id)" json:"comment_id"`
  11. BookId int `orm:"column(book_id);type(int)" json:"book_id"`
  12. // DocumentId 评论所属的文档.
  13. DocumentId int `orm:"column(document_id);type(int)" json:"document_id"`
  14. // Author 评论作者.
  15. Author string `orm:"column(author);size(100)" json:"author"`
  16. //MemberId 评论用户ID.
  17. MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
  18. // IPAddress 评论者的IP地址
  19. IPAddress string `orm:"column(ip_address);size(100)" json:"ip_address"`
  20. // 评论日期.
  21. CommentDate time.Time `orm:"type(datetime);column(comment_date);auto_now_add" json:"comment_date"`
  22. //Content 评论内容.
  23. Content string `orm:"column(content);size(2000)" json:"content"`
  24. // Approved 评论状态:0 待审核/1 已审核/2 垃圾评论/ 3 已删除
  25. Approved int `orm:"column(approved);type(int)" json:"approved"`
  26. // UserAgent 评论者浏览器内容
  27. UserAgent string `orm:"column(user_agent);size(500)" json:"user_agent"`
  28. // Parent 评论所属父级
  29. ParentId int `orm:"column(parent_id);type(int);default(0)" json:"parent_id"`
  30. AgreeCount int `orm:"column(agree_count);type(int);default(0)" json:"agree_count"`
  31. AgainstCount int `orm:"column(against_count);type(int);default(0)" json:"against_count"`
  32. }
  33. // TableName 获取对应数据库表名.
  34. func (m *Comment) TableName() string {
  35. return "comments"
  36. }
  37. // TableEngine 获取数据使用的引擎.
  38. func (m *Comment) TableEngine() string {
  39. return "INNODB"
  40. }
  41. func (m *Comment) TableNameWithPrefix() string {
  42. return conf.GetDatabasePrefix() + m.TableName()
  43. }
  44. func NewComment() *Comment {
  45. return &Comment{}
  46. }
  47. func (m *Comment) Find(id int) error {
  48. if id <= 0 {
  49. return ErrInvalidParameter
  50. }
  51. o := orm.NewOrm()
  52. return o.Read(m)
  53. }
  54. func (m *Comment) Update(cols... string) error {
  55. o := orm.NewOrm()
  56. _,err := o.Update(m,cols...)
  57. return err
  58. }
  59. //Insert 添加一条评论.
  60. func (m *Comment) Insert() error {
  61. if m.DocumentId <= 0{
  62. return errors.New("评论文档不存在")
  63. }
  64. if m.Content == "" {
  65. return ErrCommentContentNotEmpty
  66. }
  67. o := orm.NewOrm()
  68. if m.CommentId > 0 {
  69. comment := NewComment()
  70. //如果父评论不存在
  71. if err := o.Read(comment); err != nil {
  72. return err
  73. }
  74. }
  75. document := NewDocument()
  76. //如果评论的文档不存在
  77. if _,err := document.Find(m.DocumentId); err != nil {
  78. return err
  79. }
  80. book ,err := NewBook().Find(document.BookId);
  81. //如果评论的项目不存在
  82. if err != nil {
  83. return err
  84. }
  85. //如果已关闭评论
  86. if book.CommentStatus == "closed"{
  87. return ErrCommentClosed
  88. }
  89. if book.CommentStatus == "registered_only" && m.MemberId <= 0{
  90. return ErrPermissionDenied
  91. }
  92. //如果仅参与者评论
  93. if book.CommentStatus == "group_only" {
  94. if m.MemberId <= 0{
  95. return ErrPermissionDenied
  96. }
  97. rel := NewRelationship()
  98. if _,err := rel.FindForRoleId(book.BookId,m.MemberId);err != nil {
  99. return ErrPermissionDenied
  100. }
  101. }
  102. if m.MemberId > 0 {
  103. member := NewMember()
  104. //如果用户不存在
  105. if err := member.Find(m.MemberId) ; err != nil {
  106. return ErrMemberNoExist
  107. }
  108. //如果用户被禁用
  109. if member.Status == 1 {
  110. return ErrMemberDisabled
  111. }
  112. }else if m.Author == "" {
  113. m.Author = "[匿名用户]"
  114. }
  115. m.BookId = book.BookId
  116. _,err = o.Insert(m)
  117. return err
  118. }