/
index.go
88 lines (79 loc) · 1.92 KB
/
index.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
package controllers
import (
"database/sql"
"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"
"github.com/zmb3/spotify"
)
// Index controller
type Index struct {
Controller
}
// NewIndex creates a new Index controller
func NewIndex() Index {
newIndex := Index{}
newIndex.Setup()
return newIndex
}
// Get the index page view
func (cr *Index) Get() gin.HandlerFunc {
return func(c *gin.Context) {
sesh := session.DefaultSession(c)
flashedError, err := sesh.Error()
if err != nil {
c.Error(e.Internal.CausedBy(err))
}
if session.IsLoggedIn(c) {
currentUser := session.CurrentUser(c)
var spotifyUser spotify.PrivateUser
err := currentUser.SpotifyUser.Unmarshal(&spotifyUser)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
spotifyClient, err := cr.ClientFromSession(c)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
currentParty, err := currentUser.PartyG().One()
if err != nil {
if err != sql.ErrNoRows {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
}
var currentPlaylist *models.Playlist
playlists, err := s.Playlists(currentUser.Username, *spotifyClient)
if err != nil {
c.Error(e.Internal.CausedBy(err))
c.Abort()
return
}
for _, playlist := range playlists {
if currentUser.SpotifyPlaylistID.String == playlist.ID {
currentPlaylist = &playlist
break
}
}
c.HTML(http.StatusOK, "index.html", gin.H{
"user": spotifyUser,
"currentParty": currentParty,
"currentPlaylist": currentPlaylist,
"playlists": playlists,
"error": flashedError,
})
} else {
c.HTML(http.StatusOK, "index.html", gin.H{
"error": flashedError,
})
}
}
}