2015-12-22 04:07:15 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-12-22 04:07:15 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", CreatePlaylist)
|
|
|
|
bus.AddHandler("sql", UpdatePlaylist)
|
|
|
|
bus.AddHandler("sql", DeletePlaylist)
|
|
|
|
bus.AddHandler("sql", SearchPlaylists)
|
|
|
|
bus.AddHandler("sql", GetPlaylist)
|
2016-01-08 10:32:55 -06:00
|
|
|
bus.AddHandler("sql", GetPlaylistItem)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func CreatePlaylist(cmd *models.CreatePlaylistCommand) error {
|
|
|
|
playlist := models.Playlist{
|
2016-01-18 09:00:11 -06:00
|
|
|
Name: cmd.Name,
|
|
|
|
Interval: cmd.Interval,
|
|
|
|
OrgId: cmd.OrgId,
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2018-01-23 15:28:47 -06:00
|
|
|
_, err := x.Insert(&playlist)
|
2018-04-23 12:28:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-22 04:07:15 -06:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
playlistItems := make([]models.PlaylistItem, 0)
|
2016-01-18 09:00:11 -06:00
|
|
|
for _, item := range cmd.Items {
|
2020-02-29 06:35:15 -06:00
|
|
|
playlistItems = append(playlistItems, models.PlaylistItem{
|
2016-01-08 10:32:55 -06:00
|
|
|
PlaylistId: playlist.Id,
|
|
|
|
Type: item.Type,
|
|
|
|
Value: item.Value,
|
|
|
|
Order: item.Order,
|
|
|
|
Title: item.Title,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = x.Insert(&playlistItems)
|
|
|
|
|
2016-01-18 09:00:11 -06:00
|
|
|
cmd.Result = &playlist
|
2015-12-22 04:07:15 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func UpdatePlaylist(cmd *models.UpdatePlaylistCommand) error {
|
|
|
|
playlist := models.Playlist{
|
2016-01-18 09:00:11 -06:00
|
|
|
Id: cmd.Id,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Interval: cmd.Interval,
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
existingPlaylist := x.Where("id = ? AND org_id = ?", cmd.Id, cmd.OrgId).Find(models.Playlist{})
|
2015-12-22 04:07:15 -06:00
|
|
|
|
|
|
|
if existingPlaylist == nil {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrPlaylistNotFound
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd.Result = &models.PlaylistDTO{
|
2016-01-08 10:32:55 -06:00
|
|
|
Id: playlist.Id,
|
|
|
|
OrgId: playlist.OrgId,
|
2016-01-18 09:00:11 -06:00
|
|
|
Name: playlist.Name,
|
2016-01-12 06:56:47 -06:00
|
|
|
Interval: playlist.Interval,
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2018-05-10 09:54:21 -05:00
|
|
|
_, err := x.ID(cmd.Id).Cols("name", "interval").Update(&playlist)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
rawSQL := "DELETE FROM playlist_item WHERE playlist_id = ?"
|
|
|
|
_, err = x.Exec(rawSQL, cmd.Id)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
playlistItems := make([]models.PlaylistItem, 0)
|
2016-01-11 09:54:52 -06:00
|
|
|
|
2017-05-24 06:21:43 -05:00
|
|
|
for index, item := range cmd.Items {
|
2020-02-29 06:35:15 -06:00
|
|
|
playlistItems = append(playlistItems, models.PlaylistItem{
|
2016-01-08 10:32:55 -06:00
|
|
|
PlaylistId: playlist.Id,
|
|
|
|
Type: item.Type,
|
|
|
|
Value: item.Value,
|
2017-05-24 06:21:43 -05:00
|
|
|
Order: index + 1,
|
2016-01-08 10:32:55 -06:00
|
|
|
Title: item.Title,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = x.Insert(&playlistItems)
|
2015-12-22 04:07:15 -06:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetPlaylist(query *models.GetPlaylistByIdQuery) error {
|
2015-12-22 04:07:15 -06:00
|
|
|
if query.Id == 0 {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrCommandValidationFailed
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
playlist := models.Playlist{}
|
2018-01-23 15:28:47 -06:00
|
|
|
_, err := x.ID(query.Id).Get(&playlist)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
2015-12-22 04:07:15 -06:00
|
|
|
query.Result = &playlist
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func DeletePlaylist(cmd *models.DeletePlaylistCommand) error {
|
2020-11-14 02:49:07 -06:00
|
|
|
if cmd.Id == 0 || cmd.OrgId == 0 {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrCommandValidationFailed
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawPlaylistSQL = "DELETE FROM playlist WHERE id = ? and org_id = ?"
|
|
|
|
_, err := sess.Exec(rawPlaylistSQL, cmd.Id, cmd.OrgId)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawItemSQL = "DELETE FROM playlist_item WHERE playlist_id = ?"
|
|
|
|
_, err2 := sess.Exec(rawItemSQL, cmd.Id)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
|
|
|
return err2
|
2015-12-22 04:07:15 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func SearchPlaylists(query *models.GetPlaylistsQuery) error {
|
|
|
|
var playlists = make(models.Playlists, 0)
|
2015-12-22 04:07:15 -06:00
|
|
|
|
|
|
|
sess := x.Limit(query.Limit)
|
|
|
|
|
2016-01-18 09:00:11 -06:00
|
|
|
if query.Name != "" {
|
2021-09-22 04:45:29 -05:00
|
|
|
sess.Where("name LIKE ?", "%"+query.Name+"%")
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
sess.Where("org_id = ?", query.OrgId)
|
|
|
|
err := sess.Find(&playlists)
|
|
|
|
query.Result = playlists
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetPlaylistItem(query *models.GetPlaylistItemsByIdQuery) error {
|
2016-01-08 10:32:55 -06:00
|
|
|
if query.PlaylistId == 0 {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrCommandValidationFailed
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
var playlistItems = make([]models.PlaylistItem, 0)
|
2016-01-08 10:32:55 -06:00
|
|
|
err := x.Where("playlist_id=?", query.PlaylistId).Find(&playlistItems)
|
2015-12-22 04:07:15 -06:00
|
|
|
|
2016-01-08 10:32:55 -06:00
|
|
|
query.Result = &playlistItems
|
2016-01-08 09:13:49 -06:00
|
|
|
|
2016-01-08 10:32:55 -06:00
|
|
|
return err
|
|
|
|
}
|