예제 #1
0
        /// <summary>
        /// Renaming And/Or Consolidating Songs
        /// </summary>
        /// <param name="songIDs"></param>
        /// <param name="newTitle"></param>
        public void UpdateSongTitle(IEnumerable <Song> songs, string newTitle)
        {
            List <Song> songsCopy = new List <Song>(songs.Distinct());

            //Renaming (or Consolidating) a Song
            Song matchingSong = songsCopy.SelectMany(x => x.Recordings)
                                .SelectMany(x => x.Artist.Recordings)
                                .Select(x => x.Song)
                                .Distinct()
                                .Where(x => x.Title == newTitle)
                                .FirstOrDefault();

            if (matchingSong == null)
            {
                //Sort on Guid Timestamp so that the merged result uses the oldest Guid
                songsCopy.Sort((x, y) => x.SongGuidTimestamp.CompareTo(y.SongGuidTimestamp));

                matchingSong = songsCopy[0];
                songsCopy.RemoveAt(0);

                matchingSong.Title = newTitle;
            }

            if (songsCopy.Count > 0)
            {
                //New song did exist, or we passed in more than one song

                //Update track table to point at new song
                foreach (Recording recording in
                         (from song in songsCopy
                          join recording in db.Recordings on song.Id equals recording.SongId
                          select recording))
                {
                    recording.Song = matchingSong;
                }

                foreach (PlaylistSong playlistSong in
                         (from song in songsCopy
                          join plsong in db.PlaylistSongs on song.Id equals plsong.SongId
                          select plsong))
                {
                    playlistSong.Song = matchingSong;
                }

                db.Songs.RemoveRange(songsCopy);
            }

            db.SaveChanges();
        }
예제 #2
0
        public void UpdateAlbumTitle(IEnumerable <Album> albums, string newAlbumTitle)
        {
            List <Album> albumsCopy = new List <Album>(albums);

            //First, find out if the new album exists in a limited pool
            Album matchingAlbum = albumsCopy.SelectMany(x => x.Recordings)
                                  .Select(x => x.Artist).Distinct()
                                  .SelectMany(x => x.Recordings)
                                  .Select(x => x.Album).Distinct()
                                  .Where(x => x.Title == newAlbumTitle).FirstOrDefault();

            if (matchingAlbum == null)
            {
                //Update an Album's name, because it doesn't exist

                //Sort on Guid Timestamp so that the merged result uses the oldest Guid
                albumsCopy.Sort((x, y) => x.AlbumGuidTimestamp.CompareTo(y.AlbumGuidTimestamp));

                //Pop off the front
                matchingAlbum = albumsCopy[0];
                albumsCopy.RemoveAt(0);

                //Update the album formerly in the front
                matchingAlbum.Title = newAlbumTitle;
            }

            if (albumsCopy.Count > 0)
            {
                foreach (Recording recording in
                         (from album in albumsCopy
                          join recording in db.Recordings on album.Id equals recording.AlbumId
                          select recording))
                {
                    recording.Album = matchingAlbum;
                }

                //Delete orphans
                db.Albums.RemoveRange(albumsCopy);
            }

            db.SaveChanges();
        }
예제 #3
0
        public void UpdateArtistName(IEnumerable <Artist> artists, string newArtistName)
        {
            List <Artist> artistsCopy = new List <Artist>(artists);

            //First, find out if the new artist exists
            Artist matchingArtist = db.Artists
                                    .Where(x => x.Name == newArtistName)
                                    .FirstOrDefault();

            if (matchingArtist == null)
            {
                //Update an Artist's name, because it doesn't exist

                //Sort on Guid Timestamp so that the merged result uses the oldest Guid
                artistsCopy.Sort((x, y) => x.ArtistGuidTimestamp.CompareTo(y.ArtistGuidTimestamp));

                //Pop off the front
                matchingArtist = artistsCopy[0];
                artistsCopy.RemoveAt(0);

                matchingArtist.Name = newArtistName;
            }

            if (artistsCopy.Count > 0)
            {
                //For the remaining artists, Remap foreign keys
                foreach (Recording recording in
                         (from artist in artistsCopy
                          join recording in db.Recordings on artist.Id equals recording.ArtistId
                          select recording).Distinct())
                {
                    recording.Artist = matchingArtist;
                }

                //Now, delete any old artists with no remaining recordings
                db.Artists.RemoveRange(artistsCopy);
            }

            db.SaveChanges();
        }
예제 #4
0
        /// <summary>
        /// Splitting, Renaming, And/Or Consolidating Recordings by Song Title
        /// </summary>
        public void UpdateSongTitle(IEnumerable <Recording> recordings, string newTitle)
        {
            //Is there a song currently by the same artist with the same name?
            Song matchingSong = recordings
                                .Select(x => x.Artist)
                                .SelectMany(x => x.Recordings)
                                .Select(x => x.Song)
                                .Distinct()
                                .Where(x => x.Title == newTitle)
                                .FirstOrDefault();

            if (matchingSong == null)
            {
                //New Song did not exist

                //If we are updating the name of every recording of one song,
                //  then just update the name
                if (recordings.Select(x => x.Song).Distinct().Count() == 1)
                {
                    Song sourceSong = recordings.First().Song;

                    //If the recordings exactly match artist
                    if (sourceSong.Recordings.Except(recordings.Distinct()).Count() == 0)
                    {
                        sourceSong.Title = newTitle;

                        db.SaveChanges();

                        //We are done - bail
                        return;
                    }
                }

                matchingSong = new Song()
                {
                    Title             = newTitle,
                    Weight            = -1.0,
                    SongGuid          = Guid.NewGuid(),
                    SongGuidTimestamp = Epoch.Time
                };

                db.Songs.Add(matchingSong);
            }

            //Update recordings
            foreach (Recording recording in recordings)
            {
                recording.Song = matchingSong;
            }

            //Update playlists
            foreach (PlaylistSong playlistSong in
                     (from recording in recordings
                      join plRec in db.PlaylistRecordings on recording.Id equals plRec.RecordingId
                      select plRec.PlaylistSong)
                     .Distinct())
            {
                playlistSong.Song = matchingSong;
            }

            //Remove leafs
            db.Songs.RemoveRange(
                from song in db.Songs.Local
                where song.Recordings.Count == 0
                select song);

            db.SaveChanges();
        }