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

This commit is contained in:
Torkel Ödegaard
2014-12-19 09:43:16 +01:00
parent e5811e29b1
commit 36c46112df
4 changed files with 107 additions and 34 deletions
+11 -21
View File
@@ -2,32 +2,22 @@ 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"
"github.com/torkelo/grafana-pro/pkg/models"
m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/utils"
)
func GetAccount(c *middleware.Context) {
model := dtos.AccountInfo{
Name: c.UserAccount.Name,
Email: c.UserAccount.Email,
}
query := m.GetAccountInfoQuery{Id: c.UserAccount.Id}
err := bus.Dispatch(&query)
collaborators, err := models.GetCollaboratorsForAccount(c.UserAccount.Id)
if err != nil {
c.JsonApiErr(500, "Failed to fetch collaboratos", err)
return
}
for _, collaborator := range collaborators {
model.Collaborators = append(model.Collaborators, &dtos.Collaborator{
AccountId: collaborator.AccountId,
Role: collaborator.Role,
Email: collaborator.Email,
})
}
c.JSON(200, model)
c.JSON(200, query.Result)
}
func AddCollaborator(c *middleware.Context) {
@@ -38,7 +28,7 @@ func AddCollaborator(c *middleware.Context) {
return
}
accountToAdd, err := models.GetAccountByLogin(model.Email)
accountToAdd, err := m.GetAccountByLogin(model.Email)
if err != nil {
c.JsonApiErr(404, "Collaborator not found", nil)
return
@@ -49,9 +39,9 @@ func AddCollaborator(c *middleware.Context) {
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 {
c.JsonApiErr(500, "Could not add collaborator", err)
return
@@ -62,7 +52,7 @@ func AddCollaborator(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 {
c.JSON(500, utils.DynMap{"message": err.Error()})
return
@@ -92,7 +82,7 @@ func SetUsingAccount(c *middleware.Context) {
usingAccountId := c.ParamsInt64(":id")
account := c.UserAccount
otherAccounts, err := models.GetOtherAccountsFor(c.UserAccount.Id)
otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
if err != nil {
c.JSON(500, utils.DynMap{"message": err.Error()})
@@ -113,7 +103,7 @@ func SetUsingAccount(c *middleware.Context) {
}
account.UsingAccountId = usingAccountId
err = models.SaveAccount(account)
err = m.SaveAccount(account)
if err != nil {
c.JSON(500, utils.DynMap{"message": err.Error()})
return