aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs
blob: 3deab920545e33ee4c83dbe58b95311942029571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Tango.BL.Entities;
using Tango.DataStore;
using Tango.DataStore.EF;
using Tango.MachineService.DataStore;
using Tango.Web.Helpers;

namespace Tango.MachineService.Controllers
{
    public class DataStoreController : ApiController
    {
        private IDataStoreManager _manager;

        public DataStoreController()
        {
            _manager = new EFDataStoreManager();
        }

        public List<DataStoreWebItem> Get(String sn = null, String collection = null, String key = null)
        {
            try
            {
                using (var db = ObservablesContextHelper.CreateContext())
                {
                    if (sn != null)
                    {
                        var machineGuid = db.Machines.Where(x => x.SerialNumber == sn).Select(x => x.Guid).FirstOrDefault();

                        if (machineGuid == null)
                        {
                            return ThrowException<List<DataStoreWebItem>>(new KeyNotFoundException(), HttpStatusCode.NotFound, "The specified machine serial number could not be found.");
                        }

                        var localItems = db.DataStoreItems.Where(x => x.MachineGuid == machineGuid && (collection == null || x.CollectionName == collection) && (key == null || x.Key == key)).ToList();
                        var globalItems = db.GlobalDataStoreItems.Where(x => (collection == null || x.CollectionName == collection) && (key == null || x.Key == key)).ToList();

                        List<DataStoreWebItem> finalList = new List<DataStoreWebItem>();

                        foreach (var localItem in localItems)
                        {
                            var globalItem = globalItems.FirstOrDefault(x => x.CollectionName == localItem.CollectionName && x.Key == localItem.Key);
                            finalList.Add(localItem.ToWebItem(globalItem));
                            globalItems.Remove(globalItem);
                        }

                        finalList.AddRange(globalItems.Select(x => x.ToWebItem()));

                        return finalList;
                    }
                    else
                    {
                        var globalItems = db.GlobalDataStoreItems.Where(x => (collection == null || x.CollectionName == collection) && (key == null || x.Key == key)).ToList();

                        var finalList = globalItems.Select(x => x.ToWebItem()).ToList();

                        return finalList;
                    }
                }
            }
            catch (Exception ex)
            {
                return ThrowException<List<DataStoreWebItem>>(ex, HttpStatusCode.InternalServerError, ex.FlattenMessage());
            }
        }

        public void Post([FromBody]string value)
        {

        }

        public void Put(int id, [FromBody]string value)
        {

        }

        private T ThrowException<T>(Exception ex, HttpStatusCode code, String message = null)
        {
            throw new HttpResponseException(new HttpResponseMessage(code)
            {
                Content = new StringContent(message != null ? message : ex.Message),
                ReasonPhrase = ex.FlattenMessage()
            });
        }
    }

    #region Extension Methods

    public static class IDataStoreExtensions
    {
        public static DataStoreWebItem ToWebItem(this DataStoreItem item, GlobalDataStoreItem globalItem = null)
        {
            IDataStoreItem dsItem = item.ToDataStoreItem();
            DataStoreWebItem webItem = new DataStoreWebItem();
            webItem.Collection = item.CollectionName;
            webItem.Type = globalItem != null ? DataStoreWebItemType.Overrides : DataStoreWebItemType.Local;
            webItem.DataType = dsItem.Type;
            webItem.Date = dsItem.Date;
            webItem.Key = dsItem.Key;
            webItem.LocalValue = dsItem.Value;

            if (webItem.LocalValue is DataStoreProtoObject protoObject)
            {
                webItem.LocalValue = protoObject.Message;
                webItem.ProtoMessageType = protoObject.MessageType;
            }

            if (globalItem != null)
            {
                var dsGlobalItem = globalItem.ToDataStoreItem();

                webItem.GlobalValue = dsGlobalItem.Value;

                if (webItem.GlobalValue is DataStoreProtoObject protoObjectGlobal)
                {
                    webItem.GlobalValue = protoObjectGlobal.Message;
                    webItem.ProtoMessageType = protoObjectGlobal.MessageType;
                }
            }

            return webItem;
        }

        public static DataStoreWebItem ToWebItem(this GlobalDataStoreItem item)
        {
            IDataStoreItem dsItem = item.ToDataStoreItem();
            DataStoreWebItem webItem = new DataStoreWebItem();
            webItem.Collection = item.CollectionName;
            webItem.Type = DataStoreWebItemType.Global;
            webItem.DataType = dsItem.Type;
            webItem.Date = dsItem.Date;
            webItem.Key = dsItem.Key;
            webItem.GlobalValue = dsItem.Value;

            if (webItem.GlobalValue is DataStoreProtoObject protoObject)
            {
                webItem.GlobalValue = protoObject.Message;
                webItem.ProtoMessageType = protoObject.MessageType;
            }

            return webItem;
        }
    }

    #endregion
}