From 6f0f2a7908884deab8aca33ec967d03c5e564060 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Mon, 16 Nov 2020 18:32:04 +0200 Subject: Working on DataStore WebAPI. Modified data store bytes parsing to Base64. --- .../Controllers/DataStoreController.cs | 85 ++++++++++++++++++++-- 1 file changed, 78 insertions(+), 7 deletions(-) (limited to 'Software/Visual_Studio/Web/Tango.MachineService/Controllers/DataStoreController.cs') 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>(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 finalList = new List(); @@ -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>(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>(ex, HttpStatusCode.InternalServerError, ex.FlattenMessage()); + } } private T ThrowException(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 } -- cgit v1.3.1