Commit 36c46112 by Torkel Ödegaard

Refactoring data access to command query model, and adding tests for sql code

parent e5811e29
...@@ -2,32 +2,22 @@ package api ...@@ -2,32 +2,22 @@ package api
import ( import (
"github.com/torkelo/grafana-pro/pkg/api/dtos" "github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/utils" "github.com/torkelo/grafana-pro/pkg/utils"
) )
func GetAccount(c *middleware.Context) { func GetAccount(c *middleware.Context) {
model := dtos.AccountInfo{ query := m.GetAccountInfoQuery{Id: c.UserAccount.Id}
Name: c.UserAccount.Name, err := bus.Dispatch(&query)
Email: c.UserAccount.Email,
}
collaborators, err := models.GetCollaboratorsForAccount(c.UserAccount.Id)
if err != nil { if err != nil {
c.JsonApiErr(500, "Failed to fetch collaboratos", err) c.JsonApiErr(500, "Failed to fetch collaboratos", err)
return return
} }
for _, collaborator := range collaborators { c.JSON(200, query.Result)
model.Collaborators = append(model.Collaborators, &dtos.Collaborator{
AccountId: collaborator.AccountId,
Role: collaborator.Role,
Email: collaborator.Email,
})
}
c.JSON(200, model)
} }
func AddCollaborator(c *middleware.Context) { func AddCollaborator(c *middleware.Context) {
...@@ -38,7 +28,7 @@ func AddCollaborator(c *middleware.Context) { ...@@ -38,7 +28,7 @@ func AddCollaborator(c *middleware.Context) {
return return
} }
accountToAdd, err := models.GetAccountByLogin(model.Email) accountToAdd, err := m.GetAccountByLogin(model.Email)
if err != nil { if err != nil {
c.JsonApiErr(404, "Collaborator not found", nil) c.JsonApiErr(404, "Collaborator not found", nil)
return return
...@@ -49,9 +39,9 @@ func AddCollaborator(c *middleware.Context) { ...@@ -49,9 +39,9 @@ func AddCollaborator(c *middleware.Context) {
return return
} }
var collaborator = models.NewCollaborator(accountToAdd.Id, c.UserAccount.Id, models.ROLE_READ_WRITE) var collaborator = m.NewCollaborator(accountToAdd.Id, c.UserAccount.Id, m.ROLE_READ_WRITE)
err = models.AddCollaborator(collaborator) err = m.AddCollaborator(collaborator)
if err != nil { if err != nil {
c.JsonApiErr(500, "Could not add collaborator", err) c.JsonApiErr(500, "Could not add collaborator", err)
return return
...@@ -62,7 +52,7 @@ func AddCollaborator(c *middleware.Context) { ...@@ -62,7 +52,7 @@ func AddCollaborator(c *middleware.Context) {
func GetOtherAccounts(c *middleware.Context) { func GetOtherAccounts(c *middleware.Context) {
otherAccounts, err := models.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
...@@ -92,7 +82,7 @@ func SetUsingAccount(c *middleware.Context) { ...@@ -92,7 +82,7 @@ func SetUsingAccount(c *middleware.Context) {
usingAccountId := c.ParamsInt64(":id") usingAccountId := c.ParamsInt64(":id")
account := c.UserAccount account := c.UserAccount
otherAccounts, err := models.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()})
...@@ -113,7 +103,7 @@ func SetUsingAccount(c *middleware.Context) { ...@@ -113,7 +103,7 @@ func SetUsingAccount(c *middleware.Context) {
} }
account.UsingAccountId = usingAccountId account.UsingAccountId = usingAccountId
err = models.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
......
...@@ -42,3 +42,23 @@ type Account struct { ...@@ -42,3 +42,23 @@ type Account struct {
Created time.Time Created time.Time
Updated time.Time Updated time.Time
} }
// api projection model
type CollaboratorDTO struct {
AccountId int64 `json:"accountId"`
Email string `json:"email"`
Role string `json:"role"`
}
// api view projection
type AccountDTO struct {
Email string `json:"email"`
Name string `json:"name"`
Collaborators []*CollaboratorDTO `json:"collaborators"`
}
// returns a view projection
type GetAccountInfoQuery struct {
Id int64
Result AccountDTO
}
package sqlstore package sqlstore
import "github.com/torkelo/grafana-pro/pkg/models" import (
"github.com/torkelo/grafana-pro/pkg/bus"
m "github.com/torkelo/grafana-pro/pkg/models"
)
func SaveAccount(account *models.Account) error { func init() {
bus.AddHandler("sql", GetAccountInfo)
}
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
}
func SaveAccount(account *m.Account) error {
var err error var err error
sess := x.NewSession() sess := x.NewSession()
...@@ -28,16 +59,16 @@ func SaveAccount(account *models.Account) error { ...@@ -28,16 +59,16 @@ func SaveAccount(account *models.Account) error {
return nil return nil
} }
func GetAccount(id int64) (*models.Account, error) { func GetAccount(id int64) (*m.Account, error) {
var err error var err error
var account models.Account var account m.Account
has, err := x.Id(id).Get(&account) has, err := x.Id(id).Get(&account)
if err != nil { if err != nil {
return nil, err return nil, err
} else if has == false { } else if has == false {
return nil, models.ErrAccountNotFound return nil, m.ErrAccountNotFound
} }
if account.UsingAccountId == 0 { if account.UsingAccountId == 0 {
...@@ -47,23 +78,23 @@ func GetAccount(id int64) (*models.Account, error) { ...@@ -47,23 +78,23 @@ func GetAccount(id int64) (*models.Account, error) {
return &account, nil return &account, nil
} }
func GetAccountByLogin(emailOrLogin string) (*models.Account, error) { func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
var err error var err error
account := &models.Account{Login: emailOrLogin} account := &m.Account{Login: emailOrLogin}
has, err := x.Get(account) has, err := x.Get(account)
if err != nil { if err != nil {
return nil, err return nil, err
} else if has == false { } else if has == false {
return nil, models.ErrAccountNotFound return nil, m.ErrAccountNotFound
} }
return account, nil return account, nil
} }
func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, error) { func GetCollaboratorsForAccount(accountId int64) ([]*m.CollaboratorInfo, error) {
collaborators := make([]*models.CollaboratorInfo, 0) collaborators := make([]*m.CollaboratorInfo, 0)
sess := x.Table("collaborator") sess := x.Table("collaborator")
sess.Join("INNER", "account", "account.id=collaborator.account_Id") sess.Join("INNER", "account", "account.id=collaborator.account_Id")
...@@ -73,7 +104,7 @@ func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, er ...@@ -73,7 +104,7 @@ func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, er
return collaborators, err return collaborators, err
} }
func AddCollaborator(collaborator *models.Collaborator) error { func AddCollaborator(collaborator *m.Collaborator) error {
var err error var err error
sess := x.NewSession() sess := x.NewSession()
...@@ -93,8 +124,8 @@ func AddCollaborator(collaborator *models.Collaborator) error { ...@@ -93,8 +124,8 @@ func AddCollaborator(collaborator *models.Collaborator) error {
return nil return nil
} }
func GetOtherAccountsFor(accountId int64) ([]*models.OtherAccount, error) { func GetOtherAccountsFor(accountId int64) ([]*m.OtherAccount, error) {
collaborators := make([]*models.OtherAccount, 0) collaborators := make([]*m.OtherAccount, 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=?", accountId)
......
package sqlstore
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
m "github.com/torkelo/grafana-pro/pkg/models"
)
func TestAccountDataAccess(t *testing.T) {
Convey("Testing Account DB Access", t, func() {
InitTestDB(t)
Convey("Can save account", func() {
account := m.Account{
Login: "login",
Email: "login@test.com",
Name: "name",
}
err := SaveAccount(&account)
query := m.GetAccountInfoQuery{Id: account.Id}
err = GetAccountInfo(&query)
So(err, ShouldBeNil)
So(query.Result.Name, ShouldEqual, "name")
})
})
}
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