/// <summary> /// Return a list of MoBackRow object from the relation JSON /// </summary> /// <returns>The back row from relation JSO.</returns> /// <param name="relationJSON">Relation JSO.</param> public static List <MoBackRow> MoBackRowFromRelationJSON(SimpleJSONNode relationJSON) { SimpleJSONArray nestedArray = relationJSON["value"] as SimpleJSONArray; List <MoBackRow> moBackRows = new List <MoBackRow>(); // If the nested json doesn't have an object type, just return null. // When AchieveRelation without ?include={ColumnName}, it will return a pointer type. for (int i = 0; i < nestedArray.Count; i++) { if (nestedArray[i]["__type"].Value != "object") { if (nestedArray[i]["__type"].Value == "Pointer") { Debug.LogWarning("The response JSON contains Pointer type, not Object type, can't parse this relationJson. Try MoBackRelation.RelationFromMoBackJSON() instead"); return(null); } else { Debug.LogError("Unknown type: " + nestedArray[i]["__type"].Value); return(null); } } SimpleJSONClass jsonObject = nestedArray[i] as SimpleJSONClass; // This will get the MoBackObject exlucde the 2 keys: "__type" and "className". moBackRows.Add(MoBackRow.FromJSON(jsonObject, nestedArray[i]["className"])); } return(moBackRows); }
/// <summary> /// Returns the pointer to "row". /// </summary> /// <returns>The pointer.</returns> /// <param name="row">Row.</param> private MoBackPointer GetPointer(MoBackRow row) { MoBackTableInterface targetTable = new MoBackTableInterface(row.TableName.ToString()); MoBackPointer pToTarget = new MoBackPointer(row.ObjectId.ToString(), targetTable); return(pToTarget); }
/// <summary> /// Creates a <see cref="MoBack.MoBackRow"/> object that encapsulates the provided MoBackObject (e.g. for saving an object to a table as a new row). /// </summary> /// <param name="tableName">Table the row should be associated with.</param> /// <param name="data">MoBackObject to encapsulate.</param> public MoBackRow(string tableName, MoBackObject data) { MoBackRow objectAsRow = new MoBackRow(); objectAsRow.TableName = tableName; objectAsRow.ShallowCopy(data); }
/// <summary> /// Converts a JSON object to a MoBackRow object. /// </summary> /// <returns>The JSON date converted to a MoBackRow.</returns> /// <param name="jsonObject">A JSON object.</param> /// <param name="sourceTableID">Source table ID.</param> public static MoBackRow FromJSON(SimpleJSONClass jsonObject, string sourceTableID = null) { MoBackRow mObject = ExtractMetadata(jsonObject, sourceTableID); ProcessJsonIntoMoBackObject(mObject, jsonObject); return(mObject); }
/// <summary> /// Gets the objects processor. /// </summary> /// <returns>A list of objects of the MoBackRow type converted from a JSON object.</returns> /// <param name="jsonObject">JSON object.</param> private List <MoBackRow> GetObjects_processor(SimpleJSONNode jsonObject) { List <MoBackRow> objects = new List <MoBackRow>(); // TODO: this doesn't work. Need to double check later. SimpleJSONArray results = jsonObject["results"].AsArray; for (int i = 0; i < results.Count; i++) { objects.Add(MoBackRow.FromJSON(results[i] as SimpleJSONClass, table.TableName)); } return(objects); }
/// <summary> /// Deletes the multiple ojbects. /// </summary> /// <returns>A MoBackRequest.</returns> /// <param name="objectsToDelete">Objects to delete.</param> public MoBackRequest DeleteMultipleOjbects(List <MoBackRow> objectsToDelete) { string uri = MoBackURLS.BatchDelete + table.TableName; SimpleJSONArray jsonArray = new SimpleJSONArray(); SimpleJSONClass jsonBody = new SimpleJSONClass(); foreach (MoBackRow item in objectsToDelete) { jsonArray.Add(MoBackUtils.MoBackTypedObjectToJSON(item.ObjectId, MoBackValueType.String)); } jsonBody.Add("objectIds", jsonArray); byte[] bytes = jsonBody.ToString().ToByteArray(); MoBackRequest.ResponseProcessor deleteCallBack = (SimpleJSONNode responseJson) => { SimpleJSONArray responseArray = responseJson["deletedObjectIds"].AsArray; Debug.Log(responseArray.ToString()); foreach (SimpleJSONNode item in responseArray) { MoBackRow deletedRow = objectsToDelete.Find(row => row.ObjectId == item.Value); if (deletedRow != null) { deletedRow.ResetMetaData(null); } else { Debug.Log("Can't find row"); } } }; /* * Sample uri: https://api.moback.com/objectmgr/api/collections/batch/delete/{tableName} * Sample Json Request Body: * { * "objectIds" : ["Vp6pfOSwp23tC3IN", "Vp6ZnOSwp23tC3Hv"] * } */ return(new MoBackRequest(deleteCallBack, uri, HTTPMethod.POST, null, bytes)); }
/// <summary> /// Gets the object processor. /// </summary> /// <returns>The a MoBackRow converted from a JSON object.</returns> /// <param name="jsonObject">A JSON object.</param> private MoBackRow GetObject_processor(SimpleJSONNode jsonObject) { return(MoBackRow.FromJSON(jsonObject as SimpleJSONClass, table.TableName)); }
/// <summary> /// Saves the object. /// </summary> /// <returns>Returns the status of the request.</returns> /// <param name="toSave">To save.</param> /// <param name="objectDataUpdater">Object data updater.</param> public MoBackRequest SaveObject(MoBackRow toSave, Action <string, DateTime?, DateTime> objectDataUpdater) { string uri = MoBackURLS.TablesDefault + table.TableName; byte[] bytes = toSave.GetJSON().ToString().ToByteArray(); // Make a new object if (string.IsNullOrEmpty(toSave.ObjectId)) { // Declare a callback inline so we can store the MoBackRow in it without creating a bunch of extra infrastructure: MoBackRequest.ResponseProcessor objUpdater = (SimpleJSONNode json) => { /* * Sample Json response: * { * "createdAt": "2016-01-18T18:41:39.475Z", * "objectId": "Vp0x4-Swp23tC3Ap", * "success": true, * "__acl": * { * "globalRead": true, * "globalWrite": true * } * } */ string objId = json["objectId"]; DateTime createdDate = MoBackDate.DateFromString(json["createdAt"]); objectDataUpdater(objId, createdDate, createdDate); }; /* * Sample uri: https://api.moback.com/objectmgr/api/collections/{tableName} * Sample Json Request Body: * { * "Score" : 10, * "Name" : "Locke", * "Over21" : yes * } */ return(new MoBackRequest(objUpdater, uri, HTTPMethod.POST, null, bytes)); } else { // Update the object and declare a callback inline so we can store the MoBackRow in it without creating a bunch of extra infrastructure: MoBackRequest.ResponseProcessor objUpdater = (SimpleJSONNode json) => { /* * Sample Json response: * { * { * "updatedAt": "2016-01-21T23:27:18.209Z", * "__acl": * { * "globalRead": true, * "globalWrite": true * } * } * } */ DateTime updatedDate = MoBackDate.DateFromString(json["updatedAt"]); objectDataUpdater(null, null, updatedDate); }; uri += "/" + toSave.ObjectId; /* * Sample uri: https://api.moback.com/objectmgr/api/collections/{tableName}/{objectID} * Sample Json Request Body: * { * "Name" : "George" * } */ return(new MoBackRequest(objUpdater, uri, HTTPMethod.PUT, null, bytes)); } }