Dashboard: When saving a dashboard and another user has made changes inbetween, the user is promted with a warning if he really wants to overwrite the other's changes, Closes #718
This commit is contained in:
@@ -77,14 +77,18 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
|
||||
err := bus.Dispatch(&cmd)
|
||||
if err != nil {
|
||||
if err == m.ErrDashboardWithSameNameExists {
|
||||
c.JsonApiErr(400, "Dashboard with the same title already exists", nil)
|
||||
c.JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
|
||||
return
|
||||
}
|
||||
if err == m.ErrDashboardVersionMismatch {
|
||||
c.JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JsonApiErr(500, "Failed to save dashboard", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug})
|
||||
c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version})
|
||||
}
|
||||
|
||||
func GetHomeDashboard(c *middleware.Context) {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
var (
|
||||
ErrDashboardNotFound = errors.New("Account not found")
|
||||
ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
|
||||
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
|
||||
)
|
||||
|
||||
type Dashboard struct {
|
||||
@@ -58,6 +59,10 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
||||
|
||||
if dash.Data["id"] != nil {
|
||||
dash.Id = int64(dash.Data["id"].(float64))
|
||||
|
||||
if dash.Data["version"] != nil {
|
||||
dash.Version = int(dash.Data["version"].(float64))
|
||||
}
|
||||
}
|
||||
|
||||
return dash
|
||||
@@ -79,7 +84,8 @@ func (dash *Dashboard) UpdateSlug() {
|
||||
//
|
||||
|
||||
type SaveDashboardCommand struct {
|
||||
Dashboard map[string]interface{} `json:"dashboard"`
|
||||
Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
|
||||
Overwrite bool `json:"overwrite"`
|
||||
OrgId int64 `json:"-"`
|
||||
|
||||
Result *Dashboard
|
||||
|
||||
@@ -28,13 +28,30 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if hasExisting && dash.Id != existing.Id {
|
||||
return m.ErrDashboardWithSameNameExists
|
||||
if hasExisting {
|
||||
// another dashboard with same name
|
||||
if dash.Id != existing.Id {
|
||||
if cmd.Overwrite {
|
||||
dash.Id = existing.Id
|
||||
} else {
|
||||
return m.ErrDashboardWithSameNameExists
|
||||
}
|
||||
}
|
||||
// check for is someone else has written in between
|
||||
if dash.Version != existing.Version {
|
||||
if cmd.Overwrite {
|
||||
dash.Version = existing.Version
|
||||
} else {
|
||||
return m.ErrDashboardVersionMismatch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if dash.Id == 0 {
|
||||
_, err = sess.Insert(dash)
|
||||
} else {
|
||||
dash.Version += 1
|
||||
dash.Data["version"] = dash.Version
|
||||
_, err = sess.Id(dash.Id).Update(dash)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user