Commit d5a59ac6 by Torkel Ödegaard

More migration to command/query and sql tests, looking good

parent ccba9554
package api package api
import ( import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
...@@ -53,63 +52,60 @@ func AddCollaborator(c *middleware.Context) { ...@@ -53,63 +52,60 @@ func AddCollaborator(c *middleware.Context) {
} }
func GetOtherAccounts(c *middleware.Context) { func GetOtherAccounts(c *middleware.Context) {
query := m.GetOtherAccountsQuery{AccountId: c.UserAccount.Id}
err := bus.Dispatch(&query)
otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
if err != nil { if err != nil {
c.JSON(500, utils.DynMap{"message": err.Error()}) c.JSON(500, utils.DynMap{"message": err.Error()})
return return
} }
var result []*dtos.OtherAccount result := append(query.Result, &m.OtherAccountDTO{
result = append(result, &dtos.OtherAccount{ Id: c.UserAccount.Id,
Id: c.UserAccount.Id, Role: "owner",
Role: "owner", Email: c.UserAccount.Email,
IsUsing: c.UserAccount.Id == c.UserAccount.UsingAccountId,
Name: c.UserAccount.Email,
}) })
for _, other := range otherAccounts { for _, ac := range result {
result = append(result, &dtos.OtherAccount{ if ac.Id == c.UserAccount.UsingAccountId {
Id: other.Id, ac.IsUsing = true
Role: other.Role, break
Name: other.Email, }
IsUsing: other.Id == c.UserAccount.UsingAccountId,
})
} }
c.JSON(200, result) c.JSON(200, result)
} }
func SetUsingAccount(c *middleware.Context) { func SetUsingAccount(c *middleware.Context) {
usingAccountId := c.ParamsInt64(":id") // usingAccountId := c.ParamsInt64(":id")
//
account := c.UserAccount // account := c.UserAccount
otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id) // otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
//
if err != nil { // if err != nil {
c.JSON(500, utils.DynMap{"message": err.Error()}) // c.JSON(500, utils.DynMap{"message": err.Error()})
return // return
} // }
//
// validate that the account id in the list // // validate that the account id in the list
valid := false // valid := false
for _, other := range otherAccounts { // for _, other := range otherAccounts {
if other.Id == usingAccountId { // if other.Id == usingAccountId {
valid = true // valid = true
} // }
} // }
//
if !valid { // if !valid {
c.Status(401) // c.Status(401)
return // return
} // }
//
account.UsingAccountId = usingAccountId // account.UsingAccountId = usingAccountId
err = m.SaveAccount(account) // err = m.SaveAccount(account)
if err != nil { // if err != nil {
c.JSON(500, utils.DynMap{"message": err.Error()}) // c.JSON(500, utils.DynMap{"message": err.Error()})
return // return
} // }
//
c.Status(204) // c.Status(204)
} }
...@@ -6,10 +6,9 @@ import ( ...@@ -6,10 +6,9 @@ import (
) )
var ( var (
SaveAccount func(account *Account) error SaveAccount func(account *Account) error
GetAccountByLogin func(emailOrName string) (*Account, error) GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int64) (*Account, error) GetAccount func(accountId int64) (*Account, error)
GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error)
) )
// Typed errors // Typed errors
...@@ -17,13 +16,6 @@ var ( ...@@ -17,13 +16,6 @@ var (
ErrAccountNotFound = errors.New("Account not found") ErrAccountNotFound = errors.New("Account not found")
) )
// Projection from User -> other account given access to
type OtherAccount struct {
Id int64
Email string
Role string
}
type Account struct { type Account struct {
Id int64 Id int64
Login string `xorm:"UNIQUE NOT NULL"` Login string `xorm:"UNIQUE NOT NULL"`
...@@ -41,6 +33,14 @@ type Account struct { ...@@ -41,6 +33,14 @@ type Account struct {
Updated time.Time Updated time.Time
} }
// api projection
type OtherAccountDTO struct {
Id int64 `json:"id"`
Email string `json:"email"`
Role string `json:"role"`
IsUsing bool `json:"isUsing"`
}
// api projection model // api projection model
type CollaboratorDTO struct { type CollaboratorDTO struct {
AccountId int64 `json:"accountId"` AccountId int64 `json:"accountId"`
...@@ -60,3 +60,9 @@ type GetAccountInfoQuery struct { ...@@ -60,3 +60,9 @@ type GetAccountInfoQuery struct {
Id int64 Id int64
Result AccountDTO Result AccountDTO
} }
// returns a view projection
type GetOtherAccountsQuery struct {
AccountId int64
Result []*OtherAccountDTO
}
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
func init() { func init() {
bus.AddHandler("sql", GetAccountInfo) bus.AddHandler("sql", GetAccountInfo)
bus.AddHandler("sql", AddCollaborator) bus.AddHandler("sql", AddCollaborator)
bus.AddHandler("sql", GetOtherAccounts)
} }
func GetAccountInfo(query *m.GetAccountInfoQuery) error { func GetAccountInfo(query *m.GetAccountInfoQuery) error {
...@@ -114,12 +115,12 @@ func GetAccountByLogin(emailOrLogin string) (*m.Account, error) { ...@@ -114,12 +115,12 @@ func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
return account, nil return account, nil
} }
func GetOtherAccountsFor(accountId int64) ([]*m.OtherAccount, error) { func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
collaborators := make([]*m.OtherAccount, 0) query.Result = make([]*m.OtherAccountDTO, 0)
sess := x.Table("collaborator") sess := x.Table("collaborator")
sess.Join("INNER", "account", "collaborator.for_account_id=account.id") sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
sess.Where("account_id=?", accountId) sess.Where("account_id=?", query.AccountId)
sess.Cols("collaborator.id", "collaborator.role", "account.email") sess.Cols("collaborator.id", "collaborator.role", "account.email")
err := sess.Find(&collaborators) err := sess.Find(&query.Result)
return collaborators, err return err
} }
...@@ -58,6 +58,14 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -58,6 +58,14 @@ func TestAccountDataAccess(t *testing.T) {
So(query.Result.Collaborators[0].Role, ShouldEqual, m.ROLE_READ_WRITE) So(query.Result.Collaborators[0].Role, ShouldEqual, m.ROLE_READ_WRITE)
So(query.Result.Collaborators[0].Email, ShouldEqual, "ac2@test.com") So(query.Result.Collaborators[0].Email, ShouldEqual, "ac2@test.com")
}) })
Convey("Can get other accounts", func() {
query := m.GetOtherAccountsQuery{AccountId: ac2.Id}
err := GetOtherAccounts(&query)
So(err, ShouldBeNil)
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
})
}) })
}) })
}) })
......
...@@ -38,7 +38,6 @@ func Init() { ...@@ -38,7 +38,6 @@ func Init() {
m.SaveAccount = SaveAccount m.SaveAccount = SaveAccount
m.GetAccount = GetAccount m.GetAccount = GetAccount
m.GetAccountByLogin = GetAccountByLogin m.GetAccountByLogin = GetAccountByLogin
m.GetOtherAccountsFor = GetOtherAccountsFor
m.GetDashboard = GetDashboard m.GetDashboard = GetDashboard
m.SaveDashboard = SaveDashboard m.SaveDashboard = SaveDashboard
m.SearchQuery = SearchQuery m.SearchQuery = SearchQuery
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment