2014-11-14 10:13:33 -06:00
|
|
|
package sqlstore
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
import (
|
2014-12-19 03:45:22 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
|
|
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
|
|
|
)
|
2014-11-14 10:13:33 -06:00
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", GetAccountInfo)
|
2014-12-19 03:45:22 -06:00
|
|
|
bus.AddHandler("sql", AddCollaborator)
|
2014-12-19 04:08:49 -06:00
|
|
|
bus.AddHandler("sql", GetOtherAccounts)
|
2014-12-19 04:53:27 -06:00
|
|
|
bus.AddHandler("sql", CreateAccount)
|
2014-12-19 06:12:47 -06:00
|
|
|
bus.AddHandler("sql", SetUsingAccount)
|
2014-12-19 06:40:02 -06:00
|
|
|
bus.AddHandler("sql", GetAccountById)
|
|
|
|
bus.AddHandler("sql", GetAccountByLogin)
|
2014-12-19 04:53:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
|
|
|
|
account := m.Account{
|
|
|
|
Email: cmd.Email,
|
|
|
|
Login: cmd.Login,
|
|
|
|
Password: cmd.Password,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sess.Insert(&account)
|
|
|
|
cmd.Result = account
|
|
|
|
return err
|
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
}
|
|
|
|
|
2014-12-19 06:12:47 -06:00
|
|
|
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
account := m.Account{}
|
|
|
|
sess.Id(cmd.AccountId).Get(&account)
|
|
|
|
|
|
|
|
account.UsingAccountId = cmd.UsingAccountId
|
|
|
|
_, err := sess.Id(account.Id).Update(&account)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-19 03:45:22 -06:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
func GetAccountById(query *m.GetAccountByIdQuery) error {
|
2014-11-20 05:11:07 -06:00
|
|
|
var err error
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
var account m.Account
|
2014-12-19 06:40:02 -06:00
|
|
|
has, err := x.Id(query.Id).Get(&account)
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2014-11-14 10:13:33 -06:00
|
|
|
if err != nil {
|
2014-12-19 06:40:02 -06:00
|
|
|
return err
|
2014-11-20 05:11:07 -06:00
|
|
|
} else if has == false {
|
2014-12-19 06:40:02 -06:00
|
|
|
return m.ErrAccountNotFound
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
|
|
|
|
2014-11-20 08:19:44 -06:00
|
|
|
if account.UsingAccountId == 0 {
|
|
|
|
account.UsingAccountId = account.Id
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
query.Result = &account
|
|
|
|
|
|
|
|
return nil
|
2014-11-20 05:11:07 -06:00
|
|
|
}
|
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
|
2014-11-20 05:11:07 -06:00
|
|
|
var err error
|
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
account := m.Account{Login: query.Login}
|
|
|
|
has, err := x.Get(&account)
|
2014-11-20 05:11:07 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2014-12-19 06:40:02 -06:00
|
|
|
return err
|
2014-11-20 05:11:07 -06:00
|
|
|
} else if has == false {
|
2014-12-19 06:40:02 -06:00
|
|
|
return m.ErrAccountNotFound
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
if account.UsingAccountId == 0 {
|
|
|
|
account.UsingAccountId = account.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &account
|
|
|
|
|
|
|
|
return nil
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
2014-11-21 09:43:04 -06:00
|
|
|
|
2014-12-19 04:08:49 -06:00
|
|
|
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
|
|
|
|
query.Result = make([]*m.OtherAccountDTO, 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-12-19 04:08:49 -06:00
|
|
|
sess.Where("account_id=?", query.AccountId)
|
2014-12-16 02:06:49 -06:00
|
|
|
sess.Cols("collaborator.id", "collaborator.role", "account.email")
|
2014-12-19 04:08:49 -06:00
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
2014-11-24 01:04:41 -06:00
|
|
|
}
|