feat(organization): added update org address to http api and to org details settings view, closes #2672

This commit is contained in:
Torkel Ödegaard
2015-09-08 14:22:44 +02:00
parent daf64421f2
commit fad1d4cf98
8 changed files with 212 additions and 59 deletions
+4 -2
View File
@@ -93,7 +93,8 @@ func Register(r *macaron.Macaron) {
// current org
r.Group("/org", func() {
r.Get("/", wrap(GetOrgCurrent))
r.Put("/", bind(m.UpdateOrgCommand{}), wrap(UpdateOrgCurrent))
r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrgCurrent))
r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddressCurrent))
r.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
r.Get("/users", wrap(GetOrgUsersForCurrentOrg))
r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUserForCurrentOrg))
@@ -114,7 +115,8 @@ func Register(r *macaron.Macaron) {
// orgs (admin routes)
r.Group("/orgs/:orgId", func() {
r.Get("/", wrap(GetOrgById))
r.Put("/", bind(m.UpdateOrgCommand{}), wrap(UpdateOrg))
r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrg))
r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddress))
r.Delete("/", wrap(DeleteOrgById))
r.Get("/users", wrap(GetOrgUsers))
r.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUser))
+14
View File
@@ -0,0 +1,14 @@
package dtos
type UpdateOrgForm struct {
Name string `json:"name" binding:"Required"`
}
type UpdateOrgAddressForm struct {
Address1 string `json:"address1"`
Address2 string `json:"address2"`
City string `json:"city"`
ZipCode string `json:"zipcode"`
State string `json:"state"`
Country string `json:"country"`
}
+50 -11
View File
@@ -1,6 +1,7 @@
package api
import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
@@ -30,12 +31,21 @@ func getOrgHelper(orgId int64) Response {
return ApiError(500, "Failed to get organization", err)
}
org := m.OrgDTO{
Id: query.Result.Id,
Name: query.Result.Name,
org := query.Result
result := m.OrgDetailsDTO{
Id: org.Id,
Name: org.Name,
Address: m.Address{
Address1: org.Address1,
Address2: org.Address2,
City: org.City,
ZipCode: org.ZipCode,
State: org.State,
Country: org.Country,
},
}
return Json(200, &org)
return Json(200, &result)
}
// POST /api/orgs
@@ -61,18 +71,17 @@ func CreateOrg(c *middleware.Context, cmd m.CreateOrgCommand) Response {
}
// PUT /api/org
func UpdateOrgCurrent(c *middleware.Context, cmd m.UpdateOrgCommand) Response {
cmd.OrgId = c.OrgId
return updateOrgHelper(cmd)
func UpdateOrgCurrent(c *middleware.Context, form dtos.UpdateOrgForm) Response {
return updateOrgHelper(form, c.OrgId)
}
// PUT /api/orgs/:orgId
func UpdateOrg(c *middleware.Context, cmd m.UpdateOrgCommand) Response {
cmd.OrgId = c.ParamsInt64(":orgId")
return updateOrgHelper(cmd)
func UpdateOrg(c *middleware.Context, form dtos.UpdateOrgForm) Response {
return updateOrgHelper(form, c.ParamsInt64(":orgId"))
}
func updateOrgHelper(cmd m.UpdateOrgCommand) Response {
func updateOrgHelper(form dtos.UpdateOrgForm, orgId int64) Response {
cmd := m.UpdateOrgCommand{Name: form.Name, OrgId: orgId}
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrOrgNameTaken {
return ApiError(400, "Organization name taken", err)
@@ -83,6 +92,36 @@ func updateOrgHelper(cmd m.UpdateOrgCommand) Response {
return ApiSuccess("Organization updated")
}
// PUT /api/org/address
func UpdateOrgAddressCurrent(c *middleware.Context, form dtos.UpdateOrgAddressForm) Response {
return updateOrgAddressHelper(form, c.OrgId)
}
// PUT /api/orgs/:orgId/address
func UpdateOrgAddress(c *middleware.Context, form dtos.UpdateOrgAddressForm) Response {
return updateOrgAddressHelper(form, c.ParamsInt64(":orgId"))
}
func updateOrgAddressHelper(form dtos.UpdateOrgAddressForm, orgId int64) Response {
cmd := m.UpdateOrgAddressCommand{
OrgId: orgId,
Address: m.Address{
Address1: form.Address1,
Address2: form.Address2,
City: form.City,
State: form.State,
ZipCode: form.ZipCode,
Country: form.Country,
},
}
if err := bus.Dispatch(&cmd); err != nil {
return ApiError(500, "Failed to update org address", err)
}
return ApiSuccess("Address updated")
}
// GET /api/orgs/:orgId
func DeleteOrgById(c *middleware.Context) Response {
if err := bus.Dispatch(&m.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil {