diff --git a/grafana b/grafana index 5b93e09714d..9d1dacb8d41 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit 5b93e09714dbee6c1c181daf0182704334d8c6be +Subproject commit 9d1dacb8d417cac2cc66797e5dae6deba36c3080 diff --git a/pkg/api/account.go b/pkg/api/account.go index 5a871ac852d..94de464a30e 100644 --- a/pkg/api/account.go +++ b/pkg/api/account.go @@ -18,51 +18,22 @@ func GetAccount(c *middleware.Context) { c.JSON(200, query.Result) } -func AddCollaborator(c *middleware.Context) { - var cmd m.AddCollaboratorCommand +func UpdateAccount(c *middleware.Context) { + cmd := m.UpdateAccountCommand{} if !c.JsonBody(&cmd) { c.JsonApiErr(400, "Invalid request", nil) return } - userQuery := m.GetAccountByLoginQuery{Login: cmd.Email} - err := bus.Dispatch(&userQuery) - if err != nil { - c.JsonApiErr(404, "Collaborator not found", nil) - return - } - - accountToAdd := userQuery.Result - - if accountToAdd.Id == c.UserAccount.Id { - c.JsonApiErr(400, "Cannot add yourself as collaborator", nil) - return - } - cmd.AccountId = c.UserAccount.Id - cmd.CollaboratorId = accountToAdd.Id - cmd.Role = m.ROLE_READ_WRITE - - err = bus.Dispatch(&cmd) - if err != nil { - c.JsonApiErr(500, "Could not add collaborator", err) - return - } - - c.JsonOK("Collaborator added") -} - -func RemoveCollaborator(c *middleware.Context) { - collaboratorId := c.ParamsInt64(":id") - - cmd := m.RemoveCollaboratorCommand{AccountId: c.UserAccount.Id, CollaboratorId: collaboratorId} if err := bus.Dispatch(&cmd); err != nil { - c.JsonApiErr(500, "Failed to remove collaborator", err) + c.JsonApiErr(400, "Failed to update account", nil) + return } - c.JsonOK("Collaborator removed") + c.JsonOK("Account updated") } func GetOtherAccounts(c *middleware.Context) { diff --git a/pkg/api/api.go b/pkg/api/api.go index 44e44600292..2829a4d1da1 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -34,7 +34,9 @@ func Register(m *macaron.Macaron) { // account m.Group("/account", func() { m.Get("/", GetAccount) + m.Post("/", UpdateAccount) m.Put("/collaborators", AddCollaborator) + m.Get("/collaborators", GetCollaborators) m.Delete("/collaborators/:id", RemoveCollaborator) m.Post("/using/:id", SetUsingAccount) m.Get("/others", GetOtherAccounts) diff --git a/pkg/api/collaborators.go b/pkg/api/collaborators.go new file mode 100644 index 00000000000..178a78e0acd --- /dev/null +++ b/pkg/api/collaborators.go @@ -0,0 +1,64 @@ +package api + +import ( + "github.com/torkelo/grafana-pro/pkg/bus" + "github.com/torkelo/grafana-pro/pkg/middleware" + m "github.com/torkelo/grafana-pro/pkg/models" +) + +func AddCollaborator(c *middleware.Context) { + var cmd m.AddCollaboratorCommand + + if !c.JsonBody(&cmd) { + c.JsonApiErr(400, "Invalid request", nil) + return + } + + userQuery := m.GetAccountByLoginQuery{Login: cmd.Email} + err := bus.Dispatch(&userQuery) + if err != nil { + c.JsonApiErr(404, "Collaborator not found", nil) + return + } + + accountToAdd := userQuery.Result + + if accountToAdd.Id == c.UserAccount.Id { + c.JsonApiErr(400, "Cannot add yourself as collaborator", nil) + return + } + + cmd.AccountId = c.UserAccount.Id + cmd.CollaboratorId = accountToAdd.Id + cmd.Role = m.ROLE_READ_WRITE + + err = bus.Dispatch(&cmd) + if err != nil { + c.JsonApiErr(500, "Could not add collaborator", err) + return + } + + c.JsonOK("Collaborator added") +} + +func GetCollaborators(c *middleware.Context) { + query := m.GetCollaboratorsQuery{AccountId: c.UserAccount.Id} + if err := bus.Dispatch(&query); err != nil { + c.JsonApiErr(500, "Failed to get collaborators", err) + return + } + + c.JSON(200, query.Result) +} + +func RemoveCollaborator(c *middleware.Context) { + collaboratorId := c.ParamsInt64(":id") + + cmd := m.RemoveCollaboratorCommand{AccountId: c.UserAccount.Id, CollaboratorId: collaboratorId} + + if err := bus.Dispatch(&cmd); err != nil { + c.JsonApiErr(500, "Failed to remove collaborator", err) + } + + c.JsonOK("Collaborator removed") +} diff --git a/pkg/models/account.go b/pkg/models/account.go index e2bc96e8021..1f5e5d0c05c 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -42,6 +42,14 @@ type CreateAccountCommand struct { Result Account `json:"-"` } +type UpdateAccountCommand struct { + Email string `json:"email" binding:"required"` + Login string `json:"login"` + Name string `json:"name"` + + AccountId int64 `json:"-"` +} + type SetUsingAccountCommand struct { AccountId int64 UsingAccountId int64 @@ -88,12 +96,6 @@ type OtherAccountDTO struct { IsUsing bool `json:"isUsing"` } -type CollaboratorDTO struct { - CollaboratorId int64 `json:"id"` - Email string `json:"email"` - Role string `json:"role"` -} - type AccountSearchHitDTO struct { Id int64 `json:"id"` Name string `json:"name"` @@ -103,7 +105,7 @@ type AccountSearchHitDTO struct { } type AccountDTO struct { - Email string `json:"email"` - Name string `json:"name"` - Collaborators []*CollaboratorDTO `json:"collaborators"` + Email string `json:"email"` + Name string `json:"name"` + Login string `json:"login"` } diff --git a/pkg/models/collaborator.go b/pkg/models/collaborator.go index 7babd1e95d6..2f6b2a17404 100644 --- a/pkg/models/collaborator.go +++ b/pkg/models/collaborator.go @@ -21,6 +21,19 @@ type Collaborator struct { Updated time.Time } +func NewCollaborator(accountId int64, collaboratorId int64, role RoleType) *Collaborator { + return &Collaborator{ + AccountId: accountId, + CollaboratorId: collaboratorId, + Role: role, + Created: time.Now(), + Updated: time.Now(), + } +} + +// --------------------- +// COMMANDS + type RemoveCollaboratorCommand struct { CollaboratorId int64 AccountId int64 @@ -33,12 +46,20 @@ type AddCollaboratorCommand struct { Role RoleType `json:"-"` } -func NewCollaborator(accountId int64, collaboratorId int64, role RoleType) *Collaborator { - return &Collaborator{ - AccountId: accountId, - CollaboratorId: collaboratorId, - Role: role, - Created: time.Now(), - Updated: time.Now(), - } +// ---------------------- +// QUERIES + +type GetCollaboratorsQuery struct { + AccountId int64 + Result []*CollaboratorDTO +} + +// ---------------------- +// Projections and DTOs + +type CollaboratorDTO struct { + CollaboratorId int64 `json:"id"` + Email string `json:"email"` + Login string `json:"login"` + Role string `json:"role"` } diff --git a/pkg/services/sqlstore/accounts.go b/pkg/services/sqlstore/accounts.go index 61a445a7275..6b4ceefbc30 100644 --- a/pkg/services/sqlstore/accounts.go +++ b/pkg/services/sqlstore/accounts.go @@ -18,9 +18,8 @@ func init() { bus.AddHandler("sql", GetAccountById) bus.AddHandler("sql", GetAccountByLogin) bus.AddHandler("sql", GetAccountByToken) - bus.AddHandler("sql", AddCollaborator) - bus.AddHandler("sql", RemoveCollaborator) bus.AddHandler("sql", SearchAccounts) + bus.AddHandler("sql", UpdateAccount) } func CreateAccount(cmd *m.CreateAccountCommand) error { @@ -44,6 +43,21 @@ func CreateAccount(cmd *m.CreateAccountCommand) error { }) } +func UpdateAccount(cmd *m.UpdateAccountCommand) error { + return inTransaction(func(sess *xorm.Session) error { + + account := m.Account{ + Email: cmd.Email, + Login: cmd.Login, + Name: cmd.Name, + Updated: time.Now(), + } + + _, err := sess.Id(cmd.AccountId).Update(&account) + return err + }) +} + func SetUsingAccount(cmd *m.SetUsingAccountCommand) error { return inTransaction(func(sess *xorm.Session) error { account := m.Account{} @@ -66,35 +80,14 @@ func GetAccountInfo(query *m.GetAccountInfoQuery) error { } query.Result = m.AccountDTO{ - Name: account.Name, - Email: account.Email, - Collaborators: make([]*m.CollaboratorDTO, 0), + Name: account.Name, + Email: account.Email, + Login: account.Login, } - sess := x.Table("collaborator") - sess.Join("INNER", "account", "account.id=collaborator.collaborator_id") - sess.Where("collaborator.account_id=?", query.Id) - err = sess.Find(&query.Result.Collaborators) - return err } -func AddCollaborator(cmd *m.AddCollaboratorCommand) error { - return inTransaction(func(sess *xorm.Session) error { - - entity := m.Collaborator{ - AccountId: cmd.AccountId, - CollaboratorId: cmd.CollaboratorId, - Role: cmd.Role, - Created: time.Now(), - Updated: time.Now(), - } - - _, err := sess.Insert(&entity) - return err - }) -} - func GetAccountById(query *m.GetAccountByIdQuery) error { var err error @@ -165,14 +158,6 @@ func GetAccountByLogin(query *m.GetAccountByLoginQuery) error { return nil } -func RemoveCollaborator(cmd *m.RemoveCollaboratorCommand) error { - return inTransaction(func(sess *xorm.Session) error { - var rawSql = "DELETE FROM collaborator WHERE collaborator_id=? and account_id=?" - _, err := sess.Exec(rawSql, cmd.CollaboratorId, cmd.AccountId) - return err - }) -} - func GetOtherAccounts(query *m.GetOtherAccountsQuery) error { query.Result = make([]*m.OtherAccountDTO, 0) sess := x.Table("collaborator") diff --git a/pkg/services/sqlstore/collaborators.go b/pkg/services/sqlstore/collaborators.go new file mode 100644 index 00000000000..bb7fe897c32 --- /dev/null +++ b/pkg/services/sqlstore/collaborators.go @@ -0,0 +1,51 @@ +package sqlstore + +import ( + "time" + + "github.com/go-xorm/xorm" + + "github.com/torkelo/grafana-pro/pkg/bus" + m "github.com/torkelo/grafana-pro/pkg/models" +) + +func init() { + bus.AddHandler("sql", AddCollaborator) + bus.AddHandler("sql", RemoveCollaborator) + bus.AddHandler("sql", GetCollaborators) +} + +func AddCollaborator(cmd *m.AddCollaboratorCommand) error { + return inTransaction(func(sess *xorm.Session) error { + + entity := m.Collaborator{ + AccountId: cmd.AccountId, + CollaboratorId: cmd.CollaboratorId, + Role: cmd.Role, + Created: time.Now(), + Updated: time.Now(), + } + + _, err := sess.Insert(&entity) + return err + }) +} + +func GetCollaborators(query *m.GetCollaboratorsQuery) error { + query.Result = make([]*m.CollaboratorDTO, 0) + sess := x.Table("collaborator") + sess.Join("INNER", "account", "collaborator.collaborator_id=account.id") + sess.Where("collaborator.account_id=?", query.AccountId) + sess.Cols("collaborator.collaborator_id", "collaborator.role", "account.email", "account.login") + + err := sess.Find(&query.Result) + return err +} + +func RemoveCollaborator(cmd *m.RemoveCollaboratorCommand) error { + return inTransaction(func(sess *xorm.Session) error { + var rawSql = "DELETE FROM collaborator WHERE collaborator_id=? and account_id=?" + _, err := sess.Exec(rawSql, cmd.CollaboratorId, cmd.AccountId) + return err + }) +}