From 069f316ca58e0cc5a62509eaa88f9a676c8addd4 Mon Sep 17 00:00:00 2001 From: Kelly Merrick Date: Fri, 13 Sep 2024 13:04:27 -0500 Subject: [PATCH 1/4] fix(dasboards): remove deleted dashboards from users list --- api/dashboard/delete.go | 23 ++++++++++++++++++++++- api/types/user.go | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/api/dashboard/delete.go b/api/dashboard/delete.go index 05b14e86b..4f022b68c 100644 --- a/api/dashboard/delete.go +++ b/api/dashboard/delete.go @@ -59,6 +59,7 @@ func DeleteDashboard(c *gin.Context) { l := c.MustGet("logger").(*logrus.Entry) d := dashboard.Retrieve(c) u := user.Retrieve(c) + ctx := c.Request.Context() l.Debugf("deleting dashboard %s", d.GetID()) @@ -70,7 +71,27 @@ func DeleteDashboard(c *gin.Context) { return } - err := database.FromContext(c).DeleteDashboard(c, d) + // Remove the dashboard ID from the user's dashboards + dashboards := u.GetDashboards() + updatedDashboards := []string{} + for _, id := range dashboards { + if id != d.GetID() { + updatedDashboards = append(updatedDashboards, id) + } + } + u.SetDashboards(updatedDashboards) + + // send API call to update the user + u, err := database.FromContext(c).UpdateUser(ctx, u) + if err != nil { + retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err) + + util.HandleError(c, http.StatusInternalServerError, retErr) + + return + } + + err = database.FromContext(c).DeleteDashboard(c, d) if err != nil { retErr := fmt.Errorf("error while deleting dashboard %s: %w", d.GetID(), err) diff --git a/api/types/user.go b/api/types/user.go index a7d7691c0..4d454526d 100644 --- a/api/types/user.go +++ b/api/types/user.go @@ -140,7 +140,7 @@ func (u *User) GetFavorites() []string { // When the provided User type is nil, or the field within // the type is nil, it returns the zero value for the field. func (u *User) GetDashboards() []string { - // return zero value if User type or Favorites field is nil + // return zero value if User type or Dashboard field is nil if u == nil || u.Dashboards == nil { return []string{} } From c167cd324f5c15bdca8fb380b6b120e78e64d856 Mon Sep 17 00:00:00 2001 From: Kelly Merrick Date: Fri, 13 Sep 2024 13:14:49 -0500 Subject: [PATCH 2/4] linter fixes --- api/dashboard/delete.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/dashboard/delete.go b/api/dashboard/delete.go index 4f022b68c..f2104989d 100644 --- a/api/dashboard/delete.go +++ b/api/dashboard/delete.go @@ -73,12 +73,14 @@ func DeleteDashboard(c *gin.Context) { // Remove the dashboard ID from the user's dashboards dashboards := u.GetDashboards() + updatedDashboards := []string{} for _, id := range dashboards { if id != d.GetID() { updatedDashboards = append(updatedDashboards, id) } } + u.SetDashboards(updatedDashboards) // send API call to update the user From 0c801324c4295516dc7aef341c78596740e5058a Mon Sep 17 00:00:00 2001 From: Kelly Merrick Date: Fri, 13 Sep 2024 13:18:45 -0500 Subject: [PATCH 3/4] yet another linter fix --- api/dashboard/delete.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/dashboard/delete.go b/api/dashboard/delete.go index f2104989d..836c63f46 100644 --- a/api/dashboard/delete.go +++ b/api/dashboard/delete.go @@ -75,6 +75,7 @@ func DeleteDashboard(c *gin.Context) { dashboards := u.GetDashboards() updatedDashboards := []string{} + for _, id := range dashboards { if id != d.GetID() { updatedDashboards = append(updatedDashboards, id) From e1e25c3cbb394fb62d4572b330aae00188b0705d Mon Sep 17 00:00:00 2001 From: Kelly Merrick Date: Fri, 13 Sep 2024 14:03:53 -0500 Subject: [PATCH 4/4] swap db delete and user update order --- api/dashboard/delete.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api/dashboard/delete.go b/api/dashboard/delete.go index 836c63f46..093f0fb59 100644 --- a/api/dashboard/delete.go +++ b/api/dashboard/delete.go @@ -84,26 +84,26 @@ func DeleteDashboard(c *gin.Context) { u.SetDashboards(updatedDashboards) - // send API call to update the user - u, err := database.FromContext(c).UpdateUser(ctx, u) + err := database.FromContext(c).DeleteDashboard(c, d) if err != nil { - retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err) + retErr := fmt.Errorf("error while deleting dashboard %s: %w", d.GetID(), err) util.HandleError(c, http.StatusInternalServerError, retErr) return } - err = database.FromContext(c).DeleteDashboard(c, d) + c.JSON(http.StatusOK, fmt.Sprintf("dashboard %s deleted", d.GetName())) + + // send API call to update the user + u, err = database.FromContext(c).UpdateUser(ctx, u) if err != nil { - retErr := fmt.Errorf("error while deleting dashboard %s: %w", d.GetID(), err) + retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err) util.HandleError(c, http.StatusInternalServerError, retErr) return } - - c.JSON(http.StatusOK, fmt.Sprintf("dashboard %s deleted", d.GetName())) } // isAdmin is a helper function that iterates through the dashboard admins