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 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>(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 finalList = new List(); 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>(ex, HttpStatusCode.InternalServerError, ex.FlattenMessage()); } } public void Post([FromBody]string value) { } public void Put(int id, [FromBody]string value) { } private T ThrowException(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 }