public async Task <IActionResult> SavePlayedTracksAsync() { var playlistTracks = new List <PlaylistTrack>(); if (_spotifyAuthService.GetToken() != null) { var api = new SpotifyWebApi.SpotifyWebApi(_spotifyAuthService.GetToken()); var fullPlaylistTracks = api.Playlist.GetPlaylistTracks(SpotifyUri.Make("1233033915", "3bdydssu6hXzOP4kLrI8cL")).Result; foreach (var track in fullPlaylistTracks) { playlistTracks.Add(new PlaylistTrack { Name = track.Track.Name, Artist = track.Track.Artists.FirstOrDefault().Name, AddedBy = _usernames.GetValueOrDefault(track.AddedBy.Id) }); } } List <PlayedTrackViewModel> playedTrackViewModels = new List <PlayedTrackViewModel>(); var playedTrackEntities = _storageService.GetPlayedTracks().Result; for (var i = 0; i < playedTrackEntities.Count(); i++) { playedTrackViewModels.Add(TrackEntityToPlayedTrackViewModel.Map(playedTrackEntities[i], i, playlistTracks)); } await _storageService.AddPlayedTracksAsync(playedTrackViewModels); return(RedirectToAction("index")); }
public IActionResult Index() { var playlistTracks = new List <PlaylistTrack>(); if (_spotifyAuthService.GetToken() != null) { var api = new SpotifyWebApi.SpotifyWebApi(_spotifyAuthService.GetToken()); var fullPlaylistTracks = api.Playlist.GetPlaylistTracks(SpotifyUri.Make("1233033915", "2CuhODa4xTTlemWopeXG71")).Result; foreach (var track in fullPlaylistTracks) { var usernameModel = _usernames.GetValueOrDefault(track.AddedBy.Id); playlistTracks.Add(new PlaylistTrack { Name = track.Track.Name, Artist = track.Track.Artists.FirstOrDefault().Name, AddedBy = usernameModel?.Name, AddedByImage = usernameModel?.ImageName }); } } List <PlayedTrackViewModel> playedTrackViewModels = new List <PlayedTrackViewModel>(); var playedTrackEntities = _storageService.GetPlayedTracks().Result; if (playedTrackEntities != null) { for (var i = 0; i < playedTrackEntities.Count(); i++) { playedTrackViewModels.Add(TrackEntityToPlayedTrackViewModel.Map(playedTrackEntities[i], i, playlistTracks)); } } ViewBag.PlayedTracks = playedTrackViewModels; return(View()); }