document.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package models
  2. import (
  3. "time"
  4. "github.com/lifei6671/godoc/conf"
  5. "github.com/astaxie/beego/orm"
  6. )
  7. // Document struct.
  8. type Document struct {
  9. DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"document_id"`
  10. DocumentName string `orm:"column(document_name);size(500)" json:"document_name"`
  11. // Identify 文档唯一标识
  12. Identify string `orm:"column(identify);size(100);unique" json:"identify"`
  13. BookId int `orm:"column(book_id);type(int);index" json:"book_id"`
  14. OrderSort int `orm:"column(order_sort);default(0);type(int)" json:"order_sort"`
  15. // Markdown markdown格式文档.
  16. Markdown string `orm:"column(markdown);type(longtext)" json:"markdown"`
  17. // Release 发布后的Html格式内容.
  18. Release string `orm:"column(release);type(longtext)" json:"release"`
  19. // Content 未发布的 Html 格式内容.
  20. Content string `orm:"column(content);type(longtext)" json:"content"`
  21. CreateTime time.Time `orm:"column(create_time);type(datetime)" json:"create_time"`
  22. CreateAt int `orm:"column(create_at);type(int)" json:"create_at"`
  23. ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
  24. ModifyAt int `orm:"column(modify_at);type(int)" json:"modify_at"`
  25. Version int64 `orm:"type(bigint);column(version)" json:"version"`
  26. }
  27. // TableName 获取对应数据库表名.
  28. func (m *Document) TableName() string {
  29. return "documents"
  30. }
  31. // TableEngine 获取数据使用的引擎.
  32. func (m *Document) TableEngine() string {
  33. return "INNODB"
  34. }
  35. func (m *Document) TableNameWithPrefix() string {
  36. return conf.GetDatabasePrefix() + m.TableName()
  37. }
  38. func NewDocument() *Document {
  39. return &Document{}
  40. }
  41. func (m *Document) Find(id int) error {
  42. if id <= 0 {
  43. return ErrInvalidParameter
  44. }
  45. o := orm.NewOrm()
  46. err := o.Read(m)
  47. if err == orm.ErrNoRows{
  48. return ErrDataNotExist
  49. }
  50. return nil
  51. }