2021-05-11 00:10:19 -05:00
|
|
|
package libraryelements
|
|
|
|
|
|
|
|
import (
|
2021-09-27 02:04:36 -05:00
|
|
|
"context"
|
|
|
|
|
2021-05-11 00:10:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2021-05-11 00:10:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-10-10 14:47:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2023-02-01 10:32:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/libraryelements/model"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2021-05-11 00:10:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2022-10-14 14:33:06 -05:00
|
|
|
func ProvideService(cfg *setting.Cfg, sqlStore db.DB, routeRegister routing.RouteRegister, folderService folder.Service) *LibraryElementService {
|
2021-08-25 08:11:22 -05:00
|
|
|
l := &LibraryElementService{
|
|
|
|
Cfg: cfg,
|
|
|
|
SQLStore: sqlStore,
|
|
|
|
RouteRegister: routeRegister,
|
2022-02-16 07:15:44 -06:00
|
|
|
folderService: folderService,
|
2021-08-25 08:11:22 -05:00
|
|
|
log: log.New("library-elements"),
|
|
|
|
}
|
|
|
|
l.registerAPIEndpoints()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2021-05-12 01:48:17 -05:00
|
|
|
// Service is a service for operating on library elements.
|
|
|
|
type Service interface {
|
2023-02-01 10:32:05 -06:00
|
|
|
CreateElement(c context.Context, signedInUser *user.SignedInUser, cmd model.CreateLibraryElementCommand) (model.LibraryElementDTO, error)
|
|
|
|
GetElement(c context.Context, signedInUser *user.SignedInUser, UID string) (model.LibraryElementDTO, error)
|
|
|
|
GetElementsForDashboard(c context.Context, dashboardID int64) (map[string]model.LibraryElementDTO, error)
|
2022-08-10 04:56:48 -05:00
|
|
|
ConnectElementsToDashboard(c context.Context, signedInUser *user.SignedInUser, elementUIDs []string, dashboardID int64) error
|
2021-09-27 02:04:36 -05:00
|
|
|
DisconnectElementsFromDashboard(c context.Context, dashboardID int64) error
|
2022-08-10 04:56:48 -05:00
|
|
|
DeleteLibraryElementsInFolder(c context.Context, signedInUser *user.SignedInUser, folderUID string) error
|
2021-05-12 01:48:17 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 00:10:19 -05:00
|
|
|
// LibraryElementService is the service for the Library Element feature.
|
|
|
|
type LibraryElementService struct {
|
2021-08-25 08:11:22 -05:00
|
|
|
Cfg *setting.Cfg
|
2022-10-14 14:33:06 -05:00
|
|
|
SQLStore db.DB
|
2021-08-25 08:11:22 -05:00
|
|
|
RouteRegister routing.RouteRegister
|
2022-10-10 14:47:53 -05:00
|
|
|
folderService folder.Service
|
2021-05-11 00:10:19 -05:00
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateElement creates a Library Element.
|
2023-02-01 10:32:05 -06:00
|
|
|
func (l *LibraryElementService) CreateElement(c context.Context, signedInUser *user.SignedInUser, cmd model.CreateLibraryElementCommand) (model.LibraryElementDTO, error) {
|
2021-09-27 02:04:36 -05:00
|
|
|
return l.createLibraryElement(c, signedInUser, cmd)
|
2021-05-11 00:10:19 -05:00
|
|
|
}
|
|
|
|
|
2021-09-20 03:58:24 -05:00
|
|
|
// GetElement gets an element from a UID.
|
2023-02-01 10:32:05 -06:00
|
|
|
func (l *LibraryElementService) GetElement(c context.Context, signedInUser *user.SignedInUser, UID string) (model.LibraryElementDTO, error) {
|
2021-09-27 02:04:36 -05:00
|
|
|
return l.getLibraryElementByUid(c, signedInUser, UID)
|
2021-09-20 03:58:24 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 00:10:19 -05:00
|
|
|
// GetElementsForDashboard gets all connected elements for a specific dashboard.
|
2023-02-01 10:32:05 -06:00
|
|
|
func (l *LibraryElementService) GetElementsForDashboard(c context.Context, dashboardID int64) (map[string]model.LibraryElementDTO, error) {
|
2021-05-11 00:10:19 -05:00
|
|
|
return l.getElementsForDashboardID(c, dashboardID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectElementsToDashboard connects elements to a specific dashboard.
|
2022-08-10 04:56:48 -05:00
|
|
|
func (l *LibraryElementService) ConnectElementsToDashboard(c context.Context, signedInUser *user.SignedInUser, elementUIDs []string, dashboardID int64) error {
|
2021-09-27 02:04:36 -05:00
|
|
|
return l.connectElementsToDashboardID(c, signedInUser, elementUIDs, dashboardID)
|
2021-05-11 00:10:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectElementsFromDashboard disconnects elements from a specific dashboard.
|
2021-09-27 02:04:36 -05:00
|
|
|
func (l *LibraryElementService) DisconnectElementsFromDashboard(c context.Context, dashboardID int64) error {
|
2021-05-11 00:10:19 -05:00
|
|
|
return l.disconnectElementsFromDashboardID(c, dashboardID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLibraryElementsInFolder deletes all elements for a specific folder.
|
2022-08-10 04:56:48 -05:00
|
|
|
func (l *LibraryElementService) DeleteLibraryElementsInFolder(c context.Context, signedInUser *user.SignedInUser, folderUID string) error {
|
2021-09-27 02:04:36 -05:00
|
|
|
return l.deleteLibraryElementsInFolderUID(c, signedInUser, folderUID)
|
2021-05-11 00:10:19 -05:00
|
|
|
}
|