@@ -6,25 +6,26 @@ import (
6
6
7
7
"github.com/grafana/grafana/pkg/api/response"
8
8
"github.com/grafana/grafana/pkg/models"
9
+ "github.com/grafana/grafana/pkg/services/playlist"
9
10
"github.com/grafana/grafana/pkg/web"
10
11
)
11
12
12
13
func (hs * HTTPServer ) ValidateOrgPlaylist (c * models.ReqContext ) {
13
14
uid := web .Params (c .Req )[":uid" ]
14
- query := models .GetPlaylistByUidQuery {UID : uid , OrgId : c .OrgId }
15
- err := hs .SQLStore . GetPlaylist (c .Req .Context (), & query )
15
+ query := playlist .GetPlaylistByUidQuery {UID : uid , OrgId : c .OrgId }
16
+ p , err := hs .playlistService . Get (c .Req .Context (), & query )
16
17
17
18
if err != nil {
18
19
c .JsonApiErr (404 , "Playlist not found" , err )
19
20
return
20
21
}
21
22
22
- if query . Result .OrgId == 0 {
23
+ if p .OrgId == 0 {
23
24
c .JsonApiErr (404 , "Playlist not found" , err )
24
25
return
25
26
}
26
27
27
- if query . Result .OrgId != c .OrgId {
28
+ if p .OrgId != c .OrgId {
28
29
c .JsonApiErr (403 , "You are not allowed to edit/view playlist" , nil )
29
30
return
30
31
}
@@ -38,53 +39,54 @@ func (hs *HTTPServer) SearchPlaylists(c *models.ReqContext) response.Response {
38
39
limit = 1000
39
40
}
40
41
41
- searchQuery := models .GetPlaylistsQuery {
42
+ searchQuery := playlist .GetPlaylistsQuery {
42
43
Name : query ,
43
44
Limit : limit ,
44
45
OrgId : c .OrgId ,
45
46
}
46
47
47
- err := hs .SQLStore . SearchPlaylists (c .Req .Context (), & searchQuery )
48
+ playlists , err := hs .playlistService . Search (c .Req .Context (), & searchQuery )
48
49
if err != nil {
49
50
return response .Error (500 , "Search failed" , err )
50
51
}
51
52
52
- return response .JSON (http .StatusOK , searchQuery . Result )
53
+ return response .JSON (http .StatusOK , playlists )
53
54
}
54
55
55
56
func (hs * HTTPServer ) GetPlaylist (c * models.ReqContext ) response.Response {
56
57
uid := web .Params (c .Req )[":uid" ]
57
- cmd := models .GetPlaylistByUidQuery {UID : uid , OrgId : c .OrgId }
58
+ cmd := playlist .GetPlaylistByUidQuery {UID : uid , OrgId : c .OrgId }
58
59
59
- if err := hs .SQLStore .GetPlaylist (c .Req .Context (), & cmd ); err != nil {
60
+ p , err := hs .playlistService .Get (c .Req .Context (), & cmd )
61
+ if err != nil {
60
62
return response .Error (500 , "Playlist not found" , err )
61
63
}
62
64
63
65
playlistDTOs , _ := hs .LoadPlaylistItemDTOs (c .Req .Context (), uid , c .OrgId )
64
66
65
- dto := & models .PlaylistDTO {
66
- Id : cmd . Result .Id ,
67
- UID : cmd . Result .UID ,
68
- Name : cmd . Result .Name ,
69
- Interval : cmd . Result .Interval ,
70
- OrgId : cmd . Result .OrgId ,
67
+ dto := & playlist .PlaylistDTO {
68
+ Id : p .Id ,
69
+ UID : p .UID ,
70
+ Name : p .Name ,
71
+ Interval : p .Interval ,
72
+ OrgId : p .OrgId ,
71
73
Items : playlistDTOs ,
72
74
}
73
75
74
76
return response .JSON (http .StatusOK , dto )
75
77
}
76
78
77
- func (hs * HTTPServer ) LoadPlaylistItemDTOs (ctx context.Context , uid string , orgId int64 ) ([]models .PlaylistItemDTO , error ) {
79
+ func (hs * HTTPServer ) LoadPlaylistItemDTOs (ctx context.Context , uid string , orgId int64 ) ([]playlist .PlaylistItemDTO , error ) {
78
80
playlistitems , err := hs .LoadPlaylistItems (ctx , uid , orgId )
79
81
80
82
if err != nil {
81
83
return nil , err
82
84
}
83
85
84
- playlistDTOs := make ([]models .PlaylistItemDTO , 0 )
86
+ playlistDTOs := make ([]playlist .PlaylistItemDTO , 0 )
85
87
86
88
for _ , item := range playlistitems {
87
- playlistDTOs = append (playlistDTOs , models .PlaylistItemDTO {
89
+ playlistDTOs = append (playlistDTOs , playlist .PlaylistItemDTO {
88
90
Id : item .Id ,
89
91
PlaylistId : item .PlaylistId ,
90
92
Type : item .Type ,
@@ -97,13 +99,14 @@ func (hs *HTTPServer) LoadPlaylistItemDTOs(ctx context.Context, uid string, orgI
97
99
return playlistDTOs , nil
98
100
}
99
101
100
- func (hs * HTTPServer ) LoadPlaylistItems (ctx context.Context , uid string , orgId int64 ) ([]models.PlaylistItem , error ) {
101
- itemQuery := models.GetPlaylistItemsByUidQuery {PlaylistUID : uid , OrgId : orgId }
102
- if err := hs .SQLStore .GetPlaylistItem (ctx , & itemQuery ); err != nil {
102
+ func (hs * HTTPServer ) LoadPlaylistItems (ctx context.Context , uid string , orgId int64 ) ([]playlist.PlaylistItem , error ) {
103
+ itemQuery := playlist.GetPlaylistItemsByUidQuery {PlaylistUID : uid , OrgId : orgId }
104
+ items , err := hs .playlistService .GetItems (ctx , & itemQuery )
105
+ if err != nil {
103
106
return nil , err
104
107
}
105
108
106
- return * itemQuery . Result , nil
109
+ return items , nil
107
110
}
108
111
109
112
func (hs * HTTPServer ) GetPlaylistItems (c * models.ReqContext ) response.Response {
@@ -132,37 +135,39 @@ func (hs *HTTPServer) GetPlaylistDashboards(c *models.ReqContext) response.Respo
132
135
func (hs * HTTPServer ) DeletePlaylist (c * models.ReqContext ) response.Response {
133
136
uid := web .Params (c .Req )[":uid" ]
134
137
135
- cmd := models .DeletePlaylistCommand {UID : uid , OrgId : c .OrgId }
136
- if err := hs .SQLStore . DeletePlaylist (c .Req .Context (), & cmd ); err != nil {
138
+ cmd := playlist .DeletePlaylistCommand {UID : uid , OrgId : c .OrgId }
139
+ if err := hs .playlistService . Delete (c .Req .Context (), & cmd ); err != nil {
137
140
return response .Error (500 , "Failed to delete playlist" , err )
138
141
}
139
142
140
143
return response .JSON (http .StatusOK , "" )
141
144
}
142
145
143
146
func (hs * HTTPServer ) CreatePlaylist (c * models.ReqContext ) response.Response {
144
- cmd := models .CreatePlaylistCommand {}
147
+ cmd := playlist .CreatePlaylistCommand {}
145
148
if err := web .Bind (c .Req , & cmd ); err != nil {
146
149
return response .Error (http .StatusBadRequest , "bad request data" , err )
147
150
}
148
151
cmd .OrgId = c .OrgId
149
152
150
- if err := hs .SQLStore .CreatePlaylist (c .Req .Context (), & cmd ); err != nil {
153
+ p , err := hs .playlistService .Create (c .Req .Context (), & cmd )
154
+ if err != nil {
151
155
return response .Error (500 , "Failed to create playlist" , err )
152
156
}
153
157
154
- return response .JSON (http .StatusOK , cmd . Result )
158
+ return response .JSON (http .StatusOK , p )
155
159
}
156
160
157
161
func (hs * HTTPServer ) UpdatePlaylist (c * models.ReqContext ) response.Response {
158
- cmd := models .UpdatePlaylistCommand {}
162
+ cmd := playlist .UpdatePlaylistCommand {}
159
163
if err := web .Bind (c .Req , & cmd ); err != nil {
160
164
return response .Error (http .StatusBadRequest , "bad request data" , err )
161
165
}
162
166
cmd .OrgId = c .OrgId
163
167
cmd .UID = web .Params (c .Req )[":uid" ]
164
168
165
- if err := hs .SQLStore .UpdatePlaylist (c .Req .Context (), & cmd ); err != nil {
169
+ p , err := hs .playlistService .Update (c .Req .Context (), & cmd )
170
+ if err != nil {
166
171
return response .Error (500 , "Failed to save playlist" , err )
167
172
}
168
173
@@ -171,6 +176,6 @@ func (hs *HTTPServer) UpdatePlaylist(c *models.ReqContext) response.Response {
171
176
return response .Error (500 , "Failed to save playlist" , err )
172
177
}
173
178
174
- cmd . Result .Items = playlistDTOs
175
- return response .JSON (http .StatusOK , cmd . Result )
179
+ p .Items = playlistDTOs
180
+ return response .JSON (http .StatusOK , p )
176
181
}
0 commit comments