Worked on account update, moved collaborats to its own api url and files
This commit is contained in:
@@ -18,9 +18,8 @@ func init() {
|
||||
bus.AddHandler("sql", GetAccountById)
|
||||
bus.AddHandler("sql", GetAccountByLogin)
|
||||
bus.AddHandler("sql", GetAccountByToken)
|
||||
bus.AddHandler("sql", AddCollaborator)
|
||||
bus.AddHandler("sql", RemoveCollaborator)
|
||||
bus.AddHandler("sql", SearchAccounts)
|
||||
bus.AddHandler("sql", UpdateAccount)
|
||||
}
|
||||
|
||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
@@ -44,6 +43,21 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateAccount(cmd *m.UpdateAccountCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
|
||||
account := m.Account{
|
||||
Email: cmd.Email,
|
||||
Login: cmd.Login,
|
||||
Name: cmd.Name,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Id(cmd.AccountId).Update(&account)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
account := m.Account{}
|
||||
@@ -66,35 +80,14 @@ func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
||||
}
|
||||
|
||||
query.Result = m.AccountDTO{
|
||||
Name: account.Name,
|
||||
Email: account.Email,
|
||||
Collaborators: make([]*m.CollaboratorDTO, 0),
|
||||
Name: account.Name,
|
||||
Email: account.Email,
|
||||
Login: account.Login,
|
||||
}
|
||||
|
||||
sess := x.Table("collaborator")
|
||||
sess.Join("INNER", "account", "account.id=collaborator.collaborator_id")
|
||||
sess.Where("collaborator.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,
|
||||
CollaboratorId: cmd.CollaboratorId,
|
||||
Role: cmd.Role,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func GetAccountById(query *m.GetAccountByIdQuery) error {
|
||||
var err error
|
||||
|
||||
@@ -165,14 +158,6 @@ func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveCollaborator(cmd *m.RemoveCollaboratorCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "DELETE FROM collaborator WHERE collaborator_id=? and account_id=?"
|
||||
_, err := sess.Exec(rawSql, cmd.CollaboratorId, cmd.AccountId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
|
||||
query.Result = make([]*m.OtherAccountDTO, 0)
|
||||
sess := x.Table("collaborator")
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", AddCollaborator)
|
||||
bus.AddHandler("sql", RemoveCollaborator)
|
||||
bus.AddHandler("sql", GetCollaborators)
|
||||
}
|
||||
|
||||
func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
|
||||
entity := m.Collaborator{
|
||||
AccountId: cmd.AccountId,
|
||||
CollaboratorId: cmd.CollaboratorId,
|
||||
Role: cmd.Role,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func GetCollaborators(query *m.GetCollaboratorsQuery) error {
|
||||
query.Result = make([]*m.CollaboratorDTO, 0)
|
||||
sess := x.Table("collaborator")
|
||||
sess.Join("INNER", "account", "collaborator.collaborator_id=account.id")
|
||||
sess.Where("collaborator.account_id=?", query.AccountId)
|
||||
sess.Cols("collaborator.collaborator_id", "collaborator.role", "account.email", "account.login")
|
||||
|
||||
err := sess.Find(&query.Result)
|
||||
return err
|
||||
}
|
||||
|
||||
func RemoveCollaborator(cmd *m.RemoveCollaboratorCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "DELETE FROM collaborator WHERE collaborator_id=? and account_id=?"
|
||||
_, err := sess.Exec(rawSql, cmd.CollaboratorId, cmd.AccountId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user