/
playlists.go
119 lines (100 loc) · 2.74 KB
/
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
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
package controllers
import (
"encoding/gob"
"net/http"
e "github.com/chances/party-server/errors"
"github.com/chances/party-server/models"
"github.com/chances/party-server/session"
s "github.com/chances/party-server/spotify"
"github.com/gin-gonic/gin"
"gopkg.in/nullbio/null.v6"
)
// Playlists controller
type Playlists struct {
Controller
}
// NewPlaylists creates a new Playlists controller
func NewPlaylists() Playlists {
gob.Register(models.CachedPlaylist{})
gob.Register(models.Playlists{})
gob.Register(models.Playlist{})
gob.Register([]models.Playlist{})
newPlaylists := Playlists{}
newPlaylists.Setup()
return newPlaylists
}
// Get the user's (host's) current playlist
func (cr *Playlists) Get() gin.HandlerFunc {
return func(c *gin.Context) {
currentUser := session.CurrentUser(c)
if !currentUser.SpotifyPlaylistID.Valid {
c.JSON(http.StatusNotFound, models.EmptyRespose)
return
}
spotifyClient, err := cr.ClientFromSession(c)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
currentPlaylist, err := s.Playlist(currentUser.Username, currentUser.SpotifyPlaylistID.String, *spotifyClient)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
c.JSON(http.StatusOK, models.NewResponse(
currentPlaylist.Playlist.ID, "playlist-full",
cr.RequestURI(c),
currentPlaylist,
))
}
}
// Patch the current playlist via the new playlist's Spotify ID
func (cr *Playlists) Patch() gin.HandlerFunc {
return func(c *gin.Context) {
var patchPlaylist struct {
Data struct {
ID string `json:"id" binding:"required"`
} `json:"data" binding:"required"`
}
if err := c.Bind(&patchPlaylist); err != nil {
c.Error(e.BadRequest.WithDetail("Unexpected request body").CausedBy(err))
c.Abort()
return
}
id := patchPlaylist.Data.ID
currentUser := session.CurrentUser(c)
spotifyClient, err := cr.ClientFromSession(c)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
playlists, err := s.Playlists(currentUser.Username, *spotifyClient)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
for _, playlist := range playlists {
if id == playlist.ID {
currentUser.SpotifyPlaylistID = null.StringFrom(id)
err := currentUser.UpdateG("spotify_playlist_id")
if err != nil {
c.Error(e.Internal.WithDetail("Could not update user").CausedBy(err))
c.Abort()
return
}
// TODO: Update the user's current party somehow? Add the new playlists's tracks or replace the queue?
c.JSON(http.StatusOK, models.NewResponse(
playlist.ID, "playlist",
cr.RequestURI(c),
playlist,
))
return
}
}
c.Error(e.BadRequest.WithDetail("Invalid playlist id"))
}
}