aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs')
-rw-r--r--Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs85
1 files changed, 78 insertions, 7 deletions
diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs
index 3deab9205..f0dc0f2ba 100644
--- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs
+++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs
@@ -7,7 +7,7 @@ using System.Web.Http;
using Tango.BL.Entities;
using Tango.DataStore;
using Tango.DataStore.EF;
-using Tango.MachineService.DataStore;
+using Tango.DataStore.Web;
using Tango.Web.Helpers;
namespace Tango.MachineService.Controllers
@@ -25,6 +25,8 @@ namespace Tango.MachineService.Controllers
{
try
{
+ ValidateCollectionAndKey(collection, key);
+
using (var db = ObservablesContextHelper.CreateContext())
{
if (sn != null)
@@ -36,7 +38,7 @@ namespace Tango.MachineService.Controllers
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 localItems = db.DataStoreItems.Where(x => !x.IsDeleted).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>();
@@ -68,14 +70,64 @@ namespace Tango.MachineService.Controllers
}
}
- public void Post([FromBody]string value)
+ public void Put([FromBody]DataStoreWebPutItem item)
{
+ try
+ {
+ ValidateCollectionAndKey(item.Collection, item.Key);
- }
+ using (var db = ObservablesContextHelper.CreateContext())
+ {
+ if (item.MachineSerialNumber != null)
+ {
+ var machineGuid = db.Machines.Where(x => x.SerialNumber == item.MachineSerialNumber).Select(x => x.Guid).FirstOrDefault();
- public void Put(int id, [FromBody]string value)
- {
+ if (machineGuid == null)
+ {
+ ThrowException<List<DataStoreWebItem>>(new KeyNotFoundException(), HttpStatusCode.NotFound, "The specified machine serial number could not be found.");
+ }
+
+ DataStoreItem dbItem = db.DataStoreItems.FirstOrDefault(x => x.CollectionName == item.Collection && x.Key == item.Key);
+
+ if (dbItem == null)
+ {
+ dbItem = new DataStoreItem();
+ dbItem.Key = item.Key;
+ dbItem.CollectionName = item.Collection;
+ dbItem.MachineGuid = machineGuid;
+ db.DataStoreItems.Add(dbItem);
+ }
+
+ dbItem.DataType = (int)item.DataType;
+ dbItem.IsDeleted = false;
+ dbItem.IsSynchronized = false;
+ dbItem.LastUpdated = DateTime.UtcNow;
+ dbItem.Value = EFDataStoreHelper.CreateBytes(item.DataType, DataStoreHelper.ParseDataStoreValue(item.DataType, item.Value.ToStringSafe(), item.ProtoMessageType));
+ }
+ else
+ {
+ GlobalDataStoreItem dbItem = db.GlobalDataStoreItems.FirstOrDefault(x => x.CollectionName == item.Collection && x.Key == item.Key);
+
+ if (dbItem == null)
+ {
+ dbItem = new GlobalDataStoreItem();
+ dbItem.Key = item.Key;
+ dbItem.CollectionName = item.Collection;
+ db.GlobalDataStoreItems.Add(dbItem);
+ }
+ dbItem.DataType = (int)item.DataType;
+ dbItem.LastUpdated = DateTime.UtcNow;
+ dbItem.Value = EFDataStoreHelper.CreateBytes(item.DataType, DataStoreHelper.ParseDataStoreValue(item.DataType, item.Value.ToStringSafe(), item.ProtoMessageType));
+ }
+
+ db.SaveChanges();
+ }
+ }
+ catch (Exception ex)
+ {
+ ThrowException<List<DataStoreWebItem>>(ex, HttpStatusCode.InternalServerError, ex.FlattenMessage());
+ }
}
private T ThrowException<T>(Exception ex, HttpStatusCode code, String message = null)
@@ -86,6 +138,26 @@ namespace Tango.MachineService.Controllers
ReasonPhrase = ex.FlattenMessage()
});
}
+
+ private void ValidateCollectionAndKey(String collection = null, String key = null)
+ {
+ if (collection != null)
+ {
+ if (!DataStoreHelper.ValidateCollectionOrKeyName(collection))
+ {
+ throw new ArgumentException("Collection name contains invalid characters.");
+ }
+ }
+
+ if (key != null)
+ {
+ if (!DataStoreHelper.ValidateCollectionOrKeyName(key))
+ {
+ throw new ArgumentException("Item key contains invalid characters.");
+ }
+ }
+ }
+
}
#region Extension Methods
@@ -145,6 +217,5 @@ namespace Tango.MachineService.Controllers
return webItem;
}
}
-
#endregion
}