mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	Added support for delete dashboard
This commit is contained in:
		@@ -8,25 +8,49 @@ import (
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	addRoutes(func(self *HttpServer) {
 | 
			
		||||
		self.router.GET("/api/dashboards/:id", self.auth(), self.getDashboard)
 | 
			
		||||
		self.router.GET("/api/dashboards/:slug", self.auth(), self.getDashboard)
 | 
			
		||||
		self.router.GET("/api/search/", self.auth(), self.search)
 | 
			
		||||
		self.router.POST("/api/dashboard", self.auth(), self.postDashboard)
 | 
			
		||||
		self.router.DELETE("/api/dashboard/:slug", self.auth(), self.deleteDashboard)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *HttpServer) getDashboard(c *gin.Context) {
 | 
			
		||||
	id := c.Params.ByName("id")
 | 
			
		||||
	slug := c.Params.ByName("slug")
 | 
			
		||||
	accountId, err := c.Get("accountId")
 | 
			
		||||
 | 
			
		||||
	dash, err := self.store.GetDashboard(id, accountId.(int))
 | 
			
		||||
	dash, err := self.store.GetDashboard(slug, accountId.(int))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(404, newErrorResponse("Dashboard not found"))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dash.Data["id"] = dash.Id
 | 
			
		||||
 | 
			
		||||
	c.JSON(200, dash.Data)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *HttpServer) deleteDashboard(c *gin.Context) {
 | 
			
		||||
	slug := c.Params.ByName("slug")
 | 
			
		||||
	accountId, err := c.Get("accountId")
 | 
			
		||||
 | 
			
		||||
	dash, err := self.store.GetDashboard(slug, accountId.(int))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(404, newErrorResponse("Dashboard not found"))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = self.store.DeleteDashboard(slug, accountId.(int))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(500, newErrorResponse("Failed to delete dashboard: "+err.Error()))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var resp = map[string]interface{}{"title": dash.Title}
 | 
			
		||||
 | 
			
		||||
	c.JSON(200, resp)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *HttpServer) search(c *gin.Context) {
 | 
			
		||||
	query := c.Params.ByName("q")
 | 
			
		||||
	accountId, err := c.Get("accountId")
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package stores
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	log "github.com/alecthomas/log4go"
 | 
			
		||||
@@ -63,7 +64,7 @@ func NewRethinkStore(config *RethinkCfg) *rethinkStore {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *rethinkStore) SaveDashboard(dash *models.Dashboard) error {
 | 
			
		||||
	resp, err := r.Table("dashboards").Insert(dash, r.InsertOpts{Upsert: true}).RunWrite(self.session)
 | 
			
		||||
	resp, err := r.Table("dashboards").Insert(dash, r.InsertOpts{Conflict: "update"}).RunWrite(self.session)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -92,6 +93,22 @@ func (self *rethinkStore) GetDashboard(slug string, accountId int) (*models.Dash
 | 
			
		||||
	return &dashboard, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *rethinkStore) DeleteDashboard(slug string, accountId int) error {
 | 
			
		||||
	resp, err := r.Table("dashboards").
 | 
			
		||||
		GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).
 | 
			
		||||
		Delete().RunWrite(self.session)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if resp.Deleted != 1 {
 | 
			
		||||
		return errors.New("Did not find dashboard to delete")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *rethinkStore) Query(query string, accountId int) ([]*models.SearchResult, error) {
 | 
			
		||||
	docs, err := r.Table("dashboards").GetAllByIndex("AccountId", []interface{}{accountId}).Filter(r.Row.Field("Title").Match(".*")).Run(self.session)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,17 +10,19 @@ import (
 | 
			
		||||
func (self *rethinkStore) getNextAccountId() (int, error) {
 | 
			
		||||
	resp, err := r.Table("master").Get("ids").Update(map[string]interface{}{
 | 
			
		||||
		"NextAccountId": r.Row.Field("NextAccountId").Add(1),
 | 
			
		||||
	}, r.UpdateOpts{ReturnVals: true}).RunWrite(self.session)
 | 
			
		||||
	}, r.UpdateOpts{ReturnChanges: true}).RunWrite(self.session)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if resp.NewValue == nil {
 | 
			
		||||
	change := resp.Changes[0]
 | 
			
		||||
 | 
			
		||||
	if change.NewValue == nil {
 | 
			
		||||
		return 0, errors.New("Failed to get new value after incrementing account id")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return int(resp.NewValue.(map[string]interface{})["NextAccountId"].(float64)), nil
 | 
			
		||||
	return int(change.NewValue.(map[string]interface{})["NextAccountId"].(float64)), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *rethinkStore) SaveUserAccount(account *models.UserAccount) error {
 | 
			
		||||
@@ -62,15 +64,17 @@ func (self *rethinkStore) GetUserAccountLogin(emailOrName string) (*models.UserA
 | 
			
		||||
func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) {
 | 
			
		||||
	resp, err := r.Table("accounts").Get(accountId).Update(map[string]interface{}{
 | 
			
		||||
		"NextDashboardId": r.Row.Field("NextDashboardId").Add(1),
 | 
			
		||||
	}, r.UpdateOpts{ReturnVals: true}).RunWrite(self.session)
 | 
			
		||||
	}, r.UpdateOpts{ReturnChanges: true}).RunWrite(self.session)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if resp.NewValue == nil {
 | 
			
		||||
	change := resp.Changes[0]
 | 
			
		||||
 | 
			
		||||
	if change.NewValue == nil {
 | 
			
		||||
		return 0, errors.New("Failed to get next dashboard id, no new value after update")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return int(resp.NewValue.(map[string]interface{})["NextDashboardId"].(float64)), nil
 | 
			
		||||
	return int(change.NewValue.(map[string]interface{})["NextDashboardId"].(float64)), nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ import (
 | 
			
		||||
type Store interface {
 | 
			
		||||
	GetDashboard(slug string, accountId int) (*models.Dashboard, error)
 | 
			
		||||
	SaveDashboard(dash *models.Dashboard) error
 | 
			
		||||
	DeleteDashboard(slug string, accountId int) error
 | 
			
		||||
	Query(query string, acccountId int) ([]*models.SearchResult, error)
 | 
			
		||||
	SaveUserAccount(acccount *models.UserAccount) error
 | 
			
		||||
	GetUserAccountLogin(emailOrName string) (*models.UserAccount, error)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user