Playlist: update service so it supports both read+write (#55959)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user