Example #1
0
        /// <summary>
        /// Metodo encargado de interactuar con la capa de datos para realizar las inserciones en la base de datos local
        /// </summary>
        /// <param name="tracksToInsert">Lista de tracks por ingresar</param>
        private void InsertIntoDatabase(List<TrackInfo> tracksToInsert)
         {
            ArtistRepository artistRepo = new ArtistRepository();
            AlbumRepository albumRepo =new AlbumRepository();
            TrackRepository trackRepo =new TrackRepository();
            UserTrackRepository usertracksRepo = new UserTrackRepository();
            foreach (TrackInfo trackInfo in tracksToInsert)
            {
                Artist trackArtist = GetArtistByTitle(trackInfo.ArtistTitle);
                if (trackArtist == null)
                {
                    //Creates new artist and insert into database
                    trackArtist = new Artist() {ArtistID = Guid.NewGuid(), Title = trackInfo.ArtistTitle};
                    artistRepo.Add(trackArtist);
                    artistRepo.SaveChanges();

                }
                else
                {
                    //artistRepo.Attach(trackArtist);
                }
                
                Album trackAlbum = GetAlbumByTitleAndArtistTitle(trackInfo.AlbumTitle,trackArtist.Title);
                if (trackAlbum == null)
                {
                    //Set trackAlbum as new Album
                    trackAlbum= new Album() {AlbumID = Guid.NewGuid(),ArtistID = trackArtist.ArtistID,Title = trackInfo.AlbumTitle, ReleaseYear = trackInfo.Year};
                    albumRepo.Add(trackAlbum);
                    albumRepo.SaveChanges();
                }
                else
                {
                    //albumRepo.Attach(trackAlbum);
                }
                //Creates new track
                Track newTrack=new Track() {AlbumID = trackAlbum.AlbumID,Title = trackInfo.Title, Genre =trackInfo.Genre,Lyrics = trackInfo.Lyric,Path = trackInfo.SongPath,TrackID = trackInfo.TrackId};
                usertracksRepo.Add(new UserTrack() {UserID = SessionManager.Instance.UserId,TrackID = trackRepo.Add(newTrack).TrackID,IsSync = false});
                //artistRepo.SaveChanges();
                
                
                
                trackRepo.SaveChanges();
                
            }
            usertracksRepo.SaveChanges();
            artistRepo.Dispose();
            trackRepo.Dispose();
            usertracksRepo.Dispose();

        }