Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: metadata for token regen #12

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions internal/controllers/v1/playlists/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
Expand Down Expand Up @@ -67,8 +68,10 @@ func (p *PlaylistHandler) CreatePlaylist(c *gin.Context) {
Expiry: token.Expiry.Time,
}

tokenChanged := false
if !oauthToken.Valid() {
oauthToken, err = p.spotifyauth.RefreshToken(c, oauthToken)
tokenChanged = true
if err != nil {
merrors.InternalServer(c, fmt.Sprintf("Couldn't get access token %s", err))
return
Expand All @@ -77,6 +80,7 @@ func (p *PlaylistHandler) CreatePlaylist(c *gin.Context) {
_, err := qtx.UpdateToken(c, database.UpdateTokenParams{
Refresh: []byte(oauthToken.RefreshToken),
Access: []byte(oauthToken.AccessToken),
Expiry: pgtype.Timestamptz{Time: oauthToken.Expiry, Valid: true},
UserUuid: user.UserUUID,
})
if err != nil {
Expand Down Expand Up @@ -111,12 +115,22 @@ func (p *PlaylistHandler) CreatePlaylist(c *gin.Context) {
return
}

c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: "playlist successfully created",
Data: playlist,
StatusCode: http.StatusOK,
})
if tokenChanged == true {
c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: "playlist successfully created",
Data: playlist,
MetaData: oauthToken,
StatusCode: http.StatusOK,
})
} else {
c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: "playlist successfully created",
Data: playlist,
StatusCode: http.StatusOK,
})
}
}

func (p *PlaylistHandler) ListPlaylists(c *gin.Context) {
Expand Down
26 changes: 0 additions & 26 deletions internal/controllers/v1/playlists/models.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package playlists

import "github.com/google/uuid"

type CreatePlaylistReq struct {
Name string `json:"name" binding:"required"`
}
Expand All @@ -18,27 +16,3 @@ type UpdatePlaylistReq struct {
type DeletePlaylistReq struct {
PlaylistUUID string `json:"playlist_uuid" binding:"required,uuid" uri:"id"`
}

type UpdateConfigurationReq struct {
PlaylistUUID uuid.UUID `json:"playlist_uuid" binding:"required"`
Explicit *bool `json:"explicit"`
RequireApproval *bool `json:"require_approval"`
MaxSong int32 `json:"max_song"`
}

type CreatePlaylistSpotifyReq struct {
PlaylistName string `json:"playlist_name" binding:"required"`
IsPublic bool `json:"is_public"`
IsCollaborative bool `json:"is_collaborative"`
Description string `json:"description"`

AccessToken string `json:"access_token" binding:"required"`
// UserID string `json:"user_id"`
}

type CreatePlaylistSpotifyReqBody struct {
PlaylistName string `json:"name"`
IsPublic bool `json:"public"`
IsCollaborative bool `json:"collaborative"`
Description string `json:"description"`
}
59 changes: 44 additions & 15 deletions internal/controllers/v1/songs/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (s *SongHandler) AddSongToPlaylist(c *gin.Context) {
var message string
message = "song rejected"

tokenChanged := false
var oauthToken *oauth2.Token

if req.Option == "accept" {
message = "song successfully added"

Expand All @@ -111,14 +114,15 @@ func (s *SongHandler) AddSongToPlaylist(c *gin.Context) {
return
}

oauthToken := &oauth2.Token{
oauthToken = &oauth2.Token{
AccessToken: string(token.Access),
RefreshToken: string(token.Refresh),
Expiry: token.Expiry.Time,
}

if !oauthToken.Valid() {
oauthToken, err = s.spotifyauth.RefreshToken(c, oauthToken)
tokenChanged = true
if err != nil {
merrors.InternalServer(c, fmt.Sprintf("Couldn't get access token %s", err))
return
Expand All @@ -127,6 +131,7 @@ func (s *SongHandler) AddSongToPlaylist(c *gin.Context) {
_, err := qtx.UpdateToken(c, database.UpdateTokenParams{
Refresh: []byte(oauthToken.RefreshToken),
Access: []byte(oauthToken.AccessToken),
Expiry: pgtype.Timestamptz{Time: oauthToken.Expiry, Valid: true},
UserUuid: user.UserUUID,
})
if err != nil {
Expand Down Expand Up @@ -167,11 +172,20 @@ func (s *SongHandler) AddSongToPlaylist(c *gin.Context) {
return
}

c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: message,
StatusCode: http.StatusOK,
})
if tokenChanged == true {
c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: message,
MetaData: oauthToken,
StatusCode: http.StatusOK,
})
} else {
c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: message,
StatusCode: http.StatusOK,
})
}
}

func (s *SongHandler) GetAllSongs(c *gin.Context) {
Expand Down Expand Up @@ -211,8 +225,10 @@ func (s *SongHandler) GetAllSongs(c *gin.Context) {
Expiry: token.Expiry.Time,
}

tokenChanged := false
if !oauthToken.Valid() {
oauthToken, err = s.spotifyauth.RefreshToken(c, oauthToken)
tokenChanged = true
if err != nil {
merrors.InternalServer(c, fmt.Sprintf("Couldn't get access token %s", err))
return
Expand Down Expand Up @@ -296,15 +312,28 @@ func (s *SongHandler) GetAllSongs(c *gin.Context) {
// playlist_tracks = append(playlist_tracks, new_tracks.Items...)
// }

c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: "Songs successfully retrieved",
Data: gin.H{
"submitted": tracks,
"accepted": playlist_tracks,
},
StatusCode: http.StatusOK,
})
if tokenChanged == true {
c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: "Songs successfully retrieved",
Data: gin.H{
"submitted": tracks,
"accepted": playlist_tracks,
},
MetaData: oauthToken,
StatusCode: http.StatusOK,
})
} else {
c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: "Songs successfully retrieved",
Data: gin.H{
"submitted": tracks,
"accepted": playlist_tracks,
},
StatusCode: http.StatusOK,
})
}
}

func (s *SongHandler) BlacklistSong(c *gin.Context) {
Expand Down