PanelLibrary: Adds get and getAll to the api (#29772)

* PanelLibrary: Adds get to the API

* Refactor: adds tests for get and getAll and cleans up other tests

* Refactor: changed name on DTO

* Update pkg/services/librarypanels/api.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Update pkg/services/librarypanels/database.go
This commit is contained in:
Hugo Häggmark
2020-12-21 09:40:27 +01:00
committed by GitHub
co-authored by Arve Knudsen
parent e598c9e6d9
commit 433b861093
5 changed files with 305 additions and 102 deletions
+49 -9
View File
@@ -2,6 +2,7 @@ package librarypanels
import (
"context"
"fmt"
"time"
"github.com/grafana/grafana/pkg/models"
@@ -9,8 +10,8 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore"
)
// createLibraryPanel function adds a Library Panel
func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd addLibraryPanelCommand) (LibraryPanel, error) {
// createLibraryPanel adds a Library Panel
func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd createLibraryPanelCommand) (LibraryPanel, error) {
libraryPanel := LibraryPanel{
OrgID: c.SignedInUser.OrgId,
FolderID: cmd.FolderID,
@@ -23,9 +24,9 @@ func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd add
CreatedBy: c.SignedInUser.UserId,
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 title=?",
c.SignedInUser.OrgId, cmd.FolderID, cmd.Title); err != nil {
return err
} else if len(res) == 1 {
return errLibraryPanelAlreadyAdded
@@ -40,13 +41,11 @@ func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd add
return libraryPanel, err
}
// deleteLibraryPanel function deletes a Library Panel
// deleteLibraryPanel deletes a Library Panel
func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, panelID int64) error {
orgID := c.SignedInUser.OrgId
err := lps.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error {
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)
if err != nil {
return err
}
@@ -59,6 +58,47 @@ func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, panelID
return nil
})
}
return err
// getLibraryPanel gets a Library Panel.
func (lps *LibraryPanelService) getLibraryPanel(c *models.ReqContext, panelID int64) (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)
if err != nil {
return err
}
if len(libraryPanels) == 0 {
return errLibraryPanelNotFound
}
if len(libraryPanels) > 1 {
return fmt.Errorf("found %d panels, while expecting at most one", len(libraryPanels))
}
libraryPanel = libraryPanels[0]
return nil
})
return libraryPanel, err
}
// getAllLibraryPanels gets all library panels.
func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext) ([]LibraryPanel, error) {
orgID := c.SignedInUser.OrgId
libraryPanels := make([]LibraryPanel, 0)
err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
err := session.SQL("SELECT * FROM library_panel WHERE org_id=?", orgID).Find(&libraryPanels)
if err != nil {
return err
}
return nil
})
return libraryPanels, err
}