PageRenderTime 32ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/src/GMusic.Core/CachingLayer/MusicUrlManager.cs

https://github.com/0xdeafcafe/gmusic_wp
C# | 164 lines | 143 code | 19 blank | 2 comment | 18 complexity | 9da0d583afa7981eca6cda81d8659aca MD5 | raw file
  1#if WINDOWS_PHONE
  2using System;
  3using System.Collections.Generic;
  4using System.ComponentModel;
  5using System.Data.Common;
  6using System.Data.Linq;
  7using System.Data.Linq.Mapping;
  8using System.Linq;
  9using Microsoft.Phone.Data.Linq;
 10
 11namespace MVPTracker.Core.CachingLayer
 12{
 13    public class MusicUrlManager : DataContext
 14    {
 15        public string ConnectionString { get; private set; }
 16        public Table<UrlItem> StorageCache;
 17
 18        public MusicUrlManager(string connectionString)
 19            : base(connectionString)
 20        {
 21            ConnectionString = connectionString;
 22
 23            if (!DatabaseExists())
 24                CreateDatabase();
 25
 26            // Verify Tables
 27            VerifyTable<UrlItem>();
 28        }
 29        public Table<TEntity> VerifyTable<TEntity>() where TEntity : class
 30        {
 31            var table = GetTable<TEntity>();
 32            try
 33            {
 34                // can call any function against the table to verify it exists
 35                table.Any();
 36            }
 37            catch (DbException exception)
 38            {
 39                if (exception.Message.StartsWith("The specified table does not exist."))
 40                {
 41                    var databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater();
 42                    databaseSchemaUpdater.AddTable<TEntity>();
 43                    databaseSchemaUpdater.Execute();
 44                }
 45                else
 46                {
 47                    throw;
 48                }
 49            }
 50            return table;
 51        }
 52
 53        public string GetCacheData(string songId)
 54        {
 55            var entry = StorageCache.First(item => item.SongId == songId && item.Expires < DateTime.Now);
 56
 57            return entry == null ? null : entry.Url;
 58        }
 59        public void SetCacheData(string songId, string url, DateTime expires)
 60        {
 61            #region Read And Delete All Entries
 62            var cachedDataInDb = StorageCache.Where(t => t.SongId == songId).ToList();
 63            var cachedDataData = new List<UrlItem>();
 64            try { cachedDataData = new List<UrlItem>(cachedDataInDb); } catch (Exception) { }
 65            StorageCache.DeleteAllOnSubmit(cachedDataData);
 66            #endregion
 67
 68            #region Add New Data
 69            StorageCache.InsertOnSubmit(new UrlItem
 70                                            {
 71                                                Expires = expires,
 72                                                SongId = songId,
 73                                                Url = url
 74                                            });
 75            #endregion
 76
 77            Save();
 78        }
 79
 80        public void Save()
 81        {
 82            SubmitChanges();
 83        }
 84    }
 85
 86    [Table]
 87    public class UrlItem : INotifyPropertyChanged, INotifyPropertyChanging
 88    {
 89        private int _id;
 90        [Column(Name = "Id", IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
 91        public int Id
 92        {
 93            get { return _id; }
 94            set
 95            {
 96                if (_id == value) return;
 97
 98                NotifyPropertyChanging("Id");
 99                _id = value;
100                NotifyPropertyChanged("Id");
101            }
102        }
103
104        private string _songId;
105        [Column(Name = "SongId", AutoSync = AutoSync.OnInsert)]
106        public string SongId
107        {
108            get { return _songId; }
109            set
110            {
111                if (_songId == value) return;
112
113                NotifyPropertyChanging("SongId");
114                _songId = value;
115                NotifyPropertyChanged("SongId");
116            }
117        }
118
119        private string _url;
120        [Column(Name = "Url", AutoSync = AutoSync.OnInsert)]
121        public string Url
122        {
123            get { return _url; }
124            set
125            {
126                if (_url == value) return;
127
128                NotifyPropertyChanging("Url");
129                _url = value;
130                NotifyPropertyChanged("Url");
131            }
132        }
133
134        private DateTime _expires;
135        [Column(Name = "Expires", AutoSync = AutoSync.OnInsert)]
136        public DateTime Expires
137        {
138            get { return _expires; }
139            set
140            {
141                if (_expires == value) return;
142
143                NotifyPropertyChanging("Expires");
144                _expires = value;
145                NotifyPropertyChanged("Expires");
146            }
147        }
148
149        public event PropertyChangedEventHandler PropertyChanged;
150        public void NotifyPropertyChanged(String info)
151        {
152            if (PropertyChanged != null)
153                PropertyChanged(this, new PropertyChangedEventArgs(info));
154        }
155
156        public event PropertyChangingEventHandler PropertyChanging;
157        public void NotifyPropertyChanging(String info)
158        {
159            if (PropertyChanging != null)
160                PropertyChanging(this, new PropertyChangingEventArgs(info));
161        }
162    }
163}
164#endif