feat(playlist): changes to relation table
Also introduces an abstraction between playlist and dashboard. This will make it possible to att search, and tag filtering to playlists without any major refactoring
This commit is contained in:
@@ -220,26 +220,6 @@ func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
|
||||
}
|
||||
}
|
||||
|
||||
var playlists = make(m.Playlists, 0)
|
||||
err = sess.Where("data LIKE ?", fmt.Sprintf("%%%v%%", dashboard.Id)).Find(&playlists)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, playlist := range playlists {
|
||||
filteredData := make([]int64, 0)
|
||||
for _, plDashboardId := range playlist.Data {
|
||||
if plDashboardId != dashboard.Id {
|
||||
filteredData = append(filteredData, plDashboardId)
|
||||
}
|
||||
}
|
||||
playlist.Data = filteredData
|
||||
_, err = sess.Id(playlist.Id).Cols("data").Update(playlist)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ func addPlaylistMigrations(mg *Migrator) {
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "data", Type: DB_Text, Nullable: false},
|
||||
{Name: "timespan", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
},
|
||||
@@ -17,4 +15,18 @@ func addPlaylistMigrations(mg *Migrator) {
|
||||
|
||||
// create table
|
||||
mg.AddMigration("create playlist table v1", NewAddTableMigration(playlistV1))
|
||||
|
||||
playlistItemV1 := Table{
|
||||
Name: "playlist_item",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "playlist_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "value", Type: DB_Text, Nullable: false},
|
||||
{Name: "title", Type: DB_Text, Nullable: false},
|
||||
{Name: "order", Type: DB_Int, Nullable: false},
|
||||
},
|
||||
}
|
||||
|
||||
mg.AddMigration("create playlist item table v1", NewAddTableMigration(playlistItemV1))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -14,6 +15,7 @@ func init() {
|
||||
bus.AddHandler("sql", SearchPlaylists)
|
||||
bus.AddHandler("sql", GetPlaylist)
|
||||
bus.AddHandler("sql", GetPlaylistDashboards)
|
||||
bus.AddHandler("sql", GetPlaylistItem)
|
||||
}
|
||||
|
||||
func CreatePlaylist(query *m.CreatePlaylistQuery) error {
|
||||
@@ -21,14 +23,27 @@ func CreatePlaylist(query *m.CreatePlaylistQuery) error {
|
||||
|
||||
playlist := m.Playlist{
|
||||
Title: query.Title,
|
||||
Type: query.Type,
|
||||
Data: query.Data,
|
||||
Timespan: query.Timespan,
|
||||
OrgId: query.OrgId,
|
||||
}
|
||||
|
||||
_, err = x.Insert(&playlist)
|
||||
|
||||
fmt.Printf("%v", playlist.Id)
|
||||
|
||||
playlistItems := make([]m.PlaylistItem, 0)
|
||||
for _, item := range query.Items {
|
||||
playlistItems = append(playlistItems, m.PlaylistItem{
|
||||
PlaylistId: playlist.Id,
|
||||
Type: item.Type,
|
||||
Value: item.Value,
|
||||
Order: item.Order,
|
||||
Title: item.Title,
|
||||
})
|
||||
}
|
||||
|
||||
_, err = x.Insert(&playlistItems)
|
||||
|
||||
query.Result = &playlist
|
||||
return err
|
||||
}
|
||||
@@ -39,8 +54,6 @@ func UpdatePlaylist(query *m.UpdatePlaylistQuery) error {
|
||||
playlist := m.Playlist{
|
||||
Id: query.Id,
|
||||
Title: query.Title,
|
||||
Type: query.Type,
|
||||
Data: query.Data,
|
||||
Timespan: query.Timespan,
|
||||
}
|
||||
|
||||
@@ -50,9 +63,39 @@ func UpdatePlaylist(query *m.UpdatePlaylistQuery) error {
|
||||
return m.ErrPlaylistNotFound
|
||||
}
|
||||
|
||||
_, err = x.Id(query.Id).Cols("id", "title", "data", "timespan").Update(&playlist)
|
||||
query.Result = &m.PlaylistDTO{
|
||||
Id: playlist.Id,
|
||||
OrgId: playlist.OrgId,
|
||||
Title: playlist.Title,
|
||||
Timespan: playlist.Timespan,
|
||||
}
|
||||
|
||||
_, err = x.Id(query.Id).Cols("id", "title", "timespan").Update(&playlist)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rawSql := "DELETE FROM playlist_item WHERE playlist_id = ?"
|
||||
_, err = x.Exec(rawSql, query.Id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
playlistItems := make([]m.PlaylistItem, 0)
|
||||
for _, item := range query.Items {
|
||||
playlistItems = append(playlistItems, m.PlaylistItem{
|
||||
PlaylistId: playlist.Id,
|
||||
Type: item.Type,
|
||||
Value: item.Value,
|
||||
Order: item.Order,
|
||||
Title: item.Title,
|
||||
})
|
||||
}
|
||||
|
||||
_, err = x.Insert(&playlistItems)
|
||||
|
||||
query.Result = &playlist
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -63,6 +106,7 @@ func GetPlaylist(query *m.GetPlaylistByIdQuery) error {
|
||||
|
||||
playlist := m.Playlist{}
|
||||
_, err := x.Id(query.Id).Get(&playlist)
|
||||
|
||||
query.Result = &playlist
|
||||
|
||||
return err
|
||||
@@ -74,9 +118,17 @@ func DeletePlaylist(query *m.DeletePlaylistQuery) error {
|
||||
}
|
||||
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "DELETE FROM playlist WHERE id = ?"
|
||||
_, err := sess.Exec(rawSql, query.Id)
|
||||
return err
|
||||
var rawPlaylistSql = "DELETE FROM playlist WHERE id = ?"
|
||||
_, err := sess.Exec(rawPlaylistSql, query.Id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rawItemSql = "DELETE FROM playlist_item WHERE playlist_id = ?"
|
||||
_, err2 := sess.Exec(rawItemSql, query.Id)
|
||||
|
||||
return err2
|
||||
})
|
||||
}
|
||||
|
||||
@@ -96,31 +148,32 @@ func SearchPlaylists(query *m.PlaylistQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func GetPlaylistItem(query *m.GetPlaylistItemsByIdQuery) error {
|
||||
if query.PlaylistId == 0 {
|
||||
return m.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
var playlistItems = make([]m.PlaylistItem, 0)
|
||||
err := x.Where("playlist_id=?", query.PlaylistId).Find(&playlistItems)
|
||||
|
||||
query.Result = &playlistItems
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func GetPlaylistDashboards(query *m.GetPlaylistDashboardsQuery) error {
|
||||
if query.Id == 0 {
|
||||
if len(query.DashboardIds) == 0 {
|
||||
return m.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
var dashboards = make(m.PlaylistDashboards, 0)
|
||||
var playlist = m.Playlist{}
|
||||
|
||||
hasPlaylist, err := x.Id(query.Id).Get(&playlist)
|
||||
|
||||
err := x.In("id", query.DashboardIds).Find(&dashboards)
|
||||
query.Result = &dashboards
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !hasPlaylist || len(playlist.Data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = x.In("id", playlist.Data).Find(&dashboards)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user