mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Worked on account update view
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user