relationship.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package models
  2. import (
  3. "github.com/lifei6671/godoc/conf"
  4. "github.com/astaxie/beego/orm"
  5. )
  6. type Relationship struct {
  7. RelationshipId int `orm:"pk;auto;unique;column(relationship_id)" json:"relationship_id"`
  8. MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
  9. BookId int `orm:"column(book_id);type(int)" json:"book_id"`
  10. // RoleId 角色:0 创始人(创始人不能被移除) / 1 管理员/2 编辑者/3 观察者
  11. RoleId int `orm:"column(role_id);type(int)" json:"role_id"`
  12. }
  13. // TableName 获取对应数据库表名.
  14. func (m *Relationship) TableName() string {
  15. return "relationship"
  16. }
  17. func (m *Relationship) TableNameWithPrefix() string {
  18. return conf.GetDatabasePrefix() + m.TableName()
  19. }
  20. // TableEngine 获取数据使用的引擎.
  21. func (m *Relationship) TableEngine() string {
  22. return "INNODB"
  23. }
  24. // 联合唯一键
  25. func (u *Relationship) TableUnique() [][]string {
  26. return [][]string{
  27. []string{"MemberId", "BookId"},
  28. }
  29. }
  30. func NewRelationship() *Relationship {
  31. return &Relationship{}
  32. }
  33. func (m *Relationship) FindForRoleId(book_id ,member_id int) (int,error) {
  34. o := orm.NewOrm()
  35. relationship := NewRelationship()
  36. err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",book_id).Filter("member_id",member_id).One(relationship)
  37. if err != nil {
  38. return 0,err
  39. }
  40. return relationship.RoleId,nil
  41. }
  42. func (m *Relationship) Insert() error {
  43. o := orm.NewOrm()
  44. _,err := o.Insert(m)
  45. return err
  46. }
  47. func (m *Relationship) Update() error {
  48. o := orm.NewOrm()
  49. _,err := o.Update(m)
  50. return err
  51. }