/
handlers_playlist.go
172 lines (154 loc) · 4.5 KB
/
handlers_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
162
163
164
165
166
167
168
169
170
171
172
package ctrlsubsonic
import (
"errors"
"log"
"net/http"
"sort"
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/db"
)
func playlistRender(c *Controller, playlist *db.Playlist) *spec.Playlist {
user := &db.User{}
c.DB.Where("id=?", playlist.UserID).Find(user)
resp := &spec.Playlist{
ID: playlist.ID,
Name: playlist.Name,
Comment: playlist.Comment,
Created: playlist.CreatedAt,
SongCount: playlist.TrackCount,
Public: playlist.IsPublic,
Owner: user.Name,
}
trackIDs := playlist.GetItems()
resp.List = make([]*spec.TrackChild, len(trackIDs))
for i, id := range trackIDs {
track := db.Track{}
err := c.DB.
Where("id=?", id).
Preload("Album").
Preload("Album.TagArtist").
Find(&track).
Error
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("wasn't able to find track with id %d", id)
continue
}
resp.List[i] = spec.NewTCTrackByFolder(&track, track.Album)
resp.Duration += track.Length
}
return resp
}
func (c *Controller) ServeGetPlaylists(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
var playlists []*db.Playlist
c.DB.Where("user_id=?", user.ID).Or("is_public=?", true).Find(&playlists)
sub := spec.NewResponse()
sub.Playlists = &spec.Playlists{
List: make([]*spec.Playlist, len(playlists)),
}
for i, playlist := range playlists {
sub.Playlists.List[i] = playlistRender(c, playlist)
}
return sub
}
func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
playlistID, err := params.GetFirstInt("id", "playlistId")
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
playlist := db.Playlist{}
err = c.DB.
Where("id=?", playlistID).
Find(&playlist).
Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return spec.NewError(70, "playlist with id `%d` not found", playlistID)
}
sub := spec.NewResponse()
sub.Playlist = playlistRender(c, &playlist)
return sub
}
func (c *Controller) ServeCreatePlaylist(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
params := r.Context().Value(CtxParams).(params.Params)
playlistID := params.GetFirstOrInt( /* default */ 0, "id", "playlistId")
// playlistID may be 0 from above. in that case we get a new playlist
// as intended
var playlist db.Playlist
c.DB.
Where("id=?", playlistID).
FirstOrCreate(&playlist)
// update meta info
if playlist.UserID != 0 && playlist.UserID != user.ID {
return spec.NewResponse()
}
playlist.UserID = user.ID
if val, err := params.Get("name"); err == nil {
playlist.Name = val
}
// replace song IDs
var trackIDs []int
if p, err := params.GetIDList("songId"); err == nil {
for _, i := range p {
trackIDs = append(trackIDs, i.Value)
}
}
// Set the items of the playlist
playlist.SetItems(trackIDs)
c.DB.Save(playlist)
sub := spec.NewResponse()
sub.Playlist = playlistRender(c, &playlist)
return sub
}
func (c *Controller) ServeUpdatePlaylist(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
params := r.Context().Value(CtxParams).(params.Params)
playlistID := params.GetFirstOrInt( /* default */ 0, "id", "playlistId")
// playlistID may be 0 from above. in that case we get a new playlist
// as intended
var playlist db.Playlist
c.DB.
Where("id=?", playlistID).
FirstOrCreate(&playlist)
// update meta info
if playlist.UserID != 0 && playlist.UserID != user.ID {
return spec.NewResponse()
}
playlist.UserID = user.ID
if val, err := params.Get("name"); err == nil {
playlist.Name = val
}
if val, err := params.Get("comment"); err == nil {
playlist.Comment = val
}
if val, err := params.GetBool("public"); err == nil {
playlist.IsPublic = val
}
trackIDs := playlist.GetItems()
// delete items
if p, err := params.GetIntList("songIndexToRemove"); err == nil {
sort.Sort(sort.Reverse(sort.IntSlice(p)))
for _, i := range p {
trackIDs = append(trackIDs[:i], trackIDs[i+1:]...)
}
}
// add items
if p, err := params.GetIDList("songIdToAdd"); err == nil {
for _, i := range p {
trackIDs = append(trackIDs, i.Value)
}
}
playlist.SetItems(trackIDs)
c.DB.Save(playlist)
return spec.NewResponse()
}
func (c *Controller) ServeDeletePlaylist(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
c.DB.
Where("id=?", params.GetOrInt("id", 0)).
Delete(&db.Playlist{})
return spec.NewResponse()
}