From 6a1192172dd75c9442070439ca48f2e185d2d9e6 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Wed, 27 Jan 2016 21:55:54 -0800 Subject: [PATCH] Api stores dashboard creator --- pkg/api/dashboard.go | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index 79485fa8cca..5effc1d06d7 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -49,18 +49,14 @@ func GetDashboard(c *middleware.Context) { dash := query.Result - // Finding the last updater of the dashboard - updater := "Anonymous" - if dash.UpdatedBy != 0 { - userQuery := m.GetUserByIdQuery{Id: dash.UpdatedBy} - userErr := bus.Dispatch(&userQuery) - if userErr != nil { - updater = "Unknown" - } else { - user := userQuery.Result - updater = user.Login - } - } + // Finding the last creator and updater of the dashboard + updater, creator := "Anonymous", "Anonymous" + if dash.UpdatedBy > 0 { + updater = getUserLogin(dash.UpdatedBy) + } + if dash.CreatedBy > 0 { + creator = getUserLogin(dash.CreatedBy) + } dto := dtos.DashboardFullWithMeta{ Dashboard: dash.Data, @@ -74,12 +70,24 @@ func GetDashboard(c *middleware.Context) { Created: dash.Created, Updated: dash.Updated, UpdatedBy: updater, + CreatedBy: creator, }, } c.JSON(200, dto) } +func getUserLogin(userId int64) string { + query := m.GetUserByIdQuery{Id: userId} + err := bus.Dispatch(&query) + if err != nil { + return "Anonymous" + } else { + user := query.Result + return user.Login + } +} + func DeleteDashboard(c *middleware.Context) { slug := c.Params(":slug") @@ -104,10 +112,10 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { cmd.OrgId = c.OrgId if !c.IsSignedIn { - cmd.UpdatedBy = 0 + cmd.UserId = -1 } else { - cmd.UpdatedBy = c.UserId - } + cmd.UserId = c.UserId + } dash := cmd.GetDashboardModel() if dash.Id == 0 {