accounts.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package sqlstore
  2. import (
  3. "github.com/torkelo/grafana-pro/pkg/bus"
  4. m "github.com/torkelo/grafana-pro/pkg/models"
  5. )
  6. func init() {
  7. bus.AddHandler("sql", GetAccountInfo)
  8. }
  9. func GetAccountInfo(query *m.GetAccountInfoQuery) error {
  10. var account m.Account
  11. has, err := x.Id(query.Id).Get(&account)
  12. if err != nil {
  13. return err
  14. } else if has == false {
  15. return m.ErrAccountNotFound
  16. }
  17. query.Result = m.AccountDTO{
  18. Name: account.Name,
  19. Email: account.Email,
  20. Collaborators: make([]*m.CollaboratorDTO, 0),
  21. }
  22. sess := x.Table("collaborator")
  23. sess.Join("INNER", "account", "account.id=collaborator.account_Id")
  24. sess.Where("collaborator.for_account_id=?", query.Id)
  25. err = sess.Find(&query.Result.Collaborators)
  26. return err
  27. }
  28. func SaveAccount(account *m.Account) error {
  29. var err error
  30. sess := x.NewSession()
  31. defer sess.Close()
  32. if err = sess.Begin(); err != nil {
  33. return err
  34. }
  35. if account.Id == 0 {
  36. _, err = sess.Insert(account)
  37. } else {
  38. _, err = sess.Id(account.Id).Update(account)
  39. }
  40. if err != nil {
  41. sess.Rollback()
  42. return err
  43. } else if err = sess.Commit(); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. func GetAccount(id int64) (*m.Account, error) {
  49. var err error
  50. var account m.Account
  51. has, err := x.Id(id).Get(&account)
  52. if err != nil {
  53. return nil, err
  54. } else if has == false {
  55. return nil, m.ErrAccountNotFound
  56. }
  57. if account.UsingAccountId == 0 {
  58. account.UsingAccountId = account.Id
  59. }
  60. return &account, nil
  61. }
  62. func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
  63. var err error
  64. account := &m.Account{Login: emailOrLogin}
  65. has, err := x.Get(account)
  66. if err != nil {
  67. return nil, err
  68. } else if has == false {
  69. return nil, m.ErrAccountNotFound
  70. }
  71. return account, nil
  72. }
  73. func GetCollaboratorsForAccount(accountId int64) ([]*m.CollaboratorInfo, error) {
  74. collaborators := make([]*m.CollaboratorInfo, 0)
  75. sess := x.Table("collaborator")
  76. sess.Join("INNER", "account", "account.id=collaborator.account_Id")
  77. sess.Where("collaborator.for_account_id=?", accountId)
  78. err := sess.Find(&collaborators)
  79. return collaborators, err
  80. }
  81. func AddCollaborator(collaborator *m.Collaborator) error {
  82. var err error
  83. sess := x.NewSession()
  84. defer sess.Close()
  85. if err = sess.Begin(); err != nil {
  86. return err
  87. }
  88. if _, err = sess.Insert(collaborator); err != nil {
  89. sess.Rollback()
  90. return err
  91. } else if err = sess.Commit(); err != nil {
  92. return err
  93. }
  94. return nil
  95. }
  96. func GetOtherAccountsFor(accountId int64) ([]*m.OtherAccount, error) {
  97. collaborators := make([]*m.OtherAccount, 0)
  98. sess := x.Table("collaborator")
  99. sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
  100. sess.Where("account_id=?", accountId)
  101. sess.Cols("collaborator.id", "collaborator.role", "account.email")
  102. err := sess.Find(&collaborators)
  103. return collaborators, err
  104. }