public async Task <string> PlaceData(JObject dataBundle, DynDatabaseRequest request, NodeDataOvewriteMode overwriteMode, bool shouldRespond = true) { var realmDomain = DbService.GetDomainPath(request.DatabaseId); string responseResult = null; // Optionally used to return a result var dataPath = new FlatJsonPath(request.Path); var resultData = string.Empty; await Task.Run(() => { // Parse server values if (dataBundle.Children().Count() == 1 && dataBundle.First is JProperty && (dataBundle.First as JProperty).Name == ServerValueProvider.ServerValueKeyName) { var serverValueToken = ((JProperty)dataBundle.First).Value; var serverValueName = serverValueToken is JValue ? (serverValueToken as JValue).Value as string : null; var resultValue = ServerValueProvider.GetValue(serverValueName); // Now adjust path // Get the last segment, then remove it var lastSegment = dataPath.Segments.Last(); dataPath.Segments.RemoveAt(dataPath.Segments.Count - 1); dataBundle = new JObject(new JProperty(lastSegment, resultValue)); } // Put in the new data switch (overwriteMode) { case NodeDataOvewriteMode.Update: { // Flatten input bundle var flattenedBundle = new FlatJsonObject(dataBundle, dataPath.TokenPrefix); // Merge input bundle foreach (var kvp in flattenedBundle.Dictionary) { DbService.Store.Set(realmDomain, kvp.Key, kvp.Value); } resultData = dataBundle.ToString(); } break; case NodeDataOvewriteMode.Put: { // Flatten input bundle var dataTokenPrefix = dataPath.TokenPrefix; // Get existing data var flattenedBundle = new FlatJsonObject(dataBundle, dataTokenPrefix); var existingData = DbService.Store.GetKeys(realmDomain) .Where(x => x.StartsWith(dataTokenPrefix, StringComparison.Ordinal)); // Remove existing data foreach (var key in existingData) { DbService.Store.Delete(realmDomain, key); } // Add input bundle foreach (var kvp in flattenedBundle.Dictionary) { DbService.Store.Set(realmDomain, kvp.Key, kvp.Value); } resultData = dataBundle.ToString(); } break; case NodeDataOvewriteMode.Push: { // Use the Firebase Push ID algorithm var pushId = PushIdGenerator.GeneratePushId(); // Create flattened bundle with pushId added to prefix dataPath.Segments.Add(pushId); // Flatten input bundle var flattenedBundle = new FlatJsonObject(dataBundle, dataPath.TokenPrefix); // Merge input bundle foreach (var kvp in flattenedBundle.Dictionary) { DbService.Store.Set(realmDomain, kvp.Key, kvp.Value); } var pushResultBundle = new JObject(new JProperty("name", pushId)); resultData = pushResultBundle.ToString(); } break; } // If response is requested, return the result data if (shouldRespond) { responseResult = resultData; } }); return(responseResult); }
public static async Task <string> PlaceData(JObject dataBundleRoot, string path, NodeDataOvewriteMode ovewriteMode) { string result = null; // Optionally used to return a result var convTokenPath = FlatJsonPath.ConvertUriPathToTokenPath(path); var convTokenPrfx = FlatJsonPath.ConvertUriPathToTokenPrefix(path); await Task.Run(() => { var db = DatabaseAccessService.OpenOrCreateDefault(); var store = db.GetCollection <JsonObjectStoreContainer>(DatabaseAccessService.GetDataStoreKey(0)); if (store.Count() == 0) { using (var trans = db.BeginTrans()) { var newRoot = new JObject(); store.Insert(new JsonObjectStoreContainer { ContainerId = Guid.NewGuid(), FlattenedJObject = new FlatJsonObject() }); trans.Commit(); } } var rootObjectContainer = store.FindAll().FirstOrDefault(); var flattenedRootObject = new FlatJsonObject(rootObjectContainer.FlattenedJObject); //var rootObjectJ = JsonFlattener.UnflattenJObject(flattenedRootObject); using (var trans = db.BeginTrans()) { // Put in the new data switch (ovewriteMode) { case NodeDataOvewriteMode.Update: { // Flatten input bundle var flattenedBundle = new FlatJsonObject(dataBundleRoot, convTokenPrfx); flattenedRootObject.Merge(flattenedBundle); } break; case NodeDataOvewriteMode.Put: { // Flatten input bundle var flattenedBundle = new FlatJsonObject(dataBundleRoot, convTokenPrfx); // Remove existing data flattenedRootObject.RemoveNode(convTokenPath); // Add new data flattenedRootObject.Merge(flattenedBundle); } break; case NodeDataOvewriteMode.Push: { // Use the Firebase Push ID algorithm var pushId = PushIdGenerator.GeneratePushId(); // Create flattened bundle with pushId added to prefix convTokenPrfx = FlatJsonPath.AppendToTokenPrefix(convTokenPrfx, pushId); // Flatten input bundle var flattenedBundle = new FlatJsonObject(dataBundleRoot, convTokenPrfx); flattenedRootObject.Merge(flattenedBundle); result = pushId; } break; } // Update and store rootObjectContainer.FlattenedJObject = flattenedRootObject; store.Update(rootObjectContainer); trans.Commit(); } // Data was written }); return(result); }