accounts.go 3.0 KB

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