public static void WithSubmit(Action <MusicDataContext> op)
 {
     using (var db = new MusicDataContext()) {
         op(db);
         db.SubmitChanges();
     }
 }
 public override Task AddPlayCount(string songPath)
 {
     return(TaskEx.Run(() => {
         MusicDataContext.WithSubmit(db => {
             var currentEntry = from PlayFrequency playFreq in db.PlayFrequencies
                                where playFreq.SongPath == songPath
                                select playFreq;
             if (currentEntry.Any())
             {
                 // if the entry already exists
                 foreach (PlayFrequency pf in currentEntry)
                 {
                     pf.PlayCount = pf.PlayCount + 1;
                 }
             }
             else
             {
                 // if it doesn't exist
                 var newEntry = new PlayFrequency()
                 {
                     SongPath = songPath, PlayCount = 1
                 };
                 db.PlayFrequencies.InsertOnSubmit(newEntry);
             }
         });
     }));
 }
Example #3
0
 private static void Add(MusicDataContext db, PlaylistTrack track)
 {
     Add(db, new List <PlaylistTrack>()
     {
         track
     });
 }
Example #4
0
 public static PlaylistInfo TracksAndIndex()
 {
     return(MusicDataContext.WithConnection <PlaylistInfo>(db => {
         var tracks = Tracks(db);
         return new PlaylistInfo(tracks, Index(db));
     }));
 }
Example #5
0
 public static void SetPlaylist(PlaylistTrack track)
 {
     MusicDataContext.WithSubmit(db => {
         Clear(db);
         Add(db, track);
         SetIndex(db, 0);
     });
 }
 public static T WithSubmit <T>(Func <MusicDataContext, T> op)
 {
     using (var db = new MusicDataContext()) {
         var ret = op(db);
         db.SubmitChanges();
         return(ret);
     }
 }
 public static ObservableCollection <PlayFrequency> MostPlayed(int count = 100)
 {
     return(MusicDataContext.WithConnection(db => {
         var mostPlayed = (from PlayFrequency playFreq in db.PlayFrequencies
                           orderby playFreq.PlayCount descending
                           select playFreq).Take(count);
         return new ObservableCollection <PlayFrequency>(mostPlayed);
     }));
 }
 public static void CreateIfNotExists()
 {
     // Creates the database if it does not yet exist.
     using (var db = new MusicDataContext()) {
         if (db.DatabaseExists() == false)
         {
             db.CreateDatabase();
         }
     }
 }
 public override Task <List <string> > LeastPlayed(int count = 20)
 {
     return(TaskEx.Run(() => {
         return MusicDataContext.WithConnection(db => {
             var leastPlayed = (from PlayFrequency playFreq in db.PlayFrequencies
                                orderby playFreq.PlayCount ascending
                                select playFreq.SongPath).Take(count);
             return new List <string>(leastPlayed);
         });
     }));
 }
Example #10
0
 /// <summary>
 /// http://stackoverflow.com/questions/2801500/linq-display-row-numbers
 /// </summary>
 /// <param name="index"></param>
 public static void Delete(int index)
 {
     MusicDataContext.WithSubmit(db => {
         var songs = (from PlaylistItem song in db.Playlist orderby song.ItemId select song)
                     .AsEnumerable();
         if (songs != null && songs.Count() > index)
         {
             var song = songs.ElementAt(index);
             db.Playlist.DeleteOnSubmit(song);
         }
     });
 }
Example #11
0
        private static int Index(MusicDataContext db)
        {
            var entry = (from PlaylistIndex i in db.PlayIndex select i).SingleOrDefault();

            if (entry != null)
            {
                return(entry.Index);
            }
            else
            {
                return(-1);
            }
        }
 public override Task DeleteSongEntry(string songPath)
 {
     return(TaskEx.Run(() => {
         MusicDataContext.WithSubmit(db => {
             var entry = from PlayFrequency playFreq in db.PlayFrequencies
                         where playFreq.SongPath == songPath
                         select playFreq;
             foreach (PlayFrequency pf in entry)
             {
                 db.PlayFrequencies.DeleteOnSubmit(pf);
             }
         });
     }));
 }
Example #13
0
        private static List <PlaylistTrack> Tracks(MusicDataContext db)
        {
            var allSongs = from PlaylistItem song in db.Playlist orderby song.ItemId select song;

            if (allSongs.Any())
            {
                // throws OOM upon "next track" following eom
                return(new List <PlaylistTrack>(allSongs.Select(item2track)));
            }
            else
            {
                return(new List <PlaylistTrack>());
            }
        }
Example #14
0
 public static PlaylistTrack Previous()
 {
     return(MusicDataContext.WithSubmit(db => {
         var index = Index(db);
         var tracks = Tracks(db);
         if (index > 0 && tracks.Count > --index)
         {
             var track = tracks[index];
             SetIndex(db, index);
             return track;
         }
         else
         {
             return null;
         }
     }));
 }
Example #15
0
        private static void SetIndex(MusicDataContext db, int newIndex)
        {
            var entry = (from PlaylistIndex i in db.PlayIndex select i);

            if (entry.Any())
            {
                foreach (PlaylistIndex pi in entry)
                {
                    pi.Index = newIndex;
                }
            }
            else
            {
                var newEntry = new PlaylistIndex()
                {
                    Index = newIndex
                };
                db.PlayIndex.InsertOnSubmit(newEntry);
            }
        }
 public static T WithConnection <T>(Func <MusicDataContext, T> op)
 {
     using (var db = new MusicDataContext()) {
         return(op(db));
     }
 }
Example #17
0
 public static List <PlaylistTrack> Tracks()
 {
     return(MusicDataContext.WithConnection <List <PlaylistTrack> >(db => {
         return Tracks(db);
     }));
 }
Example #18
0
        private static void Clear(MusicDataContext db)
        {
            var allSongs = from PlaylistItem song in db.Playlist select song;

            db.Playlist.DeleteAllOnSubmit(allSongs);
        }
Example #19
0
 public static void AddAll(IEnumerable <PlaylistTrack> tracks)
 {
     MusicDataContext.WithSubmit(db => {
         Add(db, tracks);
     });
 }
Example #20
0
        private static void Add(MusicDataContext db, IEnumerable <PlaylistTrack> tracks)
        {
            var entries = tracks.Select(track2item);

            db.Playlist.InsertAllOnSubmit(entries);
        }
Example #21
0
 public static void Clear()
 {
     MusicDataContext.WithSubmit(db => {
         Clear(db);
     });
 }
Example #22
0
 public static int Index()
 {
     return(MusicDataContext.WithConnection <int>(db => {
         return Index(db);
     }));
 }
Example #23
0
 public static void SetIndex(int newIndex)
 {
     MusicDataContext.WithSubmit(db => {
         SetIndex(db, newIndex);
     });
 }