/// <summary> /// Metodo encargado de buscarcanciones en una carpeta para su importaci'on en la biblioteca de usuario /// </summary> /// <param name="directoryPath">Ruta de acceso a directorio</param> public void ImportSongsToLibrary(String directoryPath) { List<TrackInfo> tracksToInsert = new List<TrackInfo>(); SessionManager sessionManager= SessionManager.Instance; string unknownFrame = "Unknown"; string[] musicFiles = Directory.GetFiles(directoryPath, "*.mp3"); foreach (string musicFile in musicFiles) { UserTrackRepository userTrackRepository = new UserTrackRepository(); TrackRepository trackRepository = new TrackRepository(); using (var mp3 = new Mp3File(musicFile)) { TrackInfo currentTrack = new TrackInfo() {TrackId = Guid.NewGuid()}; if (mp3.HasTags) { Id3Tag tag = mp3.GetTag(Id3TagFamily.FileStartTag); if (tag != null) { currentTrack.Title = tag.Title.IsAssigned ? tag.Title.Value : unknownFrame; currentTrack.AlbumTitle = tag.Album.IsAssigned ? tag.Album.Value : unknownFrame; currentTrack.ArtistTitle = tag.Artists.IsAssigned ? tag.Artists : unknownFrame; currentTrack.Genre = getGenre(tag.Genre.Value); if (tag.Lyrics.Count != 0) { currentTrack.Lyric = tag.Lyrics[0].IsAssigned ? tag.Lyrics[0].Lyrics : ""; } else { currentTrack.Lyric = ""; } if (tag.Year.IsAssigned) { int year; if (Int32.TryParse(tag.Year.Value, out year)) { currentTrack.Year = year; } } } else { currentTrack.Title = unknownFrame; currentTrack.AlbumTitle =unknownFrame; currentTrack.ArtistTitle = unknownFrame; currentTrack.Genre = unknownFrame; currentTrack.Lyric = ""; currentTrack.Year = 0; } currentTrack.SongPath = musicFile; currentTrack.TrackId = Guid.NewGuid(); tracksToInsert.Add(currentTrack); } else { currentTrack.Title =unknownFrame; currentTrack.AlbumTitle = unknownFrame; currentTrack.ArtistTitle =unknownFrame; currentTrack.Genre = unknownFrame; currentTrack.Lyric = ""; currentTrack.SongPath = musicFile; currentTrack.TrackId= Guid.NewGuid(); currentTrack.Year = 0; tracksToInsert.Add(currentTrack); } } } this.userTracks.AddRange(tracksToInsert); this.InsertIntoDatabase(tracksToInsert); }
/// <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(); }