Commit 5ba24236 by Torkel Ödegaard

Worked on account update view

parent 9a641ee7
Subproject commit ab7e2f89fb596d6adc9a28177feb97574e860ce7
Subproject commit a6b0856020847b6530b9d1505fe39f347988be57
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment