public static async Task <Album> IndexNewAlbumAsync( string albumIdentifier, IRepositoryManager repositoryManager, IMapper mapper, SpotifyAPICredentials spotifyAPICredentials) { try { var spotifyAlbumDownloadModel = await FetchAlbumForIndexAsync(albumIdentifier, spotifyAPICredentials).ConfigureAwait(false); var artists = await ArtistModel.CreateOrFetchArtistAsync(spotifyAlbumDownloadModel.Artists, repositoryManager, mapper); var albumArtists = artists.Select(a => a.Id); var releaseDate = ReleaseDateParsingHelper.ParseReleaseDateToDateTime( spotifyAlbumDownloadModel.Album.ReleaseDate, spotifyAlbumDownloadModel.Album.ReleaseDatePrecision); var newAlbum = new Album { SpotifyId = spotifyAlbumDownloadModel.Album.Id, Name = spotifyAlbumDownloadModel.Album.Name, ReleaseDate = releaseDate, Type = (byte)MapAlbumTypeToEnum(spotifyAlbumDownloadModel.Album.Type), CreatedOn = DateTime.Now, LastUpdatedOn = DateTime.Now }; newAlbum.AlbumArtists = MapArtistsToAlbumArtists(albumArtists); newAlbum.AlbumImages = mapper.Map <List <AlbumImage> >(spotifyAlbumDownloadModel.Album.Images); await repositoryManager.AlbumRepository.AddAsync(newAlbum); /*var newAlbumTracks = await TrackModel.IndexNewAlbumTracksAsync( * newAlbum.Id, * spotifyAlbumDownloadModel.Album, * artists, * repositoryManager, * mapper, * spotifyAPICredentials); * * await repositoryManager.TrackRepository.AddRangeAsync(newAlbumTracks);*/ return(newAlbum); } catch (Exception) { throw; } }
public static async Task <AlbumResponseModel> FetchRandomAlbumAsync(SpotifyAPICredentials spotifyAPICredentials, string genre = "") { var randomArtist = await ArtistModel.FetchRandomArtistAsync(spotifyAPICredentials, genre); var randomArtistAlbums = await FetchArtistAlbumsAsync(randomArtist.Id, spotifyAPICredentials).ConfigureAwait(false); if (randomArtistAlbums.Count > 0) { randomArtistAlbums.Shuffle(); var randomAlbum = GetRandomAlbum(randomArtistAlbums); return(await FetchAlbumAsync(randomAlbum.Id, spotifyAPICredentials).ConfigureAwait(false)); } else { return(await FetchRandomAlbumAsync(spotifyAPICredentials, genre).ConfigureAwait(false)); } }
private static async Task <SpotifyAlbumDownloadModel> FetchAlbumForIndexAsync( string albumIdentifier, SpotifyAPICredentials spotifyAPICredentials) { var spotifyClient = SearchHelper.CreateSpotifyClient(spotifyAPICredentials); var album = await spotifyClient.Albums.Get(albumIdentifier); var artists = await ArtistModel.FetchArtistsAsync(album.Artists.Select(a => a.Id).ToList(), spotifyAPICredentials); var spotifyAlbumDownloadModel = new SpotifyAlbumDownloadModel { Artists = artists, Album = album }; return(spotifyAlbumDownloadModel); }
private static async Task <List <TrackArtist> > MapTrackArtistsAsync( SimpleTrack track, HashSet <string> albumArtistsSpotifyIds, IEnumerable <Artist> albumArtists, IRepositoryManager repositoryManager, IMapper mapper, SpotifyAPICredentials spotifyAPICredentials) { List <TrackArtist> trackArtists = new List <TrackArtist>(); List <string> trackArtistsToBeCreated = new List <string>(); var trackArtistSpotifyIds = track.Artists.Select(a => a.Id).ToHashSet(); foreach (var trackArtist in track.Artists) { if (albumArtistsSpotifyIds.Contains(trackArtist.Id)) { var albumArtist = albumArtists.Where(a => a.SpotifyId == trackArtist.Id).FirstOrDefault(); trackArtists.Add(new TrackArtist { ArtistId = albumArtist.Id, CreatedOn = DateTime.Now, LastUpdatedOn = DateTime.Now }); } else { trackArtistsToBeCreated.Add(trackArtist.Id); } } if (trackArtistsToBeCreated.Count > 0) { var newArtists = await ArtistModel.FetchArtistsAsync(trackArtistsToBeCreated, spotifyAPICredentials); var artists = await ArtistModel.CreateOrFetchArtistAsync(newArtists, repositoryManager, mapper); trackArtists.AddRange(MapArtistsToTrackArtists(artists.Select(a => a.Id).ToList())); } return(trackArtists); }