From 8faa806c907fb02a7864edd54472e78e31830075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 29 Dec 2014 13:58:06 +0100 Subject: [PATCH] Refactoring dashboard delete and search --- pkg/api/dashboard.go | 22 ++++++++++++--------- pkg/models/dashboards.go | 25 +++++++++++++++++------ pkg/stores/sqlstore/dashboards.go | 33 ++++++++++++++++++------------- pkg/stores/sqlstore/sqlstore.go | 3 --- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index b206c183cd2..6791eda8223 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -10,47 +10,51 @@ import ( func GetDashboard(c *middleware.Context) { slug := c.Params(":slug") - dash, err := m.GetDashboard(slug, c.GetAccountId()) + query := m.GetDashboardQuery{Slug: slug, AccountId: c.GetAccountId()} + err := bus.Dispatch(&query) if err != nil { c.JsonApiErr(404, "Dashboard not found", nil) return } - dash.Data["id"] = dash.Id + query.Result.Data["id"] = query.Result.Id - c.JSON(200, dash.Data) + c.JSON(200, query.Result.Data) } func DeleteDashboard(c *middleware.Context) { slug := c.Params(":slug") - dash, err := m.GetDashboard(slug, c.GetAccountId()) + query := m.GetDashboardQuery{Slug: slug, AccountId: c.GetAccountId()} + err := bus.Dispatch(&query) if err != nil { c.JsonApiErr(404, "Dashboard not found", nil) return } - err = m.DeleteDashboard(slug, c.GetAccountId()) + cmd := m.DeleteDashboardCommand{Slug: slug, AccountId: c.GetAccountId()} + err = bus.Dispatch(&cmd) if err != nil { c.JsonApiErr(500, "Failed to delete dashboard", err) return } - var resp = map[string]interface{}{"title": dash.Title} + var resp = map[string]interface{}{"title": query.Result.Title} c.JSON(200, resp) } func Search(c *middleware.Context) { - query := c.Query("q") + queryText := c.Query("q") - results, err := m.SearchQuery(query, c.GetAccountId()) + query := m.SearchDashboardsQuery{Query: queryText, AccountId: c.GetAccountId()} + err := bus.Dispatch(&query) if err != nil { c.JsonApiErr(500, "Search failed", err) return } - c.JSON(200, results) + c.JSON(200, query.Result) } func PostDashboard(c *middleware.Context) { diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index ac8140fc656..2aa5020a3e5 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -7,12 +7,6 @@ import ( "time" ) -var ( - GetDashboard func(slug string, accountId int64) (*Dashboard, error) - DeleteDashboard func(slug string, accountId int64) error - SearchQuery func(query string, acccountId int64) ([]*SearchResult, error) -) - // Typed errors var ( ErrDashboardNotFound = errors.New("Account not found") @@ -37,6 +31,13 @@ type SearchResult struct { Slug string `json:"slug"` } +type SearchDashboardsQuery struct { + Query string + AccountId int64 + + Result []*SearchResult +} + type SaveDashboardCommand struct { Id string `json:"id"` Title string `json:"title"` @@ -46,6 +47,18 @@ type SaveDashboardCommand struct { Result *Dashboard } +type DeleteDashboardCommand struct { + Slug string + AccountId int64 +} + +type GetDashboardQuery struct { + Slug string + AccountId int64 + + Result *Dashboard +} + func convertToStringArray(arr []interface{}) []string { b := make([]string, len(arr)) for i := range arr { diff --git a/pkg/stores/sqlstore/dashboards.go b/pkg/stores/sqlstore/dashboards.go index 21bf1dbb357..827993f7f3f 100644 --- a/pkg/stores/sqlstore/dashboards.go +++ b/pkg/stores/sqlstore/dashboards.go @@ -7,10 +7,13 @@ import ( ) func init() { - bus.AddHandler("sql", SaveDashboard2) + bus.AddHandler("sql", SaveDashboard) + bus.AddHandler("sql", GetDashboard) + bus.AddHandler("sql", DeleteDashboard) + bus.AddHandler("sql", SearchDashboards) } -func SaveDashboard2(cmd *m.SaveDashboardCommand) error { +func SaveDashboard(cmd *m.SaveDashboardCommand) error { return inTransaction(func(sess *xorm.Session) error { dash := cmd.GetDashboardModel() @@ -27,34 +30,36 @@ func SaveDashboard2(cmd *m.SaveDashboardCommand) error { }) } -func GetDashboard(slug string, accountId int64) (*m.Dashboard, error) { - dashboard := m.Dashboard{Slug: slug, AccountId: accountId} +func GetDashboard(query *m.GetDashboardQuery) error { + dashboard := m.Dashboard{Slug: query.Slug, AccountId: query.AccountId} has, err := x.Get(&dashboard) if err != nil { - return nil, err + return err } else if has == false { - return nil, m.ErrDashboardNotFound + return m.ErrDashboardNotFound } - return &dashboard, nil + query.Result = &dashboard + + return nil } -func SearchQuery(query string, accountId int64) ([]*m.SearchResult, error) { - sess := x.Limit(100, 0).Where("account_id=?", accountId) +func SearchDashboards(query *m.SearchDashboardsQuery) error { + sess := x.Limit(100, 0).Where("account_id=?", query.AccountId) sess.Table("Dashboard") - results := make([]*m.SearchResult, 0) - err := sess.Find(&results) + query.Result = make([]*m.SearchResult, 0) + err := sess.Find(&query.Result) - return results, err + return err } -func DeleteDashboard(slug string, accountId int64) error { +func DeleteDashboard(cmd *m.DeleteDashboardCommand) error { sess := x.NewSession() defer sess.Close() rawSql := "DELETE FROM Dashboard WHERE account_id=? and slug=?" - _, err := sess.Exec(rawSql, accountId, slug) + _, err := sess.Exec(rawSql, cmd.AccountId, cmd.Slug) return err } diff --git a/pkg/stores/sqlstore/sqlstore.go b/pkg/stores/sqlstore/sqlstore.go index 43aad899417..ad992f8d71d 100644 --- a/pkg/stores/sqlstore/sqlstore.go +++ b/pkg/stores/sqlstore/sqlstore.go @@ -35,9 +35,6 @@ func init() { } func Init() { - m.GetDashboard = GetDashboard - m.SearchQuery = SearchQuery - m.DeleteDashboard = DeleteDashboard } func NewEngine() (err error) {