diff --git a/pkg/api/api.go b/pkg/api/api.go index 1a876c3bc70..6c56e6aa9f1 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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)) diff --git a/pkg/api/dtos/org.go b/pkg/api/dtos/org.go new file mode 100644 index 00000000000..b4bb26ca78a --- /dev/null +++ b/pkg/api/dtos/org.go @@ -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"` +} diff --git a/pkg/api/org.go b/pkg/api/org.go index 2c9e8895131..6ed4a7c4a14 100644 --- a/pkg/api/org.go +++ b/pkg/api/org.go @@ -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 { diff --git a/pkg/models/address.go b/pkg/models/address.go new file mode 100644 index 00000000000..0cf8aaf7ddb --- /dev/null +++ b/pkg/models/address.go @@ -0,0 +1,10 @@ +package models + +type Address 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"` +} diff --git a/pkg/models/org.go b/pkg/models/org.go index 30958688ae3..2a9fa8efa04 100644 --- a/pkg/models/org.go +++ b/pkg/models/org.go @@ -15,6 +15,14 @@ type Org struct { Id int64 Version int Name string + + Address1 string + Address2 string + City string + ZipCode string + State string + Country string + Created time.Time Updated time.Time } @@ -35,8 +43,13 @@ type DeleteOrgCommand struct { } type UpdateOrgCommand struct { - Name string `json:"name" binding:"Required"` - OrgId int64 `json:"-"` + Name string + OrgId int64 +} + +type UpdateOrgAddressCommand struct { + OrgId int64 + Address } type GetOrgByIdQuery struct { @@ -63,6 +76,12 @@ type OrgDTO struct { Name string `json:"name"` } +type OrgDetailsDTO struct { + Id int64 `json:"id"` + Name string `json:"name"` + Address Address `json:"address"` +} + type UserOrgDTO struct { OrgId int64 `json:"orgId"` Name string `json:"name"` diff --git a/pkg/services/sqlstore/org.go b/pkg/services/sqlstore/org.go index 6ef0a4de91a..26bb4a92ffb 100644 --- a/pkg/services/sqlstore/org.go +++ b/pkg/services/sqlstore/org.go @@ -3,6 +3,7 @@ package sqlstore import ( "time" + "github.com/Unknwon/log" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/events" m "github.com/grafana/grafana/pkg/models" @@ -12,6 +13,7 @@ func init() { bus.AddHandler("sql", GetOrgById) bus.AddHandler("sql", CreateOrg) bus.AddHandler("sql", UpdateOrg) + bus.AddHandler("sql", UpdateOrgAddress) bus.AddHandler("sql", GetOrgByName) bus.AddHandler("sql", SearchOrgs) bus.AddHandler("sql", DeleteOrg) @@ -146,6 +148,35 @@ func UpdateOrg(cmd *m.UpdateOrgCommand) error { }) } +func UpdateOrgAddress(cmd *m.UpdateOrgAddressCommand) error { + return inTransaction2(func(sess *session) error { + log.Info("address %s", cmd.Address1) + + org := m.Org{ + Address1: cmd.Address1, + Address2: cmd.Address2, + City: cmd.City, + ZipCode: cmd.ZipCode, + State: cmd.State, + Country: cmd.Country, + + Updated: time.Now(), + } + + if _, err := sess.Id(cmd.OrgId).Update(&org); err != nil { + return err + } + + sess.publishAfterCommit(&events.OrgUpdated{ + Timestamp: org.Updated, + Id: org.Id, + Name: org.Name, + }) + + return nil + }) +} + func DeleteOrg(cmd *m.DeleteOrgCommand) error { return inTransaction2(func(sess *session) error { diff --git a/public/app/features/org/orgDetailsCtrl.js b/public/app/features/org/orgDetailsCtrl.js index 97c89d3ffed..b900e295b99 100644 --- a/public/app/features/org/orgDetailsCtrl.js +++ b/public/app/features/org/orgDetailsCtrl.js @@ -15,13 +15,20 @@ function (angular) { $scope.getOrgInfo = function() { backendSrv.get('/api/org').then(function(org) { $scope.org = org; + $scope.address = org.address; contextSrv.user.orgName = org.name; }); }; $scope.update = function() { if (!$scope.orgForm.$valid) { return; } - backendSrv.put('/api/org', $scope.org).then($scope.getOrgInfo); + var data = {name: $scope.org.name}; + backendSrv.put('/api/org', data).then($scope.getOrgInfo); + }; + + $scope.updateAddress = function() { + if (!$scope.addressForm.$valid) { return; } + backendSrv.put('/api/org/address', $scope.address).then($scope.getOrgInfo); }; $scope.init(); diff --git a/public/app/features/org/partials/orgDetails.html b/public/app/features/org/partials/orgDetails.html index 0537154aa9f..db965d27633 100644 --- a/public/app/features/org/partials/orgDetails.html +++ b/public/app/features/org/partials/orgDetails.html @@ -7,59 +7,90 @@
-

Organization info

+

Organization

-
-
-
+
+
Info
+ + +
  • - Org. name + Org. name
  • - -
  • -
-
-
-
-
    -
  • - Address 1 -
  • -
  • - -
  • -
-
-
-
-
    -
  • - Address 2 -
  • -
  • - -
  • -
-
-
-
-
    -
  • - City -
  • -
  • - +
-
-
- - +
+ + +
+ +
+
Address
+ +
+
+
    +
  • + Address 1 +
  • +
  • + +
  • +
  • + Address 2 +
  • +
  • + +
  • +
+
+
+
+
    +
  • + City +
  • +
  • + +
  • +
  • + Postal code +
  • +
  • + +
  • +
+
+
+
+
    +
  • + State +
  • +
  • + +
  • +
  • + Country +
  • +
  • + +
  • +
+
+
+ +
+ + +
+