accounts.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package sqlstore
  2. import (
  3. "time"
  4. "github.com/go-xorm/xorm"
  5. "github.com/torkelo/grafana-pro/pkg/bus"
  6. m "github.com/torkelo/grafana-pro/pkg/models"
  7. )
  8. func init() {
  9. bus.AddHandler("sql", GetAccountInfo)
  10. bus.AddHandler("sql", AddCollaborator)
  11. bus.AddHandler("sql", GetOtherAccounts)
  12. bus.AddHandler("sql", CreateAccount)
  13. bus.AddHandler("sql", SetUsingAccount)
  14. bus.AddHandler("sql", GetAccountById)
  15. bus.AddHandler("sql", GetAccountByLogin)
  16. bus.AddHandler("sql", GetAccountByToken)
  17. }
  18. func CreateAccount(cmd *m.CreateAccountCommand) error {
  19. return inTransaction(func(sess *xorm.Session) error {
  20. account := m.Account{
  21. Email: cmd.Email,
  22. Login: cmd.Login,
  23. Password: cmd.Password,
  24. Salt: cmd.Salt,
  25. Created: time.Now(),
  26. Updated: time.Now(),
  27. }
  28. _, err := sess.Insert(&account)
  29. cmd.Result = account
  30. return err
  31. })
  32. }
  33. func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
  34. return inTransaction(func(sess *xorm.Session) error {
  35. account := m.Account{}
  36. sess.Id(cmd.AccountId).Get(&account)
  37. account.UsingAccountId = cmd.UsingAccountId
  38. _, err := sess.Id(account.Id).Update(&account)
  39. return err
  40. })
  41. }
  42. func GetAccountInfo(query *m.GetAccountInfoQuery) error {
  43. var account m.Account
  44. has, err := x.Id(query.Id).Get(&account)
  45. if err != nil {
  46. return err
  47. } else if has == false {
  48. return m.ErrAccountNotFound
  49. }
  50. query.Result = m.AccountDTO{
  51. Name: account.Name,
  52. Email: account.Email,
  53. Collaborators: make([]*m.CollaboratorDTO, 0),
  54. }
  55. sess := x.Table("collaborator")
  56. sess.Join("INNER", "account", "account.id=collaborator.account_Id")
  57. sess.Where("collaborator.for_account_id=?", query.Id)
  58. err = sess.Find(&query.Result.Collaborators)
  59. return err
  60. }
  61. func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
  62. return inTransaction(func(sess *xorm.Session) error {
  63. entity := m.Collaborator{
  64. AccountId: cmd.AccountId,
  65. ForAccountId: cmd.ForAccountId,
  66. Role: cmd.Role,
  67. Created: time.Now(),
  68. Updated: time.Now(),
  69. }
  70. _, err := sess.Insert(&entity)
  71. return err
  72. })
  73. }
  74. func GetAccountById(query *m.GetAccountByIdQuery) error {
  75. var err error
  76. var account m.Account
  77. has, err := x.Id(query.Id).Get(&account)
  78. if err != nil {
  79. return err
  80. } else if has == false {
  81. return m.ErrAccountNotFound
  82. }
  83. if account.UsingAccountId == 0 {
  84. account.UsingAccountId = account.Id
  85. }
  86. query.Result = &account
  87. return nil
  88. }
  89. func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
  90. var err error
  91. var account m.Account
  92. has, err := x.Where("token=?", query.Token).Get(&account)
  93. if err != nil {
  94. return err
  95. } else if has == false {
  96. return m.ErrAccountNotFound
  97. }
  98. if account.UsingAccountId == 0 {
  99. account.UsingAccountId = account.Id
  100. }
  101. query.Result = &account
  102. return nil
  103. }
  104. func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
  105. var err error
  106. account := m.Account{Login: query.Login}
  107. has, err := x.Get(&account)
  108. if err != nil {
  109. return err
  110. } else if has == false {
  111. return m.ErrAccountNotFound
  112. }
  113. if account.UsingAccountId == 0 {
  114. account.UsingAccountId = account.Id
  115. }
  116. query.Result = &account
  117. return nil
  118. }
  119. func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
  120. query.Result = make([]*m.OtherAccountDTO, 0)
  121. sess := x.Table("collaborator")
  122. sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
  123. sess.Where("account_id=?", query.AccountId)
  124. sess.Cols("collaborator.id", "collaborator.role", "account.email")
  125. err := sess.Find(&query.Result)
  126. return err
  127. }