accounts.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  17. func CreateAccount(cmd *m.CreateAccountCommand) error {
  18. return inTransaction(func(sess *xorm.Session) error {
  19. account := m.Account{
  20. Email: cmd.Email,
  21. Login: cmd.Login,
  22. Password: cmd.Password,
  23. Salt: cmd.Salt,
  24. Created: time.Now(),
  25. Updated: time.Now(),
  26. }
  27. _, err := sess.Insert(&account)
  28. cmd.Result = account
  29. return err
  30. })
  31. }
  32. func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
  33. return inTransaction(func(sess *xorm.Session) error {
  34. account := m.Account{}
  35. sess.Id(cmd.AccountId).Get(&account)
  36. account.UsingAccountId = cmd.UsingAccountId
  37. _, err := sess.Id(account.Id).Update(&account)
  38. return err
  39. })
  40. }
  41. func GetAccountInfo(query *m.GetAccountInfoQuery) error {
  42. var account m.Account
  43. has, err := x.Id(query.Id).Get(&account)
  44. if err != nil {
  45. return err
  46. } else if has == false {
  47. return m.ErrAccountNotFound
  48. }
  49. query.Result = m.AccountDTO{
  50. Name: account.Name,
  51. Email: account.Email,
  52. Collaborators: make([]*m.CollaboratorDTO, 0),
  53. }
  54. sess := x.Table("collaborator")
  55. sess.Join("INNER", "account", "account.id=collaborator.account_Id")
  56. sess.Where("collaborator.for_account_id=?", query.Id)
  57. err = sess.Find(&query.Result.Collaborators)
  58. return err
  59. }
  60. func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
  61. return inTransaction(func(sess *xorm.Session) error {
  62. entity := m.Collaborator{
  63. AccountId: cmd.AccountId,
  64. ForAccountId: cmd.ForAccountId,
  65. Role: cmd.Role,
  66. Created: time.Now(),
  67. Updated: time.Now(),
  68. }
  69. _, err := sess.Insert(&entity)
  70. return err
  71. })
  72. }
  73. func GetAccountById(query *m.GetAccountByIdQuery) error {
  74. var err error
  75. var account m.Account
  76. has, err := x.Id(query.Id).Get(&account)
  77. if err != nil {
  78. return err
  79. } else if has == false {
  80. return m.ErrAccountNotFound
  81. }
  82. if account.UsingAccountId == 0 {
  83. account.UsingAccountId = account.Id
  84. }
  85. query.Result = &account
  86. return nil
  87. }
  88. func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
  89. var err error
  90. account := m.Account{Login: query.Login}
  91. has, err := x.Get(&account)
  92. if err != nil {
  93. return err
  94. } else if has == false {
  95. return m.ErrAccountNotFound
  96. }
  97. if account.UsingAccountId == 0 {
  98. account.UsingAccountId = account.Id
  99. }
  100. query.Result = &account
  101. return nil
  102. }
  103. func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
  104. query.Result = make([]*m.OtherAccountDTO, 0)
  105. sess := x.Table("collaborator")
  106. sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
  107. sess.Where("account_id=?", query.AccountId)
  108. sess.Cols("collaborator.id", "collaborator.role", "account.email")
  109. err := sess.Find(&query.Result)
  110. return err
  111. }