diff --git a/grafana b/grafana index ab7e2f89fb5..a6b08560208 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit ab7e2f89fb596d6adc9a28177feb97574e860ce7 +Subproject commit a6b0856020847b6530b9d1505fe39f347988be57 diff --git a/pkg/api/account.go b/pkg/api/account.go index 6d665fbbdbb..16c359faf80 100644 --- a/pkg/api/account.go +++ b/pkg/api/account.go @@ -6,13 +6,45 @@ import ( m "github.com/torkelo/grafana-pro/pkg/models" ) +func GetAccount(c *middleware.Context) { + query := m.GetAccountByIdQuery{Id: c.AccountId} + + if err := bus.Dispatch(&query); err != nil { + if err == m.ErrAccountNotFound { + c.JsonApiErr(404, "Account not found", err) + return + } + + c.JsonApiErr(500, "Failed to get account", err) + return + } + + account := m.AccountDTO{ + Id: query.Result.Id, + Name: query.Result.Name, + } + + c.JSON(200, &account) +} + func CreateAccount(c *middleware.Context, cmd m.CreateAccountCommand) { cmd.UserId = c.UserId if err := bus.Dispatch(&cmd); err != nil { - c.JsonApiErr(500, "Failed to create account", nil) + c.JsonApiErr(500, "Failed to create account", err) return } c.JsonOK("Account created") } + +func UpdateAccount(c *middleware.Context, cmd m.UpdateAccountCommand) { + cmd.AccountId = c.AccountId + + if err := bus.Dispatch(&cmd); err != nil { + c.JsonApiErr(500, "Failed to update account", err) + return + } + + c.JsonOK("Account updated") +} diff --git a/pkg/api/api.go b/pkg/api/api.go index 6b4c2c44e31..71837c0fce6 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -49,7 +49,9 @@ func Register(r *macaron.Macaron) { // account r.Group("/account", func() { + r.Get("/", GetAccount) r.Post("/", bind(m.CreateAccountCommand{}), CreateAccount) + r.Put("/", bind(m.UpdateAccountCommand{}), UpdateAccount) r.Post("/users", bind(m.AddAccountUserCommand{}), AddAccountUser) r.Get("/users", GetAccountUsers) r.Delete("/users/:id", RemoveAccountUser) diff --git a/pkg/models/account.go b/pkg/models/account.go index 2d8a9e2bb6c..43afb7d7488 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -29,7 +29,7 @@ type CreateAccountCommand struct { } type UpdateAccountCommand struct { - Name string `json:"name"` + Name string `json:"name" binding:"Required"` AccountId int64 `json:"-"` } @@ -43,6 +43,11 @@ type GetAccountByIdQuery struct { Result *Account } +type AccountDTO struct { + Id int64 `json:"id"` + Name string `json:"name"` +} + type UserAccountDTO struct { AccountId int64 `json:"accountId"` Name string `json:"name"` diff --git a/pkg/services/sqlstore/account.go b/pkg/services/sqlstore/account.go index c78859a7132..be3bb0d8b8f 100644 --- a/pkg/services/sqlstore/account.go +++ b/pkg/services/sqlstore/account.go @@ -10,11 +10,27 @@ import ( ) func init() { + bus.AddHandler("sql", GetAccount) bus.AddHandler("sql", CreateAccount) bus.AddHandler("sql", SetUsingAccount) bus.AddHandler("sql", UpdateAccount) } +func GetAccount(query *m.GetAccountByIdQuery) error { + var account m.Account + exists, err := x.Id(query.Id).Get(&account) + if err != nil { + return err + } + + if !exists { + return m.ErrAccountNotFound + } + + query.Result = &account + return nil +} + func CreateAccount(cmd *m.CreateAccountCommand) error { return inTransaction(func(sess *xorm.Session) error {