/
list_playlists.go
executable file
·55 lines (50 loc) · 1.07 KB
/
list_playlists.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
package mongo
import (
"context"
"github.com/pkg/errors"
"github.com/thavlik/t4vd/sources/pkg/api"
)
func (s *mongoStore) ListPlaylists(
ctx context.Context,
projectID string,
) ([]*api.Playlist, error) {
cursor, err := s.playlists.Find(
ctx,
map[string]interface{}{
"project": projectID,
},
)
if err != nil {
return nil, errors.Wrap(err, "mongo")
}
var docs []map[string]interface{}
if err := cursor.All(ctx, &docs); err != nil {
return nil, errors.Wrap(err, "mongo")
}
playlists := make([]*api.Playlist, len(docs))
for i, doc := range docs {
playlists[i] = convertPlaylistDoc(doc)
}
return playlists, nil
}
func (s *mongoStore) ListPlaylistIDs(
ctx context.Context,
projectID string,
blacklist bool,
) ([]string, error) {
return getFieldFromDocs(
ctx,
projectID,
s.playlists,
"p",
blacklist,
)
}
func convertPlaylistDoc(m map[string]interface{}) *api.Playlist {
return &api.Playlist{
ID: m["p"].(string),
Blacklist: m["blacklist"].(bool),
SubmitterID: m["submitter"].(string),
Submitted: m["submitted"].(int64),
}
}