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