Exemple #1
0
        public async Task <IActionResult> CreatePlaylist([FromQuery] string token,
                                                         [FromBody] CreatePlaylistModel data)
        {
            var api = new SpotifyWebAPI
            {
                AccessToken = token,
                TokenType   = "Bearer",
            };

            var headers = new Dictionary <string, string> {
                { "Authorization", "Bearer " + token }
            };

            var me = await api.WebClient.DownloadAsync("https://api.spotify.com/v1/me", headers);

            var meJson = JToken.Parse(me.Item2);

            var userId = meJson["id"].Value <string>();

            var playlistName = data.PlaylistName
                               ?? "Spot " + DateTime.UtcNow.ToString("o");

            var playlist = await api.CreatePlaylistAsync(userId, playlistName, isPublic : false);

            var trackUris             = data.TrackIds.Select(x => "spotify:track:" + x).ToList();
            var addPlaylistTracksResp = await api.AddPlaylistTracksAsync(playlist.Id, trackUris);

            return(Ok(playlist.Id));
        }
Exemple #2
0
        public FullPlaylist CreateAPlaylist(CreatePlaylistModel model)
        {
            Token          token   = GetToken();
            PrivateProfile profile = GetMe(token);

            if (profile.Id == null && token.RefreshToken != null)
            {
                string oldRefreshToken = token.RefreshToken;
                token = RefreshToken(token.RefreshToken, Constants.ClientSecret);
                token.RefreshToken = oldRefreshToken;
                _tokenService.SetToken(token);
                profile = GetMe(token);
            }

            SpotifyAPI.Web.SpotifyWebAPI api = new SpotifyWebAPI()
            {
                AccessToken = token.AccessToken, TokenType = token.TokenType
            };
            FullPlaylist fullPlaylist = api.CreatePlaylist(profile.Id, model.Name);

            if (fullPlaylist.HasError())
            {
                throw new Exception("Playlist can not be created");
            }

            return(fullPlaylist);
        }
        public IActionResult CreatePlaylist([FromBody] CreatePlaylistModel model)
        {
            int userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(c => c.Type == claimTypes.Id.ToString()).Value);

            var newPlaylistId = db.CreatePlaylist(model.Name, model.Description, userId);

            if (newPlaylistId <= 0)
            {
                return(StatusCode(500, new ErrorDetails()
                {
                    errorId = ErrorList.UnknownError.Id, errorMessage = ErrorList.UnknownError.Description
                }));
            }

            return(Ok(new { response = new { albumId = newPlaylistId } }));
        }
Exemple #4
0
        public async Task <IActionResult> CreatePlaylist(CreatePlaylistModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var playlist = await _service.CreatePlaylist(model);

                    SavePlaylistId(playlist.Id);

                    return(RedirectToAction(nameof(Playlist), new { playlist.Id }));
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }

            return(View(model));
        }
Exemple #5
0
 public Task <Playlist> CreatePlaylist(CreatePlaylistModel model)
 {
     return(Post <Playlist>($"playlists", model));
 }