sqlstore_accounts.go 2.2 KB

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