Playlist: update service so it supports both read+write (#55959)

This commit is contained in:
Ryan McKinley
2022-10-04 11:11:18 -04:00
committed by GitHub
parent d293055ef6
commit 3b1a8d45ed
14 changed files with 118 additions and 287 deletions
+9 -3
View File
@@ -27,11 +27,17 @@ func exportSystemPlaylists(helper *commitHelper, job *gitExportJob) error {
comment: "Export playlists",
}
for _, playlist := range res {
// TODO: fix the playlist API so it returns the json we need :)
for _, item := range res {
playlist, err := job.playlistService.Get(helper.ctx, &playlist.GetPlaylistByUidQuery{
UID: item.UID,
OrgId: helper.orgID,
})
if err != nil {
return err
}
gitcmd.body = append(gitcmd.body, commitBody{
fpath: filepath.Join(helper.orgDir, "system", "playlists", fmt.Sprintf("%s-playlist.json", playlist.UID)),
fpath: filepath.Join(helper.orgDir, "system", "playlists", fmt.Sprintf("%s-playlist.json", playlist.Uid)),
body: prettyJSON(playlist),
})
}
+16 -18
View File
@@ -22,20 +22,17 @@ type Playlist struct {
OrgId int64 `json:"-" db:"org_id"`
}
type PlaylistDTO struct {
playlist.Model
OrgId int64 `json:"-"`
}
type PlaylistDTO = playlist.Model
type PlaylistItemDTO = playlist.PlaylistItem
type PlaylistItemType = playlist.PlaylistItemType
type PlaylistItem struct {
Id int64 `db:"id"`
PlaylistId int64 `db:"playlist_id"`
Type string `db:"type"`
Value string `db:"value"`
Order int `db:"order"`
Title string `db:"title"`
Type string `json:"type" db:"type"`
Value string `json:"value" db:"value"`
Order int `json:"order" db:"order"`
Title string `json:"title" db:"title"`
}
type Playlists []*Playlist
@@ -45,18 +42,18 @@ type Playlists []*Playlist
//
type UpdatePlaylistCommand struct {
OrgId int64 `json:"-"`
UID string `json:"uid"`
Name string `json:"name" binding:"Required"`
Interval string `json:"interval"`
Items []PlaylistItemDTO `json:"items"`
OrgId int64 `json:"-"`
UID string `json:"uid"`
Name string `json:"name" binding:"Required"`
Interval string `json:"interval"`
Items []PlaylistItem `json:"items"`
}
type CreatePlaylistCommand struct {
Name string `json:"name" binding:"Required"`
Interval string `json:"interval"`
Items []PlaylistItemDTO `json:"items"`
OrgId int64 `json:"-"`
Name string `json:"name" binding:"Required"`
Interval string `json:"interval"`
Items []PlaylistItem `json:"items"`
OrgId int64 `json:"-"`
}
type DeletePlaylistCommand struct {
@@ -69,6 +66,7 @@ type DeletePlaylistCommand struct {
//
type GetPlaylistsQuery struct {
// NOTE: the frontend never sends this query
Name string
Limit int
OrgId int64
+2 -2
View File
@@ -7,8 +7,8 @@ import (
type Service interface {
Create(context.Context, *CreatePlaylistCommand) (*Playlist, error)
Update(context.Context, *UpdatePlaylistCommand) (*PlaylistDTO, error)
Get(context.Context, *GetPlaylistByUidQuery) (*Playlist, error)
GetItems(context.Context, *GetPlaylistItemsByUidQuery) ([]PlaylistItem, error)
GetWithoutItems(context.Context, *GetPlaylistByUidQuery) (*Playlist, error)
Get(context.Context, *GetPlaylistByUidQuery) (*PlaylistDTO, error)
Search(context.Context, *GetPlaylistsQuery) (Playlists, error)
Delete(ctx context.Context, cmd *DeletePlaylistCommand) error
}
+24 -3
View File
@@ -35,12 +35,33 @@ func (s *Service) Update(ctx context.Context, cmd *playlist.UpdatePlaylistComman
return s.store.Update(ctx, cmd)
}
func (s *Service) Get(ctx context.Context, q *playlist.GetPlaylistByUidQuery) (*playlist.Playlist, error) {
func (s *Service) GetWithoutItems(ctx context.Context, q *playlist.GetPlaylistByUidQuery) (*playlist.Playlist, error) {
return s.store.Get(ctx, q)
}
func (s *Service) GetItems(ctx context.Context, q *playlist.GetPlaylistItemsByUidQuery) ([]playlist.PlaylistItem, error) {
return s.store.GetItems(ctx, q)
func (s *Service) Get(ctx context.Context, q *playlist.GetPlaylistByUidQuery) (*playlist.PlaylistDTO, error) {
v, err := s.store.Get(ctx, q)
if err != nil {
return nil, err
}
rawItems, err := s.store.GetItems(ctx, &playlist.GetPlaylistItemsByUidQuery{
PlaylistUID: v.UID,
OrgId: q.OrgId,
})
if err != nil {
return nil, err
}
items := make([]playlist.PlaylistItemDTO, len(rawItems))
for i := 0; i < len(rawItems); i++ {
items[i].Type = playlist.PlaylistItemType(rawItems[i].Type)
items[i].Value = rawItems[i].Value
}
return &playlist.PlaylistDTO{
Uid: v.UID,
Name: v.Name,
Interval: v.Interval,
Items: &items,
}, nil
}
func (s *Service) Search(ctx context.Context, q *playlist.GetPlaylistsQuery) (playlist.Playlists, error) {
@@ -39,12 +39,12 @@ func (s *sqlxStore) Insert(ctx context.Context, cmd *playlist.CreatePlaylistComm
if len(cmd.Items) > 0 {
playlistItems := make([]playlist.PlaylistItem, 0)
for _, item := range cmd.Items {
for order, item := range cmd.Items {
playlistItems = append(playlistItems, playlist.PlaylistItem{
PlaylistId: p.Id,
Type: string(item.Type),
Type: item.Type,
Value: item.Value,
Order: item.Order,
Order: order + 1,
Title: item.Title,
})
}
@@ -94,7 +94,7 @@ func (s *sqlxStore) Update(ctx context.Context, cmd *playlist.UpdatePlaylistComm
for index, item := range cmd.Items {
playlistItems = append(playlistItems, playlist.PlaylistItem{
PlaylistId: p.Id,
Type: string(item.Type),
Type: item.Type,
Value: item.Value,
Order: index + 1,
Title: item.Title,
@@ -197,5 +197,5 @@ func newGenerateAndValidateNewPlaylistUid(ctx context.Context, sess *session.Ses
}
}
return "", models.ErrPlaylistFailedGenerateUniqueUid
return "", playlist.ErrPlaylistFailedGenerateUniqueUid
}
@@ -20,7 +20,7 @@ func testIntegrationPlaylistDataAccess(t *testing.T, fn getStore) {
playlistStore := fn(ss)
t.Run("Can create playlist", func(t *testing.T) {
items := []playlist.PlaylistItemDTO{
items := []playlist.PlaylistItem{
{Title: "graphite", Value: "graphite", Type: "dashboard_by_tag"},
{Title: "Backend response times", Value: "3", Type: "dashboard_by_id"},
}
@@ -44,7 +44,7 @@ func testIntegrationPlaylistDataAccess(t *testing.T, fn getStore) {
})
t.Run("Can update playlist", func(t *testing.T) {
items := []playlist.PlaylistItemDTO{
items := []playlist.PlaylistItem{
{Title: "influxdb", Value: "influxdb", Type: "dashboard_by_tag"},
{Title: "Backend response times", Value: "2", Type: "dashboard_by_id"},
}
@@ -66,7 +66,7 @@ func testIntegrationPlaylistDataAccess(t *testing.T, fn getStore) {
})
t.Run("Search playlist", func(t *testing.T) {
items := []playlist.PlaylistItemDTO{
items := []playlist.PlaylistItem{
{Title: "graphite", Value: "graphite", Type: "dashboard_by_tag"},
{Title: "Backend response times", Value: "3", Type: "dashboard_by_id"},
}
@@ -35,12 +35,12 @@ func (s *sqlStore) Insert(ctx context.Context, cmd *playlist.CreatePlaylistComma
}
playlistItems := make([]playlist.PlaylistItem, 0)
for _, item := range cmd.Items {
for order, item := range cmd.Items {
playlistItems = append(playlistItems, playlist.PlaylistItem{
PlaylistId: p.Id,
Type: string(item.Type),
Type: item.Type,
Value: item.Value,
Order: item.Order,
Order: order + 1,
Title: item.Title,
})
}
@@ -70,12 +70,10 @@ func (s *sqlStore) Update(ctx context.Context, cmd *playlist.UpdatePlaylistComma
p.Id = existingPlaylist.Id
dto = playlist.PlaylistDTO{
OrgId: p.OrgId,
Uid: p.UID,
Name: p.Name,
Interval: p.Interval,
}
dto.Id = p.Id
dto.Uid = p.UID
dto.Name = p.Name
dto.Interval = p.Interval
_, err = sess.Where("id=?", p.Id).Cols("name", "interval").Update(&p)
if err != nil {
@@ -89,12 +87,12 @@ func (s *sqlStore) Update(ctx context.Context, cmd *playlist.UpdatePlaylistComma
return err
}
playlistItems := make([]models.PlaylistItem, 0)
playlistItems := make([]playlist.PlaylistItem, 0)
for index, item := range cmd.Items {
playlistItems = append(playlistItems, models.PlaylistItem{
playlistItems = append(playlistItems, playlist.PlaylistItem{
PlaylistId: p.Id,
Type: string(item.Type),
Type: item.Type,
Value: item.Value,
Order: index + 1,
Title: item.Title,
@@ -198,7 +196,7 @@ func generateAndValidateNewPlaylistUid(sess *sqlstore.DBSession, orgId int64) (s
for i := 0; i < 3; i++ {
uid := generateNewUid()
playlist := models.Playlist{OrgId: orgId, UID: uid}
playlist := playlist.Playlist{OrgId: orgId, UID: uid}
exists, err := sess.Get(&playlist)
if err != nil {
return "", err
@@ -209,7 +207,7 @@ func generateAndValidateNewPlaylistUid(sess *sqlstore.DBSession, orgId int64) (s
}
}
return "", models.ErrPlaylistFailedGenerateUniqueUid
return "", playlist.ErrPlaylistFailedGenerateUniqueUid
}
var generateNewUid func() string = util.GenerateShortUID
@@ -267,30 +267,6 @@ func (m SQLStoreMock) GetDashboardACLInfoList(ctx context.Context, query *models
return m.ExpectedError
}
func (m *SQLStoreMock) CreatePlaylist(ctx context.Context, cmd *models.CreatePlaylistCommand) error {
return m.ExpectedError
}
func (m *SQLStoreMock) UpdatePlaylist(ctx context.Context, cmd *models.UpdatePlaylistCommand) error {
return m.ExpectedError
}
func (m *SQLStoreMock) GetPlaylist(ctx context.Context, query *models.GetPlaylistByUidQuery) error {
return m.ExpectedError
}
func (m *SQLStoreMock) DeletePlaylist(ctx context.Context, cmd *models.DeletePlaylistCommand) error {
return m.ExpectedError
}
func (m *SQLStoreMock) SearchPlaylists(ctx context.Context, query *models.GetPlaylistsQuery) error {
return m.ExpectedError
}
func (m *SQLStoreMock) GetPlaylistItem(ctx context.Context, query *models.GetPlaylistItemsByUidQuery) error {
return m.ExpectedError
}
func (m *SQLStoreMock) GetAlertById(ctx context.Context, query *models.GetAlertByIdQuery) error {
query.Result = m.ExpectedAlert
return m.ExpectedError