collaborators.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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", AddCollaborator)
  10. bus.AddHandler("sql", RemoveCollaborator)
  11. bus.AddHandler("sql", GetCollaborators)
  12. }
  13. func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
  14. return inTransaction(func(sess *xorm.Session) error {
  15. entity := m.Collaborator{
  16. AccountId: cmd.AccountId,
  17. CollaboratorId: cmd.CollaboratorId,
  18. Role: cmd.Role,
  19. Created: time.Now(),
  20. Updated: time.Now(),
  21. }
  22. _, err := sess.Insert(&entity)
  23. return err
  24. })
  25. }
  26. func GetCollaborators(query *m.GetCollaboratorsQuery) error {
  27. query.Result = make([]*m.CollaboratorDTO, 0)
  28. sess := x.Table("collaborator")
  29. sess.Join("INNER", "account", "collaborator.collaborator_id=account.id")
  30. sess.Where("collaborator.account_id=?", query.AccountId)
  31. sess.Cols("collaborator.collaborator_id", "collaborator.role", "account.email", "account.login")
  32. err := sess.Find(&query.Result)
  33. return err
  34. }
  35. func RemoveCollaborator(cmd *m.RemoveCollaboratorCommand) error {
  36. return inTransaction(func(sess *xorm.Session) error {
  37. var rawSql = "DELETE FROM collaborator WHERE collaborator_id=? and account_id=?"
  38. _, err := sess.Exec(rawSql, cmd.CollaboratorId, cmd.AccountId)
  39. return err
  40. })
  41. }