/// <summary> /// Metodo encargado de inicializar la biblioteca, carga los datos de las canciones desde la base de datos /// </summary> public void InitializeLibrary() { FileManager fileManager =new FileManager(); fileManager.CreateUserDirectory(); SessionManager sessionManager = SessionManager.Instance; TrackRepository trackRepo = new TrackRepository(); UserTrackRepository userTrackRepo =new UserTrackRepository(); List<Track> userStoredTracks= trackRepo.GetTraksByUserId(sessionManager.UserId); foreach (var userTrack in userStoredTracks) { TrackInfo trackInfo =new TrackInfo() {Title = userTrack.Title,TrackId = userTrack.TrackID,AlbumTitle = userTrack.Album.Title, ArtistTitle = userTrack.Album.Artist.Title,SongPath = userTrack.Path,Year = userTrack.Album.ReleaseYear, Lyric = userTrack.Lyrics,Genre = userTrack.Genre}; trackInfo.isSynced = userTrackRepo.GetUserTrackByPK(sessionManager.UserId,trackInfo.TrackId).IsSync; this.userTracks.Add(trackInfo); } trackRepo.Dispose(); userTrackRepo.Dispose(); }
/// <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(); }