Commit 433b8610 by Hugo Häggmark Committed by GitHub

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
parent e598c9e6
......@@ -19,6 +19,8 @@ type Response interface {
WriteTo(ctx *models.ReqContext)
// Status gets the response's status.
Status() int
// Body gets the response's body.
Body() []byte
}
type NormalResponse struct {
......@@ -48,6 +50,11 @@ func (r *NormalResponse) Status() int {
return r.status
}
// Body gets the response's body.
func (r *NormalResponse) Body() []byte {
return r.body
}
func (r *NormalResponse) WriteTo(ctx *models.ReqContext) {
if r.err != nil {
ctx.Logger.Error(r.errMessage, "error", r.err, "remote_addr", ctx.RemoteAddr())
......
......@@ -17,15 +17,16 @@ func (lps *LibraryPanelService) registerAPIEndpoints() {
}
lps.RouteRegister.Group("/api/library-panels", func(libraryPanels routing.RouteRegister) {
libraryPanels.Post("/", middleware.ReqSignedIn, binding.Bind(addLibraryPanelCommand{}), api.Wrap(lps.createHandler))
libraryPanels.Post("/", middleware.ReqSignedIn, binding.Bind(createLibraryPanelCommand{}), api.Wrap(lps.createHandler))
libraryPanels.Delete("/:panelId", middleware.ReqSignedIn, api.Wrap(lps.deleteHandler))
libraryPanels.Get("/:panelId", middleware.ReqSignedIn, api.Wrap(lps.getHandler))
libraryPanels.Get("/", middleware.ReqSignedIn, api.Wrap(lps.getAllHandler))
})
}
// createHandler handles POST /api/library-panels.
func (lps *LibraryPanelService) createHandler(c *models.ReqContext, cmd addLibraryPanelCommand) api.Response {
func (lps *LibraryPanelService) createHandler(c *models.ReqContext, cmd createLibraryPanelCommand) api.Response {
panel, err := lps.createLibraryPanel(c, cmd)
if err != nil {
if errors.Is(err, errLibraryPanelAlreadyAdded) {
return api.Error(400, errLibraryPanelAlreadyAdded.Error(), err)
......@@ -33,13 +34,12 @@ func (lps *LibraryPanelService) createHandler(c *models.ReqContext, cmd addLibra
return api.Error(500, "Failed to create library panel", err)
}
return api.JSON(200, util.DynMap{"panel": panel})
return api.JSON(200, util.DynMap{"result": panel})
}
// deleteHandler handles DELETE /api/library-panels/:panelId.
// deleteHandler handles DELETE /api/library-panels/:panelId
func (lps *LibraryPanelService) deleteHandler(c *models.ReqContext) api.Response {
err := lps.deleteLibraryPanel(c, c.ParamsInt64(":panelId"))
if err != nil {
if errors.Is(err, errLibraryPanelNotFound) {
return api.Error(404, errLibraryPanelNotFound.Error(), err)
......@@ -49,3 +49,26 @@ func (lps *LibraryPanelService) deleteHandler(c *models.ReqContext) api.Response
return api.Success("Library panel deleted")
}
// getHandler handles GET /api/library-panels/:panelId
func (lps *LibraryPanelService) getHandler(c *models.ReqContext) api.Response {
libraryPanel, err := lps.getLibraryPanel(c, c.ParamsInt64(":panelId"))
if err != nil {
if errors.Is(err, errLibraryPanelNotFound) {
return api.Error(404, errLibraryPanelNotFound.Error(), err)
}
return api.Error(500, "Failed to get library panel", err)
}
return api.JSON(200, util.DynMap{"result": libraryPanel})
}
// getAllHandler handles GET /api/library-panels/
func (lps *LibraryPanelService) getAllHandler(c *models.ReqContext) api.Response {
libraryPanels, err := lps.getAllLibraryPanels(c)
if err != nil {
return api.Error(500, "Failed to get library panels", err)
}
return api.JSON(200, util.DynMap{"result": libraryPanels})
}
......@@ -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
})
}
// 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
}
......@@ -30,8 +30,8 @@ var (
// Commands
// addLibraryPanelCommand is the command for adding a LibraryPanel
type addLibraryPanelCommand struct {
// createLibraryPanelCommand is the command for adding a LibraryPanel
type createLibraryPanelCommand struct {
FolderID int64 `json:"folderId"`
Title string `json:"title"`
Model json.RawMessage `json:"model"`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment