Exemplo n.º 1
0
 /// <summary>
 ///     Uploading song list to the DB
 /// </summary>
 /// <param name="list">Song list to upload</param>
 public void LoadToDb(IEnumerable<Song> list)
 {
     using (var context = new Context())
     {
         foreach (var song in list)
         {
             context.Artists.AddOrUpdate(a => a.ArtistName, new Artist
             {
                 ArtistName = song.ArtistName
             });
             context.SaveChanges();
             context.Albums.AddOrUpdate(a => a.AlbumName,
                 new Album
                 {
                     AlbumName = song.Album
                 });
             context.SaveChanges();
             context.Tracks.AddOrUpdate(t => t.PathOnUserPc, new Track
             {
                 TrackName = song.Title,
                 Duration = song.Duration,
                 PathOnUserPc = song.Path,
                 Album = context.Albums.SingleOrDefault(a => a.AlbumName == song.Album),
                 Artist = context.Artists.FirstOrDefault(a => a.ArtistName == song.ArtistName)
             });
             context.SaveChanges();
         }
     }
     DBChanged?.Invoke();
 }
Exemplo n.º 2
0
 /// <summary>
 ///     Song information replacement
 /// </summary>
 /// <param name="song">Song to replace</param>
 public void ReplaceSong(Song song)
 {
     using (var context = new Context())
     {
         context.Tracks.Remove(context.Tracks.SingleOrDefault(t => t.PathOnUserPc == song.Path));
         context.SaveChanges();
         context.Artists.AddOrUpdate(a => a.ArtistName, new Artist
         {
             ArtistName = song.ArtistName
         });
         context.SaveChanges();
         context.Albums.AddOrUpdate(a => a.AlbumName,
             new Album
             {
                 AlbumName = song.Album
             });
         context.SaveChanges();
         context.Tracks.AddOrUpdate(t => t.PathOnUserPc,
             new Track
             {
                 TrackName = song.Title,
                 Duration = song.Duration,
                 PathOnUserPc = song.Path,
                 Album = context.Albums.SingleOrDefault(a => a.AlbumName == song.Album),
                 Artist = context.Artists.FirstOrDefault(a => a.ArtistName == song.ArtistName)
             });
         context.SaveChanges();
         DBChanged?.Invoke();
     }
 }
Exemplo n.º 3
0
 /// <summary>
 ///     Deleting song
 /// </summary>
 /// <param name="obj">Song to delete</param>
 public void RemoveSongFromDb(Song obj)
 {
     using (var context = new Context())
     {
         var removed = context.Tracks.FirstOrDefault(t => t.PathOnUserPc == obj.Path);
         context.Tracks.Remove(removed);
         context.SaveChanges();
         DBChanged?.Invoke();
     }
 }
Exemplo n.º 4
0
 /// <summary>
 ///     Selecting statements from DB
 /// </summary>
 /// <returns>List that contains all of the songs placed in DB</returns>
 public async Task<List<Song>> GetSongsFromDbAsync()
 {
     using (var context = new Context())
     {
         return
             await
                 context.Tracks.Select(
                     track => new Song
                     {
                         Album = track.Album.AlbumName,
                         Path = track.PathOnUserPc,
                         Duration = track.Duration,
                         ArtistName = track.Artist.ArtistName,
                         Title = track.TrackName
                     }).ToListAsync();
     }
 }