Ejemplo n.º 1
0
        public static async Task <bool> ExportPlaylistAsync(SpotifyTokens token, string quizId)
        {
            bool createResult = false;
            var  quizToExport = JsonConvert.DeserializeObject <Quiz>(await MongoManager.GetOneQuizAsync(quizId, "Quizzes"));
            var  playlistName = quizToExport.Name;

            // Check if it already exists, in that case warn user? Replace all songs?
            // The way this works now is that a new playlist is created with the same name if it already exists.
            //var listOfPlaylists = await SpotifyManager.GetAllUserPlaylists(token);
            //bool playlistExists = false;
            //foreach (var item in listOfPlaylists.Items)
            //{
            //    if (item.Name == playlistName)
            //    {
            //        playlistExists = true;
            //        break;
            //    }
            //}
            if (true /*!playlistExists*/)
            {
                // Create a new spotifyPlaylist with the chosen name if it does not exist.
                // In SpotifyManager.
                var newPlaylistId = await SpotifyManager.CreateNewPlaylistAsync(token, playlistName);

                // Add tracks to the newly created playlist.
                // In SpotifyManager.
                StringBuilder tracks = new StringBuilder();
                foreach (var song in quizToExport.Songs)
                {
                    tracks.Append("spotify:track:" + song.SpotifyReferenceID);
                    if (song != quizToExport.Songs[quizToExport.Songs.Count - 1])
                    {
                        tracks.Append(",");
                    }
                }
                var tracksAddResult = SpotifyManager.AddTracksToPlaylist(token, newPlaylistId, tracks.ToString());
                if (tracksAddResult == "Created")
                {
                    createResult = true;
                    await MongoManager.UpdateSpotifyPlaylistRefAsync(quizId, newPlaylistId);
                }
            }
            return(createResult);
        }