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

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 {