aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.DataStore.EF/EFDataStoreCollection.cs
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2020-10-21 04:06:23 +0300
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2020-10-21 04:06:23 +0300
commite8cec64cf5c35b17fed6a1f8f4ea0665d4ddf39b (patch)
treeb6818e4199eef7cb0b1e183aaf1af08cdba723e9 /Software/Visual_Studio/Tango.DataStore.EF/EFDataStoreCollection.cs
parente54736fabf4338e9f98a7b8aba738180ae57c120 (diff)
downloadTango-e8cec64cf5c35b17fed6a1f8f4ea0665d4ddf39b.tar.gz
Tango-e8cec64cf5c35b17fed6a1f8f4ea0665d4ddf39b.zip
DataStore EF !
Diffstat (limited to 'Software/Visual_Studio/Tango.DataStore.EF/EFDataStoreCollection.cs')
-rw-r--r--Software/Visual_Studio/Tango.DataStore.EF/EFDataStoreCollection.cs117
1 files changed, 117 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.DataStore.EF/EFDataStoreCollection.cs b/Software/Visual_Studio/Tango.DataStore.EF/EFDataStoreCollection.cs
new file mode 100644
index 000000000..769533445
--- /dev/null
+++ b/Software/Visual_Studio/Tango.DataStore.EF/EFDataStoreCollection.cs
@@ -0,0 +1,117 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tango.BL;
+using Tango.BL.Entities;
+
+namespace Tango.DataStore.EF
+{
+ public class EFDataStoreCollection : IDataStoreCollection
+ {
+ public string Name { get; }
+
+ public EFDataStoreCollection(String name)
+ {
+ Name = name;
+ }
+
+ public void Put<T>(string key, T value)
+ {
+ Put(key, (object)value);
+ }
+
+ public void Put(string key, object value)
+ {
+ Put(key, DataStoreHelper.GetDataType(value), value);
+ }
+
+ public void Put(string key, DataType type, object value)
+ {
+ using (var db = ObservablesContext.CreateDefault())
+ {
+ DataStoreItem item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key);
+
+ if (item == null)
+ {
+ item = new DataStoreItem();
+ db.DataStoreItems.Add(item);
+ }
+
+ item.CollectionName = Name;
+ item.Key = key;
+ item.DataType = (int)type;
+ item.Value = EFDataStoreHelper.CreateBytes(type, value);
+
+ db.SaveChanges();
+ }
+ }
+
+ public T Get<T>(string key)
+ {
+ return (T)Get(key);
+ }
+
+ public object Get(string key)
+ {
+ return GetItem(key).Value;
+ }
+
+ public IDataStoreItem GetItem(string key)
+ {
+ using (var db = ObservablesContext.CreateDefault())
+ {
+ var item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key);
+
+ if (item == null)
+ {
+ throw new KeyNotFoundException("The specified data store key was not found.");
+ }
+
+ return item.ToDataStoreItem();
+ }
+ }
+
+ public List<IDataStoreItem> GetAll()
+ {
+ using (var db = ObservablesContext.CreateDefault())
+ {
+ return db.DataStoreItems.Where(x => x.CollectionName == Name).ToList().Select(x => x.ToDataStoreItem()).ToList();
+ }
+ }
+
+ public List<IDataStoreItem> GetUnsynchronized()
+ {
+ using (var db = ObservablesContext.CreateDefault())
+ {
+ return db.DataStoreItems.Where(x => x.CollectionName == Name && !x.IsSynchronized).ToList().Select(x => x.ToDataStoreItem()).ToList();
+ }
+ }
+
+ public void Delete(string key)
+ {
+ using (var db = ObservablesContext.CreateDefault())
+ {
+ db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}' AND KEY = '{key}'");
+ }
+ }
+
+ public void DeleteAll()
+ {
+ using (var db = ObservablesContext.CreateDefault())
+ {
+ db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}'");
+ }
+ }
+
+ public int Count()
+ {
+ using (var db = ObservablesContext.CreateDefault())
+ {
+ return db.DataStoreItems.Count(x => x.CollectionName == Name);
+ }
+ }
+ }
+}