From d5a59ac6b7814ea776eba74674653060d87d045b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 19 Dec 2014 11:08:49 +0100 Subject: [PATCH] More migration to command/query and sql tests, looking good --- pkg/api/api_account.go | 88 +++++++++++++--------------- pkg/models/account.go | 28 +++++---- pkg/stores/sqlstore/accounts.go | 11 ++-- pkg/stores/sqlstore/accounts_test.go | 8 +++ pkg/stores/sqlstore/sqlstore.go | 1 - 5 files changed, 73 insertions(+), 63 deletions(-) diff --git a/pkg/api/api_account.go b/pkg/api/api_account.go index 734463c3d50..2ef03d5ee4f 100644 --- a/pkg/api/api_account.go +++ b/pkg/api/api_account.go @@ -1,7 +1,6 @@ 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) } diff --git a/pkg/models/account.go b/pkg/models/account.go index 11c26954690..4e8b9b635b5 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -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 +} diff --git a/pkg/stores/sqlstore/accounts.go b/pkg/stores/sqlstore/accounts.go index 909d36e4e6c..95d2f0eec61 100644 --- a/pkg/stores/sqlstore/accounts.go +++ b/pkg/stores/sqlstore/accounts.go @@ -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 } diff --git a/pkg/stores/sqlstore/accounts_test.go b/pkg/stores/sqlstore/accounts_test.go index ce830f568bd..1457707ae64 100644 --- a/pkg/stores/sqlstore/accounts_test.go +++ b/pkg/stores/sqlstore/accounts_test.go @@ -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") + }) }) }) }) diff --git a/pkg/stores/sqlstore/sqlstore.go b/pkg/stores/sqlstore/sqlstore.go index 95a05e0f178..f4807b0038d 100644 --- a/pkg/stores/sqlstore/sqlstore.go +++ b/pkg/stores/sqlstore/sqlstore.go @@ -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