2014-12-22 05:25:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
|
|
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-12-29 06:58:06 -06:00
|
|
|
bus.AddHandler("sql", SaveDashboard)
|
|
|
|
bus.AddHandler("sql", GetDashboard)
|
|
|
|
bus.AddHandler("sql", DeleteDashboard)
|
|
|
|
bus.AddHandler("sql", SearchDashboards)
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
2014-12-22 05:25:08 -06:00
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
dash := cmd.GetDashboardModel()
|
|
|
|
|
2015-01-05 10:04:29 -06:00
|
|
|
// try get existing dashboard
|
|
|
|
existing := m.Dashboard{Slug: dash.Slug, AccountId: dash.AccountId}
|
|
|
|
hasExisting, err := sess.Get(&existing)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasExisting && dash.Id != existing.Id {
|
|
|
|
return m.ErrDashboardWithSameNameExists
|
|
|
|
}
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
if dash.Id == 0 {
|
|
|
|
_, err = sess.Insert(dash)
|
|
|
|
} else {
|
|
|
|
_, err = sess.Id(dash.Id).Update(dash)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Result = dash
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func GetDashboard(query *m.GetDashboardQuery) error {
|
|
|
|
dashboard := m.Dashboard{Slug: query.Slug, AccountId: query.AccountId}
|
2014-12-22 05:25:08 -06:00
|
|
|
has, err := x.Get(&dashboard)
|
|
|
|
if err != nil {
|
2014-12-29 06:58:06 -06:00
|
|
|
return err
|
2014-12-22 05:25:08 -06:00
|
|
|
} else if has == false {
|
2014-12-29 06:58:06 -06:00
|
|
|
return m.ErrDashboardNotFound
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
query.Result = &dashboard
|
|
|
|
|
|
|
|
return nil
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func SearchDashboards(query *m.SearchDashboardsQuery) error {
|
|
|
|
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId)
|
2014-12-22 05:25:08 -06:00
|
|
|
sess.Table("Dashboard")
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
query.Result = make([]*m.SearchResult, 0)
|
|
|
|
err := sess.Find(&query.Result)
|
2014-12-22 05:25:08 -06:00
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
return err
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
|
2014-12-22 05:25:08 -06:00
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
rawSql := "DELETE FROM Dashboard WHERE account_id=? and slug=?"
|
2014-12-29 06:58:06 -06:00
|
|
|
_, err := sess.Exec(rawSql, cmd.AccountId, cmd.Slug)
|
2014-12-22 05:25:08 -06:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|