refactor(playlist): refactor of playlist feature, and PR #3776
This commit is contained in:
@@ -3,20 +3,23 @@ package migrations
|
||||
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
|
||||
func addPlaylistMigrations(mg *Migrator) {
|
||||
playlistV1 := Table{
|
||||
mg.AddMigration("Drop old table playlist table", NewDropTableMigration("playlist"))
|
||||
mg.AddMigration("Drop old table playlist_item table", NewDropTableMigration("playlist_item"))
|
||||
|
||||
playlistV2 := Table{
|
||||
Name: "playlist",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "interval", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
},
|
||||
}
|
||||
|
||||
// create table
|
||||
mg.AddMigration("create playlist table v1", NewAddTableMigration(playlistV1))
|
||||
mg.AddMigration("create playlist table v2", NewAddTableMigration(playlistV2))
|
||||
|
||||
playlistItemV1 := Table{
|
||||
playlistItemV2 := Table{
|
||||
Name: "playlist_item",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
@@ -28,5 +31,5 @@ func addPlaylistMigrations(mg *Migrator) {
|
||||
},
|
||||
}
|
||||
|
||||
mg.AddMigration("create playlist item table v1", NewAddTableMigration(playlistItemV1))
|
||||
mg.AddMigration("create playlist item table v2", NewAddTableMigration(playlistItemV2))
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package sqlstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -18,13 +19,13 @@ func init() {
|
||||
bus.AddHandler("sql", GetPlaylistItem)
|
||||
}
|
||||
|
||||
func CreatePlaylist(query *m.CreatePlaylistQuery) error {
|
||||
func CreatePlaylist(cmd *m.CreatePlaylistCommand) error {
|
||||
var err error
|
||||
|
||||
playlist := m.Playlist{
|
||||
Title: query.Title,
|
||||
Interval: query.Interval,
|
||||
OrgId: query.OrgId,
|
||||
Name: cmd.Name,
|
||||
Interval: cmd.Interval,
|
||||
OrgId: cmd.OrgId,
|
||||
}
|
||||
|
||||
_, err = x.Insert(&playlist)
|
||||
@@ -32,7 +33,7 @@ func CreatePlaylist(query *m.CreatePlaylistQuery) error {
|
||||
fmt.Printf("%v", playlist.Id)
|
||||
|
||||
playlistItems := make([]m.PlaylistItem, 0)
|
||||
for _, item := range query.Items {
|
||||
for _, item := range cmd.Items {
|
||||
playlistItems = append(playlistItems, m.PlaylistItem{
|
||||
PlaylistId: playlist.Id,
|
||||
Type: item.Type,
|
||||
@@ -44,40 +45,40 @@ func CreatePlaylist(query *m.CreatePlaylistQuery) error {
|
||||
|
||||
_, err = x.Insert(&playlistItems)
|
||||
|
||||
query.Result = &playlist
|
||||
cmd.Result = &playlist
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdatePlaylist(query *m.UpdatePlaylistQuery) error {
|
||||
func UpdatePlaylist(cmd *m.UpdatePlaylistCommand) error {
|
||||
var err error
|
||||
x.Logger.SetLevel(5)
|
||||
playlist := m.Playlist{
|
||||
Id: query.Id,
|
||||
Title: query.Title,
|
||||
Interval: query.Interval,
|
||||
Id: cmd.Id,
|
||||
OrgId: cmd.OrgId,
|
||||
Name: cmd.Name,
|
||||
Interval: cmd.Interval,
|
||||
}
|
||||
|
||||
existingPlaylist := x.Where("id = ?", query.Id).Find(m.Playlist{})
|
||||
existingPlaylist := x.Where("id = ? AND org_id = ?", cmd.Id, cmd.OrgId).Find(m.Playlist{})
|
||||
|
||||
if existingPlaylist == nil {
|
||||
return m.ErrPlaylistNotFound
|
||||
}
|
||||
|
||||
query.Result = &m.PlaylistDTO{
|
||||
cmd.Result = &m.PlaylistDTO{
|
||||
Id: playlist.Id,
|
||||
OrgId: playlist.OrgId,
|
||||
Title: playlist.Title,
|
||||
Name: playlist.Name,
|
||||
Interval: playlist.Interval,
|
||||
}
|
||||
|
||||
_, err = x.Id(query.Id).Cols("id", "title", "timespan").Update(&playlist)
|
||||
_, err = x.Id(cmd.Id).Cols("id", "name", "interval").Update(&playlist)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rawSql := "DELETE FROM playlist_item WHERE playlist_id = ?"
|
||||
_, err = x.Exec(rawSql, query.Id)
|
||||
_, err = x.Exec(rawSql, cmd.Id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -85,7 +86,7 @@ func UpdatePlaylist(query *m.UpdatePlaylistQuery) error {
|
||||
|
||||
playlistItems := make([]m.PlaylistItem, 0)
|
||||
|
||||
for _, item := range query.Items {
|
||||
for _, item := range cmd.Items {
|
||||
playlistItems = append(playlistItems, m.PlaylistItem{
|
||||
PlaylistId: playlist.Id,
|
||||
Type: item.Type,
|
||||
@@ -113,33 +114,33 @@ func GetPlaylist(query *m.GetPlaylistByIdQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DeletePlaylist(query *m.DeletePlaylistQuery) error {
|
||||
if query.Id == 0 {
|
||||
func DeletePlaylist(cmd *m.DeletePlaylistCommand) error {
|
||||
if cmd.Id == 0 {
|
||||
return m.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawPlaylistSql = "DELETE FROM playlist WHERE id = ?"
|
||||
_, err := sess.Exec(rawPlaylistSql, query.Id)
|
||||
var rawPlaylistSql = "DELETE FROM playlist WHERE id = ? and org_id = ?"
|
||||
_, err := sess.Exec(rawPlaylistSql, cmd.Id, cmd.OrgId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rawItemSql = "DELETE FROM playlist_item WHERE playlist_id = ?"
|
||||
_, err2 := sess.Exec(rawItemSql, query.Id)
|
||||
_, err2 := sess.Exec(rawItemSql, cmd.Id)
|
||||
|
||||
return err2
|
||||
})
|
||||
}
|
||||
|
||||
func SearchPlaylists(query *m.PlaylistQuery) error {
|
||||
func SearchPlaylists(query *m.GetPlaylistsQuery) error {
|
||||
var playlists = make(m.Playlists, 0)
|
||||
|
||||
sess := x.Limit(query.Limit)
|
||||
|
||||
if query.Title != "" {
|
||||
sess.Where("title LIKE ?", query.Title)
|
||||
if query.Name != "" {
|
||||
sess.Where("name LIKE ?", query.Name)
|
||||
}
|
||||
|
||||
sess.Where("org_id = ?", query.OrgId)
|
||||
|
||||
Reference in New Issue
Block a user