PanelLibrary: Adds uid and renames title to name (#29944)

* PanelLibrary: Adds uid and renames title to name

* Chore: removing lines

* Chore: updates comments

* Chore: changes after PR comments
This commit is contained in:
Hugo Häggmark
2020-12-22 12:00:41 +01:00
committed by GitHub
parent 3f65c79998
commit 35a755fe50
5 changed files with 66 additions and 64 deletions
+10 -8
View File
@@ -5,6 +5,8 @@ import (
"fmt"
"time"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
@@ -15,7 +17,8 @@ func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd cre
libraryPanel := LibraryPanel{
OrgID: c.SignedInUser.OrgId,
FolderID: cmd.FolderID,
Title: cmd.Title,
UID: util.GenerateShortUID(),
Name: cmd.Name,
Model: cmd.Model,
Created: time.Now(),
@@ -25,8 +28,8 @@ func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd cre
UpdatedBy: c.SignedInUser.UserId,
}
err := lps.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error {
if res, err := session.Query("SELECT 1 FROM library_panel WHERE org_id=? AND folder_id=? AND title=?",
c.SignedInUser.OrgId, cmd.FolderID, cmd.Title); err != nil {
if res, err := session.Query("SELECT 1 FROM library_panel WHERE org_id=? AND folder_id=? AND name=?",
c.SignedInUser.OrgId, cmd.FolderID, cmd.Name); err != nil {
return err
} else if len(res) == 1 {
return errLibraryPanelAlreadyAdded
@@ -42,10 +45,10 @@ func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd cre
}
// deleteLibraryPanel deletes a Library Panel
func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, panelID int64) error {
func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, uid string) error {
orgID := c.SignedInUser.OrgId
return lps.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error {
result, err := session.Exec("DELETE FROM library_panel WHERE id=? and org_id=?", panelID, orgID)
result, err := session.Exec("DELETE FROM library_panel WHERE uid=? and org_id=?", uid, orgID)
if err != nil {
return err
}
@@ -61,16 +64,15 @@ func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, panelID
}
// getLibraryPanel gets a Library Panel.
func (lps *LibraryPanelService) getLibraryPanel(c *models.ReqContext, panelID int64) (LibraryPanel, error) {
func (lps *LibraryPanelService) getLibraryPanel(c *models.ReqContext, uid string) (LibraryPanel, error) {
orgID := c.SignedInUser.OrgId
var libraryPanel LibraryPanel
err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
libraryPanels := make([]LibraryPanel, 0)
err := session.SQL("SELECT * FROM library_panel WHERE id=? and org_id=?", panelID, orgID).Find(&libraryPanels)
err := session.SQL("SELECT * FROM library_panel WHERE uid=? and org_id=?", uid, orgID).Find(&libraryPanels)
if err != nil {
return err
}
if len(libraryPanels) == 0 {
return errLibraryPanelNotFound
}