/// <summary> /// Creates a MoBackRelation object from JSON returned from server. /// </summary> /// <returns>The relation.</returns> /// <param name="relationJSON">Relation in JSON form, from server.</param> public static MoBackRelation RelationFromMoBackJSON(SimpleJSONNode relationJSON) { MoBackRelation relation = new MoBackRelation(); SimpleJSONArray pointersJSON = relationJSON ["value"].AsArray; for (int i = 0; i < pointersJSON.Count; i++) { // If the nested json doesn't have a pointer type, just return null. // When AchieveRelation with ?include={ColumnName}, it will return an object type. if (pointersJSON[i]["__type"].Value != "Pointer") { if (pointersJSON[i]["__type"].Value == "object") { Debug.LogWarning("The response JSON contains Object type, not Pointer type, can't parse this relationJson. Try MoBackRelation.MoBackRowFromRelationJSON() instead"); return(null); } else { Debug.LogError("Unknown type: " + pointersJSON[i]["__type"].Value); return(null); } } relation._pointers.Add(MoBackPointer.PointerFromMoBackJSON(pointersJSON[i])); } return(relation); }
/// <summary> /// Gets all relation objects. /// </summary> /// <returns>The all relation objects.</returns> /// <param name="tableName">Table name.</param> /// <param name="objectID">Object I.</param> /// <param name="columnName">Column name.</param> public static MoBackRequest <List <MoBackRow> > GetAllRelationObjects(string tableName, string objectID, string columnName) { MoBackRequest <List <MoBackRow> > .ResponseProcessor getAllRelationObjectsProcessor = (SimpleJSONNode responseJson) => { SimpleJSONNode relationJson = responseJson[columnName]; List <MoBackRow> moBackObjects = MoBackRelation.MoBackRowFromRelationJSON(relationJson); return(moBackObjects); }; /* * Sample uri: https://api.moback.com/objectmgr/api/collections/{tableName}/{objectID}?include={columnName} */ return(new MoBackRequest <List <MoBackRow> >(getAllRelationObjectsProcessor, MoBackURLS.TablesDefault + tableName + "/" + objectID + string.Format("?include={0}", columnName), HTTPMethod.GET)); }
/// <summary> /// MoBack JSON from relation. Of dubious usefulness because uploading and updating relations directly is presently forbidden. /// </summary> /// <returns>The moback JSON representing the relation.</returns> /// <param name="relation">Relation.</param> public static SimpleJSONNode MoBackJSONFromRelation(MoBackRelation relation) { SimpleJSONClass relationJSON = new SimpleJSONClass(); relationJSON ["__type"] = "Pointer"; SimpleJSONArray pointersJSON = new SimpleJSONArray(); for (int i = 0; i < relation._pointers.Count; i++) { pointersJSON.Add(MoBackPointer.PointerToMoBackJSON(relation._pointers[i])); } relationJSON ["value"] = pointersJSON; return(relationJSON); }
/// <summary> /// Gets all relation pointers. /// </summary> /// <returns>The all relation pointers.</returns> /// <param name="tableName">Table name.</param> /// <param name="objectID">Object I.</param> /// <param name="columnName">Column name.</param> public static MoBackRequest <List <MoBackPointer> > GetAllRelationPointers(string tableName, string objectID, string columnName) { MoBackRequest <List <MoBackPointer> > .ResponseProcessor getAllRelationPointersProcessor = (SimpleJSONNode responseJson) => { SimpleJSONNode relationJson = responseJson[columnName]; MoBackRelation moBackRelation = MoBackRelation.RelationFromMoBackJSON(relationJson); if (moBackRelation != null) { return(moBackRelation.pointers.ToList()); } return(null); }; /* * Sample uri: https://api.moback.com/objectmgr/api/collections/{tableName}/{objectID} */ return(new MoBackRequest <List <MoBackPointer> >(getAllRelationPointersProcessor, MoBackURLS.TablesDefault + tableName + "/" + objectID, HTTPMethod.GET)); }
/// <summary> /// Request to remove one or more pointers to a relations column. /// </summary> /// <returns>The relations.</returns> /// <param name="relationColumnID">Relation column to add pointers to.</param> /// <param name="pointerToRemove">Pointer to add.</param> public MoBackRequest RemoveRelations(string relationColumnID, MoBackRow[] pointerToRemove) { MoBackPointer[] targetArray = new MoBackPointer[pointerToRemove.Length]; for (int i = 0; i < pointerToRemove.Length; i++) { targetArray[i] = GetPointer(pointerToRemove[i]); } if (string.IsNullOrEmpty(ObjectId)) { return(null); } return(MoBackTableInterface.RequestBuilder.ModifyRelationForGivenRowColumn(TableName, ObjectId, relationColumnID, MoBackRelation.RelationRemoveOpAsMoBackJSON(targetArray))); }
/// <summary> /// Request to add one or more pointers to a relations column. /// </summary> /// <returns>The relations.</returns> /// <param name="relationColumnID">Relation column to add pointers to.</param> /// <param name="pointersToAdd">Pointers to add.</param> public MoBackRequest AddRelations(string relationColumnID, MoBackRow[] pointersToAdd) { MoBackPointer[] targetArray = new MoBackPointer[pointersToAdd.Length]; for (int i = 0; i < pointersToAdd.Length; i++) { targetArray[i] = GetPointer(pointersToAdd[i]); } if (string.IsNullOrEmpty(ObjectId)) { Debug.LogError("Relations can only be added to objects that have known object IDs"); return(null); } return(MoBackTableInterface.RequestBuilder.ModifyRelationForGivenRowColumn(TableName, ObjectId, relationColumnID, MoBackRelation.RelationAddOpAsMoBackJSON(targetArray))); }