|
|
@ -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) { |
|
|
|
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) { |
|
|
|
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) { |
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
|
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) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|