Commit d5a59ac6 by Torkel Ödegaard

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

parent ccba9554
package api
import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models"
......@@ -53,63 +52,60 @@ func AddCollaborator(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 {
c.JSON(500, utils.DynMap{"message": err.Error()})
return
}
var result []*dtos.OtherAccount
result = append(result, &dtos.OtherAccount{
Id: c.UserAccount.Id,
Role: "owner",
IsUsing: c.UserAccount.Id == c.UserAccount.UsingAccountId,
Name: c.UserAccount.Email,
result := append(query.Result, &m.OtherAccountDTO{
Id: c.UserAccount.Id,
Role: "owner",
Email: c.UserAccount.Email,
})
for _, other := range otherAccounts {
result = append(result, &dtos.OtherAccount{
Id: other.Id,
Role: other.Role,
Name: other.Email,
IsUsing: other.Id == c.UserAccount.UsingAccountId,
})
for _, ac := range result {
if ac.Id == c.UserAccount.UsingAccountId {
ac.IsUsing = true
break
}
}
c.JSON(200, result)
}
func SetUsingAccount(c *middleware.Context) {
usingAccountId := c.ParamsInt64(":id")
account := c.UserAccount
otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
if err != nil {
c.JSON(500, utils.DynMap{"message": err.Error()})
return
}
// validate that the account id in the list
valid := false
for _, other := range otherAccounts {
if other.Id == usingAccountId {
valid = true
}
}
if !valid {
c.Status(401)
return
}
account.UsingAccountId = usingAccountId
err = m.SaveAccount(account)
if err != nil {
c.JSON(500, utils.DynMap{"message": err.Error()})
return
}
c.Status(204)
// usingAccountId := c.ParamsInt64(":id")
//
// account := c.UserAccount
// otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
//
// if err != nil {
// c.JSON(500, utils.DynMap{"message": err.Error()})
// return
// }
//
// // validate that the account id in the list
// valid := false
// for _, other := range otherAccounts {
// if other.Id == usingAccountId {
// valid = true
// }
// }
//
// if !valid {
// c.Status(401)
// return
// }
//
// account.UsingAccountId = usingAccountId
// err = m.SaveAccount(account)
// if err != nil {
// c.JSON(500, utils.DynMap{"message": err.Error()})
// return
// }
//
// c.Status(204)
}
......@@ -6,10 +6,9 @@ import (
)
var (
SaveAccount func(account *Account) error
GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int64) (*Account, error)
GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error)
SaveAccount func(account *Account) error
GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int64) (*Account, error)
)
// Typed errors
......@@ -17,13 +16,6 @@ var (
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 {
Id int64
Login string `xorm:"UNIQUE NOT NULL"`
......@@ -41,6 +33,14 @@ type Account struct {
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
type CollaboratorDTO struct {
AccountId int64 `json:"accountId"`
......@@ -60,3 +60,9 @@ type GetAccountInfoQuery struct {
Id int64
Result AccountDTO
}
// returns a view projection
type GetOtherAccountsQuery struct {
AccountId int64
Result []*OtherAccountDTO
}
......@@ -12,6 +12,7 @@ import (
func init() {
bus.AddHandler("sql", GetAccountInfo)
bus.AddHandler("sql", AddCollaborator)
bus.AddHandler("sql", GetOtherAccounts)
}
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
......@@ -114,12 +115,12 @@ func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
return account, nil
}
func GetOtherAccountsFor(accountId int64) ([]*m.OtherAccount, error) {
collaborators := make([]*m.OtherAccount, 0)
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
query.Result = make([]*m.OtherAccountDTO, 0)
sess := x.Table("collaborator")
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")
err := sess.Find(&collaborators)
return collaborators, err
err := sess.Find(&query.Result)
return err
}
......@@ -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].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() {
m.SaveAccount = SaveAccount
m.GetAccount = GetAccount
m.GetAccountByLogin = GetAccountByLogin
m.GetOtherAccountsFor = GetOtherAccountsFor
m.GetDashboard = GetDashboard
m.SaveDashboard = SaveDashboard
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