Commit 33543651 by Torkel Ödegaard

Api handler refactoring using the wrap and response func/type, fixed small issue…

Api handler refactoring using the wrap and response func/type, fixed small issue in influxdb 0.9 response handling
parent a39e484a
...@@ -58,11 +58,11 @@ func Register(r *macaron.Macaron) { ...@@ -58,11 +58,11 @@ func Register(r *macaron.Macaron) {
r.Group("/user", func() { r.Group("/user", func() {
r.Get("/", wrap(GetSignedInUser)) r.Get("/", wrap(GetSignedInUser))
r.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser)) r.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser))
r.Post("/using/:id", UserSetUsingOrg) r.Post("/using/:id", wrap(UserSetUsingOrg))
r.Get("/orgs", wrap(GetSignedInUserOrgList)) r.Get("/orgs", wrap(GetSignedInUserOrgList))
r.Post("/stars/dashboard/:id", StarDashboard) r.Post("/stars/dashboard/:id", wrap(StarDashboard))
r.Delete("/stars/dashboard/:id", UnstarDashboard) r.Delete("/stars/dashboard/:id", wrap(UnstarDashboard))
r.Put("/password", bind(m.ChangeUserPasswordCommand{}), ChangeUserPassword) r.Put("/password", bind(m.ChangeUserPasswordCommand{}), wrap(ChangeUserPassword))
}) })
// users (admin permission required) // users (admin permission required)
......
...@@ -6,45 +6,35 @@ import ( ...@@ -6,45 +6,35 @@ import (
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
) )
func StarDashboard(c *middleware.Context) { func StarDashboard(c *middleware.Context) Response {
if !c.IsSignedIn { if !c.IsSignedIn {
c.JsonApiErr(412, "You need to sign in to star dashboards", nil) return ApiError(412, "You need to sign in to star dashboards", nil)
return
} }
var cmd = m.StarDashboardCommand{ cmd := m.StarDashboardCommand{UserId: c.UserId, DashboardId: c.ParamsInt64(":id")}
UserId: c.UserId,
DashboardId: c.ParamsInt64(":id"),
}
if cmd.DashboardId <= 0 { if cmd.DashboardId <= 0 {
c.JsonApiErr(400, "Missing dashboard id", nil) return ApiError(400, "Missing dashboard id", nil)
return
} }
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to star dashboard", err) return ApiError(500, "Failed to star dashboard", err)
return
} }
c.JsonOK("Dashboard starred!") return ApiSuccess("Dashboard starred!")
} }
func UnstarDashboard(c *middleware.Context) { func UnstarDashboard(c *middleware.Context) Response {
var cmd = m.UnstarDashboardCommand{
UserId: c.UserId, cmd := m.UnstarDashboardCommand{UserId: c.UserId, DashboardId: c.ParamsInt64(":id")}
DashboardId: c.ParamsInt64(":id"),
}
if cmd.DashboardId <= 0 { if cmd.DashboardId <= 0 {
c.JsonApiErr(400, "Missing dashboard id", nil) return ApiError(400, "Missing dashboard id", nil)
return
} }
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to unstar dashboard", err) return ApiError(500, "Failed to unstar dashboard", err)
return
} }
c.JsonOK("Dashboard unstarred") return ApiSuccess("Dashboard unstarred")
} }
...@@ -92,55 +92,47 @@ func validateUsingOrg(userId int64, orgId int64) bool { ...@@ -92,55 +92,47 @@ func validateUsingOrg(userId int64, orgId int64) bool {
return valid return valid
} }
func UserSetUsingOrg(c *middleware.Context) { // POST /api/user/using/:id
func UserSetUsingOrg(c *middleware.Context) Response {
orgId := c.ParamsInt64(":id") orgId := c.ParamsInt64(":id")
if !validateUsingOrg(c.UserId, orgId) { if !validateUsingOrg(c.UserId, orgId) {
c.JsonApiErr(401, "Not a valid organization", nil) return ApiError(401, "Not a valid organization", nil)
return
} }
cmd := m.SetUsingOrgCommand{ cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId}
UserId: c.UserId,
OrgId: orgId,
}
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed change active organization", err) return ApiError(500, "Failed change active organization", err)
return
} }
c.JsonOK("Active organization changed") return ApiSuccess("Active organization changed")
} }
func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) { func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response {
userQuery := m.GetUserByIdQuery{Id: c.UserId} userQuery := m.GetUserByIdQuery{Id: c.UserId}
if err := bus.Dispatch(&userQuery); err != nil { if err := bus.Dispatch(&userQuery); err != nil {
c.JsonApiErr(500, "Could not read user from database", err) return ApiError(500, "Could not read user from database", err)
return
} }
passwordHashed := util.EncodePassword(cmd.OldPassword, userQuery.Result.Salt) passwordHashed := util.EncodePassword(cmd.OldPassword, userQuery.Result.Salt)
if passwordHashed != userQuery.Result.Password { if passwordHashed != userQuery.Result.Password {
c.JsonApiErr(401, "Invalid old password", nil) return ApiError(401, "Invalid old password", nil)
return
} }
if len(cmd.NewPassword) < 4 { if len(cmd.NewPassword) < 4 {
c.JsonApiErr(400, "New password too short", nil) return ApiError(400, "New password too short", nil)
return
} }
cmd.UserId = c.UserId cmd.UserId = c.UserId
cmd.NewPassword = util.EncodePassword(cmd.NewPassword, userQuery.Result.Salt) cmd.NewPassword = util.EncodePassword(cmd.NewPassword, userQuery.Result.Salt)
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to change user password", err) return ApiError(500, "Failed to change user password", err)
return
} }
c.JsonOK("User password changed") return ApiSuccess("User password changed")
} }
// GET /api/users // GET /api/users
......
...@@ -172,7 +172,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) { ...@@ -172,7 +172,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
}; };
function handleInfluxQueryResponse(alias, data) { function handleInfluxQueryResponse(alias, data) {
if (!data || !data.results || !data.results[0]) { if (!data || !data.results || !data.results[0].series) {
throw { message: 'No results in response from InfluxDB' }; throw { message: 'No results in response from InfluxDB' };
} }
return new InfluxSeries({ series: data.results[0].series, alias: alias }).getTimeSeries(); return new InfluxSeries({ series: data.results[0].series, alias: alias }).getTimeSeries();
......
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