/// <summary> /// Deletes the object. /// </summary> /// <returns>Returns the status of the request.</returns> /// <param name="objectID">Object identifier.</param> public MoBackRequest DeleteObject(string objectID, MoBackRequest.ResponseProcessor deleteProcessor = null) { if (deleteProcessor == null) { /* * Sample uri: https://api.moback.com/objectmgr/api/collections/{tableName}/{objectID} */ return(new MoBackRequest(MoBackURLS.TablesDefault + table.TableName + "/" + objectID, HTTPMethod.DELETE)); } /* * Sample uri: https://api.moback.com/objectmgr/api/collections/{tableName}/{objectID} */ return(new MoBackRequest(deleteProcessor, MoBackURLS.TablesDefault + table.TableName + "/" + objectID, HTTPMethod.DELETE)); }
/// <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> /// Deletes the current user. /// </summary> /// <returns>A MoBackRequest with a MoBackUser type.</returns> public MoBackRequest DeleteCurrentUser() { if (!IsLoggedIn) { Debug.LogError("This current user hasn't been logged in yet"); return(null); } MoBackRequest.ResponseProcessor deleteProcessor = (SimpleJSONNode jsonObject) => { if (jsonObject["code"] == "1000") { ObjectId = null; CreatedDate = default(DateTime); UpdatedDate = default(DateTime); IsLoggedIn = false; CompleteSignedUp = false; // Check in storeValues and remove them if any. if (storeValues.ContainsKey("objectId")) { storeValues.Remove("objectId"); Debug.Log("remove objectid"); } if (storeValues.ContainsKey("createdAt")) { storeValues.Remove("createdAt"); Debug.Log("createdAt"); } if (storeValues.ContainsKey("updatedAt")) { storeValues.Remove("updatedAt"); Debug.Log("createdAt"); } } }; return(new MoBackRequest(deleteProcessor, MoBackURLS.User, HTTPMethod.DELETE, null, null)); }
/// <summary> /// Signs up. /// </summary> /// <returns>A MoBackRequest with a MoBackUser type.</returns> /// <param name="userName">User name.</param> /// <param name="email">Email.</param> /// <param name="password">Password.</param> public MoBackRequest SignUp() { SimpleJSONClass jsonStructure; // Get any values in storedValue to Json. jsonStructure = GetJSON(); // Add specific column for MoBackUser. jsonStructure.Add("userId", UserName); jsonStructure.Add("email", EmailAddress); jsonStructure.Add("password", Password); byte[] postData = jsonStructure.ToString().ToByteArray(); MoBackRequest.ResponseProcessor SignUpProcessor = (SimpleJSONNode jsonObject) => { // If objectId is null or empty means we're unable to create an new user. ObjectId = jsonObject["objectId"]; CompleteSignedUp = !string.IsNullOrEmpty(ObjectId); }; return(new MoBackRequest(SignUpProcessor, MoBackURLS.SignUp, HTTPMethod.POST, null, postData)); }
/// <summary> /// Login the specified userName and password. /// </summary> /// <param name="userName">User name.</param> /// <param name="password">Password.</param> /// <returns>A MoBackRequest with a MoBackUser type.</returns> public MoBackRequest Login() { SimpleJSONClass jsonStructure = new SimpleJSONClass(); jsonStructure.Add("userId", UserName); jsonStructure.Add("password", Password); byte[] postData = jsonStructure.ToString().ToByteArray(); MoBackRequest.ResponseProcessor LoginProcessor = (SimpleJSONNode jsonObject) => { string ssotoken = jsonObject["ssotoken"]; if (string.IsNullOrEmpty(ssotoken)) { IsLoggedIn = false; } else { IsLoggedIn = true; MoBack.MoBackAppSettings.SessionToken = ssotoken; } }; return(new MoBackRequest(LoginProcessor, MoBackURLS.Login, HTTPMethod.POST, null, postData)); }
/// <summary> /// Gets the user info. /// </summary> /// <returns>A MoBackRequest with a MoBackUser type.</returns> public MoBackRequest GetUserInfo() { // If there is not sessiontoken means user hasn't logged in yet, just exit. if (!IsLoggedIn) { Debug.LogError("There is no current login user."); return(null); } MoBackRequest.ResponseProcessor GetCurrentUserInfoProcessor = (SimpleJSONNode jsonObject) => { SimpleJSONClass userInfoJson = jsonObject["user"] as SimpleJSONClass; foreach (KeyValuePair <string, SimpleJSONNode> entry in userInfoJson.dict) { // we stored these outside of storeValues. if (entry.Key == "userId" || entry.Key == "createdAt" || entry.Key == "updatedAt" || entry.Key == "objectId") { continue; } MoBackValueType moBackType; object data = MoBackUtils.MoBackTypedObjectFromJSON(entry.Value, out moBackType); // This should always be equivalent to calling SetValue() with the appropriate type; if SetValue() functions are refactored then so too should this, and vice-versa. this.SetColumnType(entry.Key, moBackType); this.storeValues[entry.Key] = data; } UserName = userInfoJson["userId"]; ObjectId = userInfoJson["objectId"]; CreatedDate = MoBackDate.DateFromString(userInfoJson["createdAt"]); UpdatedDate = MoBackDate.DateFromString(userInfoJson["updatedAt"]); }; return(new MoBackRequest(GetCurrentUserInfoProcessor, MoBackURLS.User, HTTPMethod.GET, null, null)); }
/// <summary> /// Saves multiple objects with one single call to server. /// </summary> /// <returns>The MobackRequest.</returns> /// <param name="rowsToSave">MoBack Rows need to save.</param> public MoBackRequest SaveMultipleObjects(List <MoBackRow> rowsToSave) { string uri = MoBackURLS.Batch + table.TableName; SimpleJSONArray jsonArray = new SimpleJSONArray(); SimpleJSONClass jsonStructure = new SimpleJSONClass(); for (int i = 0; i < rowsToSave.Count; i++) { SimpleJSONClass jsonToSave = rowsToSave[i].GetJSON(); if (rowsToSave[i].ObjectId != null) { jsonToSave.Add("objectId", rowsToSave[i].ObjectId); } jsonArray.Add(jsonToSave); } jsonStructure.Add("objects", jsonArray); Debug.Log(jsonStructure.ToString()); byte[] bytes = jsonStructure.ToString().ToByteArray(); // Construct a callback to update all rowsToSave. MoBackRequest.ResponseProcessor objUpdater = (SimpleJSONNode responseJson) => { SimpleJSONClass allObjectsInJsonFormat = responseJson["batchResponse"].AsObject; // The response should have the same amount as of the request body. if (allObjectsInJsonFormat.Count != rowsToSave.Count) { Debug.LogError(string.Format("Response objects count: {0}. Request objects count: {1}", allObjectsInJsonFormat.Count, rowsToSave.Count)); Debug.LogError("Response doesn't have the same object as Request Body. Something wrong!"); return; } for (int i = 0; i < rowsToSave.Count; i++) { SimpleJSONClass jsonAtCurrentIndex = allObjectsInJsonFormat[i] as SimpleJSONClass; if (!jsonAtCurrentIndex["success"].AsBool) { Debug.LogError("Unable to save: " + jsonAtCurrentIndex["objectId"]); continue; } if (!string.IsNullOrEmpty(jsonAtCurrentIndex["createdAt"].ToString())) { string objectId = jsonAtCurrentIndex["objectId"]; DateTime createdTime = MoBackDate.DateFromString(jsonAtCurrentIndex["createdAt"]); rowsToSave[i].UpdateAfterSave(objectId, createdTime, createdTime); } else if (!string.IsNullOrEmpty(jsonAtCurrentIndex["updatedAt"].ToString())) { string objectId = jsonAtCurrentIndex["objectId"]; DateTime updateTime = MoBackDate.DateFromString(jsonAtCurrentIndex["updatedAt"]); rowsToSave[i].UpdateAfterSave(objectId, null, updateTime); } } }; /* * Sample uri: https://api.moback.com/objectmgr/api/collections/batch/{tableName} * Sample Json Request Body: * { * "objects" : [{ "Name" : "Joe", "FavoriteFood" : ["pizza", "fried eggs", "ham sandwich"]}] * } */ return(new MoBackRequest(objUpdater, uri, HTTPMethod.POST, null, bytes)); }
/// <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)); } }
/// <summary> /// Deletes the object. /// </summary> /// <returns>Returns the status of the request.</returns> /// <param name="objectID">Object identifier.</param> public MoBackRequest DeleteObject(string objectID, MoBackRequest.ResponseProcessor deleteProcessor = null) { return(RequestB.DeleteObject(objectID, deleteProcessor)); }