sqlstore_accounts.go 2.0 KB

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