grafana/pkg/stores/sqlstore/accounts.go

126 lines
2.5 KiB
Go
Raw Normal View History

2014-11-14 10:13:33 -06:00
package sqlstore
import (
"time"
"github.com/go-xorm/xorm"
"github.com/torkelo/grafana-pro/pkg/bus"
m "github.com/torkelo/grafana-pro/pkg/models"
)
2014-11-14 10:13:33 -06:00
func init() {
bus.AddHandler("sql", GetAccountInfo)
bus.AddHandler("sql", AddCollaborator)
}
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
var account m.Account
has, err := x.Id(query.Id).Get(&account)
if err != nil {
return err
} else if has == false {
return m.ErrAccountNotFound
}
query.Result = m.AccountDTO{
Name: account.Name,
Email: account.Email,
Collaborators: make([]*m.CollaboratorDTO, 0),
}
sess := x.Table("collaborator")
sess.Join("INNER", "account", "account.id=collaborator.account_Id")
sess.Where("collaborator.for_account_id=?", query.Id)
err = sess.Find(&query.Result.Collaborators)
return err
}
func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
return inTransaction(func(sess *xorm.Session) error {
entity := m.Collaborator{
AccountId: cmd.AccountId,
ForAccountId: cmd.ForAccountId,
Role: cmd.Role,
Created: time.Now(),
Updated: time.Now(),
}
_, err := sess.Insert(&entity)
return err
})
}
func SaveAccount(account *m.Account) error {
2014-11-14 10:13:33 -06:00
var err error
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
2014-12-15 14:56:16 -06:00
if account.Id == 0 {
_, err = sess.Insert(account)
} else {
_, err = sess.Id(account.Id).Update(account)
}
if err != nil {
2014-11-14 10:13:33 -06:00
sess.Rollback()
return err
} else if err = sess.Commit(); err != nil {
return err
}
return nil
}
func GetAccount(id int64) (*m.Account, error) {
2014-11-20 05:11:07 -06:00
var err error
var account m.Account
has, err := x.Id(id).Get(&account)
2014-11-20 05:11:07 -06:00
2014-11-14 10:13:33 -06:00
if err != nil {
2014-11-20 05:11:07 -06:00
return nil, err
} else if has == false {
return nil, m.ErrAccountNotFound
2014-11-14 10:13:33 -06:00
}
if account.UsingAccountId == 0 {
account.UsingAccountId = account.Id
}
return &account, nil
2014-11-20 05:11:07 -06:00
}
func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
2014-11-20 05:11:07 -06:00
var err error
account := &m.Account{Login: emailOrLogin}
2014-11-20 05:11:07 -06:00
has, err := x.Get(account)
if err != nil {
return nil, err
} else if has == false {
return nil, m.ErrAccountNotFound
2014-11-14 10:13:33 -06:00
}
2014-11-20 05:11:07 -06:00
return account, nil
2014-11-14 10:13:33 -06:00
}
func GetOtherAccountsFor(accountId int64) ([]*m.OtherAccount, error) {
collaborators := make([]*m.OtherAccount, 0)
2014-11-24 03:17:13 -06:00
sess := x.Table("collaborator")
2014-12-16 02:06:49 -06:00
sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
2014-11-24 03:17:13 -06:00
sess.Where("account_id=?", accountId)
2014-12-16 02:06:49 -06:00
sess.Cols("collaborator.id", "collaborator.role", "account.email")
2014-11-24 01:04:41 -06:00
err := sess.Find(&collaborators)
return collaborators, err
}