account.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. m "github.com/grafana/grafana/pkg/models"
  6. )
  7. func GetAccount(c *middleware.Context) {
  8. query := m.GetAccountByIdQuery{Id: c.AccountId}
  9. if err := bus.Dispatch(&query); err != nil {
  10. if err == m.ErrAccountNotFound {
  11. c.JsonApiErr(404, "Account not found", err)
  12. return
  13. }
  14. c.JsonApiErr(500, "Failed to get account", err)
  15. return
  16. }
  17. account := m.AccountDTO{
  18. Id: query.Result.Id,
  19. Name: query.Result.Name,
  20. }
  21. c.JSON(200, &account)
  22. }
  23. func CreateAccount(c *middleware.Context, cmd m.CreateAccountCommand) {
  24. cmd.UserId = c.UserId
  25. if err := bus.Dispatch(&cmd); err != nil {
  26. c.JsonApiErr(500, "Failed to create account", err)
  27. return
  28. }
  29. c.JsonOK("Account created")
  30. }
  31. func UpdateAccount(c *middleware.Context, cmd m.UpdateAccountCommand) {
  32. cmd.AccountId = c.AccountId
  33. if err := bus.Dispatch(&cmd); err != nil {
  34. c.JsonApiErr(500, "Failed to update account", err)
  35. return
  36. }
  37. c.JsonOK("Account updated")
  38. }