From f89f308c525e9deebc6d2cf6416e27dfe1a299dc Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sun, 19 May 2019 16:03:38 +0200 Subject: Cleanup DiscoBot Project --- FireBase/Offline/OfflineDatabase.cs | 71 ++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 40 deletions(-) (limited to 'FireBase/Offline/OfflineDatabase.cs') diff --git a/FireBase/Offline/OfflineDatabase.cs b/FireBase/Offline/OfflineDatabase.cs index 9cebf9c..3e6e7d8 100644 --- a/FireBase/Offline/OfflineDatabase.cs +++ b/FireBase/Offline/OfflineDatabase.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; - using LiteDB; /// @@ -23,21 +22,18 @@ /// Custom string which will get appended to the file name. public OfflineDatabase(Type itemType, string filenameModifier) { - var fullName = this.GetFileName(itemType.ToString()); - if(fullName.Length > 100) - { - fullName = fullName.Substring(0, 100); - } + var fullName = GetFileName(itemType.ToString()); + if (fullName.Length > 100) fullName = fullName.Substring(0, 100); - BsonMapper mapper = BsonMapper.Global; + var mapper = BsonMapper.Global; mapper.Entity().Id(o => o.Key); - string root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - string filename = fullName + filenameModifier + ".db"; + var root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + var filename = fullName + filenameModifier + ".db"; var path = Path.Combine(root, filename); - this.db = new LiteRepository(new LiteDatabase(path, mapper)); + db = new LiteRepository(new LiteDatabase(path, mapper)); - this.cache = db.Database.GetCollection().FindAll() + cache = db.Database.GetCollection().FindAll() .ToDictionary(o => o.Key, o => o); } @@ -45,24 +41,24 @@ /// Gets the number of elements contained in the . /// /// The number of elements contained in the . - public int Count => this.cache.Count; + public int Count => cache.Count; /// /// Gets a value indicating whether this is a read-only collection. /// - public bool IsReadOnly => this.cache.IsReadOnly; + public bool IsReadOnly => cache.IsReadOnly; /// /// Gets an containing the keys of the . /// /// An containing the keys of the object that implements . - public ICollection Keys => this.cache.Keys; + public ICollection Keys => cache.Keys; /// /// Gets an containing the values in the . /// /// An containing the values in the object that implements . - public ICollection Values => this.cache.Values; + public ICollection Values => cache.Values; /// /// Gets or sets the element with the specified key. @@ -71,15 +67,12 @@ /// The element with the specified key. public OfflineEntry this[string key] { - get - { - return this.cache[key]; - } + get => cache[key]; set { - this.cache[key] = value; - this.db.Upsert(value); + cache[key] = value; + db.Upsert(value); } } @@ -89,12 +82,12 @@ /// An enumerator that can be used to iterate through the collection. public IEnumerator> GetEnumerator() { - return this.cache.GetEnumerator(); + return cache.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - return this.GetEnumerator(); + return GetEnumerator(); } /// @@ -103,7 +96,7 @@ /// The object to add to the . public void Add(KeyValuePair item) { - this.Add(item.Key, item.Value); + Add(item.Key, item.Value); } /// @@ -111,8 +104,8 @@ /// public void Clear() { - this.cache.Clear(); - this.db.Delete(Query.All()); + cache.Clear(); + db.Delete(Query.All()); } /// @@ -122,7 +115,7 @@ /// True if is found in the ; otherwise, false. public bool Contains(KeyValuePair item) { - return this.ContainsKey(item.Key); + return ContainsKey(item.Key); } /// @@ -132,7 +125,7 @@ /// The zero-based index in at which copying begins. public void CopyTo(KeyValuePair[] array, int arrayIndex) { - this.cache.CopyTo(array, arrayIndex); + cache.CopyTo(array, arrayIndex); } /// @@ -142,7 +135,7 @@ /// True if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . public bool Remove(KeyValuePair item) { - return this.Remove(item.Key); + return Remove(item.Key); } /// @@ -152,7 +145,7 @@ /// True if the contains an element with the key; otherwise, false. public bool ContainsKey(string key) { - return this.cache.ContainsKey(key); + return cache.ContainsKey(key); } /// @@ -162,8 +155,8 @@ /// The object to use as the value of the element to add. public void Add(string key, OfflineEntry value) { - this.cache.Add(key, value); - this.db.Insert(value); + cache.Add(key, value); + db.Insert(value); } /// @@ -173,8 +166,8 @@ /// True if the element is successfully removed; otherwise, false. This method also returns false if was not found in the original . public bool Remove(string key) { - this.cache.Remove(key); - return this.db.Delete(key); + cache.Remove(key); + return db.Delete(key); } /// @@ -184,18 +177,16 @@ /// True if the object that implements contains an element with the specified key; otherwise, false. public bool TryGetValue(string key, out OfflineEntry value) { - return this.cache.TryGetValue(key, out value); + return cache.TryGetValue(key, out value); } private string GetFileName(string fileName) { - var invalidChars = new[] { '`', '[', ',', '=' }; - foreach(char c in invalidChars.Concat(System.IO.Path.GetInvalidFileNameChars()).Distinct()) - { + var invalidChars = new[] {'`', '[', ',', '='}; + foreach (var c in invalidChars.Concat(Path.GetInvalidFileNameChars()).Distinct()) fileName = fileName.Replace(c, '_'); - } return fileName; } } -} +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf