Commit e371e036 by utkarshcmu

Able to save preference version, created, updated fields

parent 0bf721a7
......@@ -164,7 +164,7 @@ func Register(r *macaron.Macaron) {
r.Delete("/:id", wrap(DeleteApiKey))
}, reqOrgAdmin)
r.Combo("/preferences").Get(GetPreferences).Put(bind(m.SavePreferencesCommand{}), wrap(SavePreferences))
r.Combo("/preferences").Get(GetPreferences).Put(bind(m.SavePreferencesCommand{}), wrap(SavePreferences))
// Data sources
r.Group("/datasources", func() {
......
......@@ -10,10 +10,10 @@ import (
func SavePreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Response {
cmd.UserId = c.UserId
cmd.OrgId = c.OrgId
cmd.OrgId = c.OrgId
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")
......@@ -32,7 +32,7 @@ func GetPreferences(c *middleware.Context) {
dto := m.PreferencesDTO{
Id: query.Result.Id,
UserId: query.Result.UserId,
OrgId: query.Result.OrgId,
OrgId: query.Result.OrgId,
Preference: query.Result.Preference,
}
......
......@@ -2,7 +2,7 @@ package models
import (
"errors"
"time"
"time"
)
// Typed errors
......@@ -14,10 +14,10 @@ type Preferences struct {
Id int64
OrgId int64
UserId int64
Version int
Version int
Preference map[string]interface{}
Created time.Time
Updated time.Time
Created time.Time
Updated time.Time
}
// ---------------------
......@@ -26,7 +26,7 @@ type Preferences struct {
type GetPreferencesQuery struct {
Id int64
OrgId int64
UserId int64
UserId int64
Result *Preferences
}
......@@ -46,6 +46,6 @@ type SavePreferencesCommand struct {
type PreferencesDTO struct {
Id int64 `json:"Id"`
UserId int64 `json:"UserId"`
OrgId int64 `json:"OrgId"`
OrgId int64 `json:"OrgId"`
Preference map[string]interface{} `json:"Preference"`
}
......@@ -10,10 +10,10 @@ func addPreferencesMigrations(mg *Migrator) {
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "org_id", Type: DB_Int, Nullable: false},
{Name: "user_id", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "version", Type: DB_Int, Nullable: false},
{Name: "version", Type: DB_Int, Nullable: false},
{Name: "preference", Type: DB_Text, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
},
}
......
......@@ -3,6 +3,7 @@ package sqlstore
import (
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"time"
)
func init() {
......@@ -54,10 +55,12 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
savePref.UserId = cmd.UserId
savePref.OrgId = cmd.OrgId
savePref.Preference = cmd.Preference
savePref = SetPreferencesModel(savePref, false)
affectedRows, saveErr = sess.Insert(&savePref)
} else {
savePref = prefResults[0]
savePref.Preference = cmd.Preference
savePref = SetPreferencesModel(savePref, true)
affectedRows, saveErr = sess.Id(savePref.Id).Update(&savePref)
}
......@@ -68,3 +71,16 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error {
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