aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/NotifyMachineDataDownloadCompletedResponse.cs
blob: 6d5769885dea4a33ad28b1520651bf95e266ff02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.DTO;
using Tango.Transport.Web;

namespace Tango.PPC.Common.Web
{
    public class NotifyMachineDataDownloadCompletedResponse : WebResponseMessage
    {
        
    }
}
#fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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);
                item.IsSynchronized = false;
                item.LastUpdated = DateTime.UtcNow;

                db.SaveChanges();
            }
        }

        public T Get<T>(string key)
        {
            return (T)Get(key, null);
        }

        public T Get<T>(string key, T defaultValue)
        {
            return (T)Get(key, (object)defaultValue);
        }

        public object Get(string key)
        {
            return Get(key, null);
        }

        public object Get(string key, object defaultValue)
        {
            return GetItem(key, defaultValue).Value;
        }

        public IDataStoreItem GetItem(string key)
        {
            return GetItem(key, null);
        }

        public IDataStoreItem GetItem(string key, object defaultValue)
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                var item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key);

                if (item == null)
                {
                    if (defaultValue == null)
                    {
                        throw new KeyNotFoundException("The specified data store key was not found.");
                    }
                    else
                    {
                        Put(key, defaultValue);
                        return GetItem(key);
                    }
                }

                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);
            }
        }
    }
}