Commit e371e036 by utkarshcmu

Able to save preference version, created, updated fields

parent 0bf721a7
...@@ -13,7 +13,7 @@ func SavePreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Respon ...@@ -13,7 +13,7 @@ func SavePreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Respon
cmd.OrgId = c.OrgId cmd.OrgId = c.OrgId
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
return ApiError(500, "Failed to saved preferences", err) return ApiError(500, "Failed to save preferences", err)
} }
return ApiSuccess("Preferences saved") return ApiSuccess("Preferences saved")
......
...@@ -3,6 +3,7 @@ package sqlstore ...@@ -3,6 +3,7 @@ package sqlstore
import ( import (
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"time"
) )
func init() { func init() {
...@@ -54,10 +55,12 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error { ...@@ -54,10 +55,12 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
savePref.UserId = cmd.UserId savePref.UserId = cmd.UserId
savePref.OrgId = cmd.OrgId savePref.OrgId = cmd.OrgId
savePref.Preference = cmd.Preference savePref.Preference = cmd.Preference
savePref = SetPreferencesModel(savePref, false)
affectedRows, saveErr = sess.Insert(&savePref) affectedRows, saveErr = sess.Insert(&savePref)
} else { } else {
savePref = prefResults[0] savePref = prefResults[0]
savePref.Preference = cmd.Preference savePref.Preference = cmd.Preference
savePref = SetPreferencesModel(savePref, true)
affectedRows, saveErr = sess.Id(savePref.Id).Update(&savePref) affectedRows, saveErr = sess.Id(savePref.Id).Update(&savePref)
} }
...@@ -68,3 +71,16 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error { ...@@ -68,3 +71,16 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
return saveErr return saveErr
}) })
} }
func SetPreferencesModel(pref m.Preferences, updating bool) m.Preferences {
if updating {
pref.Version = pref.Version + 1
} else {
pref.Version = 0
pref.Created = time.Now()
}
pref.Updated = time.Now()
return pref
}
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