-
Notifications
You must be signed in to change notification settings - Fork 34
/
playlist.go
161 lines (121 loc) · 3.25 KB
/
playlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package sqlstore
import (
"fmt"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", CreatePlaylist)
bus.AddHandler("sql", UpdatePlaylist)
bus.AddHandler("sql", DeletePlaylist)
bus.AddHandler("sql", SearchPlaylists)
bus.AddHandler("sql", GetPlaylist)
bus.AddHandler("sql", GetPlaylistItem)
}
func CreatePlaylist(cmd *m.CreatePlaylistCommand) error {
var err error
playlist := m.Playlist{
Name: cmd.Name,
Interval: cmd.Interval,
OrgId: cmd.OrgId,
}
_, err = x.Insert(&playlist)
fmt.Printf("%v", playlist.Id)
playlistItems := make([]m.PlaylistItem, 0)
for _, item := range cmd.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)
cmd.Result = &playlist
return err
}
func UpdatePlaylist(cmd *m.UpdatePlaylistCommand) error {
var err error
playlist := m.Playlist{
Id: cmd.Id,
OrgId: cmd.OrgId,
Name: cmd.Name,
Interval: cmd.Interval,
}
existingPlaylist := x.Where("id = ? AND org_id = ?", cmd.Id, cmd.OrgId).Find(m.Playlist{})
if existingPlaylist == nil {
return m.ErrPlaylistNotFound
}
cmd.Result = &m.PlaylistDTO{
Id: playlist.Id,
OrgId: playlist.OrgId,
Name: playlist.Name,
Interval: playlist.Interval,
}
_, 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, cmd.Id)
if err != nil {
return err
}
playlistItems := make([]m.PlaylistItem, 0)
for index, item := range cmd.Items {
playlistItems = append(playlistItems, m.PlaylistItem{
PlaylistId: playlist.Id,
Type: item.Type,
Value: item.Value,
Order: index + 1,
Title: item.Title,
})
}
_, err = x.Insert(&playlistItems)
return err
}
func GetPlaylist(query *m.GetPlaylistByIdQuery) error {
if query.Id == 0 {
return m.ErrCommandValidationFailed
}
playlist := m.Playlist{}
_, err := x.Id(query.Id).Get(&playlist)
query.Result = &playlist
return err
}
func DeletePlaylist(cmd *m.DeletePlaylistCommand) error {
if cmd.Id == 0 {
return m.ErrCommandValidationFailed
}
return inTransaction(func(sess *DBSession) error {
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, cmd.Id)
return err2
})
}
func SearchPlaylists(query *m.GetPlaylistsQuery) error {
var playlists = make(m.Playlists, 0)
sess := x.Limit(query.Limit)
if query.Name != "" {
sess.Where("name LIKE ?", query.Name)
}
sess.Where("org_id = ?", query.OrgId)
err := sess.Find(&playlists)
query.Result = playlists
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
}