Browse Source

Refactoring dashboard delete and search

Torkel Ödegaard 11 years ago
parent
commit
8faa806c90
4 changed files with 51 additions and 32 deletions
  1. 13 9
      pkg/api/dashboard.go
  2. 19 6
      pkg/models/dashboards.go
  3. 19 14
      pkg/stores/sqlstore/dashboards.go
  4. 0 3
      pkg/stores/sqlstore/sqlstore.go

+ 13 - 9
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) {

+ 19 - 6
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 {

+ 19 - 14
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
 }

+ 0 - 3
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) {