Example #1
0
 protected void AddArtist(Artist artist)
 {
     _artists.Add(artist);
     ArtistsHash = ComputeArtistsHash(_artists);
 }
Example #2
0
        private Album AddNonTrackEntitiesAndReturnAlbum(TagLib.File file, ISession session)
        {
            var artistsToAdd = file.Tag.AlbumArtists
                    .Union(file.Tag.Performers)
                    .Select(s => s.TrimEnd('\0'))
                    .ToArray()
                    .Except(from artist in session.Query<Artist>()
                            select artist.Name
                        , StringComparer.Ordinal)
                        .ToList();

            if (artistsToAdd.Any())
            {
                foreach (string artistName in artistsToAdd)
                {
                    Artist newArtist = new Artist
                    {
                        Name = artistName
                    };

                    session.Save(newArtist);
                }
            }

            // Add album if necessary
            var tagAlbumArtists = file.Tag.AlbumArtists.Select(s => s.TrimEnd('\0')).ToArray();

            var albumArtistsQueryable =
                (from artist in session.Query<Artist>()
                 where tagAlbumArtists.Contains(artist.Name)
                 select artist)
                 .ToList();

            var albumArtists = new HashedSet<Artist>(albumArtistsQueryable);
            string albumArtistsHash = Album.ComputeArtistsHash(albumArtists);

            // matchingAlbumsInDatabase.Count() should be 0 or 1
            var tagAlbum = file.Tag.Album.TrimEnd('\0');

            var matchingAlbumsInDatabase =
                (from album in session.Query<Album>()
                 where album.Title == tagAlbum
                 && album.ArtistsHash == albumArtistsHash
                 select album);

            Album trackAlbum;
            if (!matchingAlbumsInDatabase.Any())
            {
                trackAlbum = new Album
                    {
                        Title = tagAlbum,
                        Artists = albumArtists
                    };

                session.Save(trackAlbum);
            }
            else
            {
                trackAlbum = matchingAlbumsInDatabase.First();
            }

            // Add genres if necessary
            var genreNamesToAdd = file.Tag.Genres.Select(s => s.TrimEnd('\0')).Except(
                from genre in session.Query<Genre>()
                select genre.Name, StringComparer.Ordinal);

            foreach (string genreName in genreNamesToAdd)
            {
                Genre genre = new Genre
                {
                    Name = genreName
                };

                session.Save(genre);
            }

            return trackAlbum;
        }