From 862df7c6fadca1c1a2ee9cb30a766461a9f2d98a Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 28 Aug 2019 12:10:59 +0800 Subject: [PATCH] =?UTF-8?q?*=20ObjectControllerTests.cs:=20chore:=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=B9=E6=8D=AE=E6=9D=A1=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=20Object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ObjectTest.cs: * AVObject.cs: * AVObjectController.cs: --- Storage/Storage.Test/ObjectControllerTests.cs | 10 ++++++++ Storage/Storage.Test/ObjectTest.cs | 8 +------ .../Object/Controller/AVObjectController.cs | 10 ++++++++ Storage/Storage/Public/AVObject.cs | 24 +++---------------- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/Storage/Storage.Test/ObjectControllerTests.cs b/Storage/Storage.Test/ObjectControllerTests.cs index 5b02f5f..ba7ee79 100644 --- a/Storage/Storage.Test/ObjectControllerTests.cs +++ b/Storage/Storage.Test/ObjectControllerTests.cs @@ -23,6 +23,16 @@ namespace LeanCloudTests { Assert.NotNull(obj.UpdatedAt); } + [Test] + public async Task SaveWithQuery() { + AVObject account = AVObject.CreateWithoutData("Account", "5d65fa5330863b008065e476"); + AVQuery query = new AVQuery("Account"); + query.WhereGreaterThan("balance", 80); + account["balance"] = 50; + await account.SaveAsync(query); + TestContext.Out.WriteLine($"balance: {account["balance"]}"); + } + [Test] public async Task Fetch() { AVObject obj = AVObject.CreateWithoutData("Todo", "5d5f6039d5de2b006cf29c8f"); diff --git a/Storage/Storage.Test/ObjectTest.cs b/Storage/Storage.Test/ObjectTest.cs index 161eebb..299d986 100644 --- a/Storage/Storage.Test/ObjectTest.cs +++ b/Storage/Storage.Test/ObjectTest.cs @@ -8,13 +8,7 @@ namespace LeanCloudTests { public class ObjectTests { [SetUp] public void SetUp() { - AVClient.Initialize(new AVClient.Configuration { - ApplicationId = "BMYV4RKSTwo8WSqt8q9ezcWF-gzGzoHsz", - ApplicationKey = "pbf6Nk5seyjilexdpyrPwjSp", - ApiServer = "https://avoscloud.com", - RTMServer = "https://router-g0-push.avoscloud.com", - }); - AVClient.HttpLog(TestContext.Out.WriteLine); + Utils.InitNorthChina(); } [Test] diff --git a/Storage/Storage/Internal/Object/Controller/AVObjectController.cs b/Storage/Storage/Internal/Object/Controller/AVObjectController.cs index 9493344..e0d4934 100644 --- a/Storage/Storage/Internal/Object/Controller/AVObjectController.cs +++ b/Storage/Storage/Internal/Object/Controller/AVObjectController.cs @@ -35,14 +35,24 @@ namespace LeanCloud.Storage.Internal { public Task SaveAsync(IObjectState state, IDictionary operations, + AVQuery query, string sessionToken, CancellationToken cancellationToken) { var objectJSON = AVObject.ToJSONObjectForSaving(operations); + var command = new AVCommand { Path = state.ObjectId == null ? $"classes/{Uri.EscapeDataString(state.ClassName)}" : $"classes/{Uri.EscapeDataString(state.ClassName)}/{state.ObjectId}", Method = state.ObjectId == null ? HttpMethod.Post : HttpMethod.Put, Content = objectJSON }; + // 查询条件 + if (query != null && query.where != null) { + Dictionary where = new Dictionary { + { "where", PointerOrLocalIdEncoder.Instance.Encode(query.where) } + }; + string encode = AVClient.BuildQueryString(where); + command.Path = $"{command.Path}?{encode}"; + } return AVPlugins.Instance.CommandRunner.RunCommandAsync>(command, cancellationToken: cancellationToken).OnSuccess(t => { var serverState = AVObjectCoder.Instance.Decode(t.Result.Item2, AVDecoder.Instance); serverState = serverState.MutatedClone(mutableClone => { diff --git a/Storage/Storage/Public/AVObject.cs b/Storage/Storage/Public/AVObject.cs index a17e2de..a41a981 100644 --- a/Storage/Storage/Public/AVObject.cs +++ b/Storage/Storage/Public/AVObject.cs @@ -557,13 +557,12 @@ string propertyName } } - protected virtual Task SaveAsync(Task toAwait, - CancellationToken cancellationToken) { + public virtual Task SaveAsync(AVQuery query = null, CancellationToken cancellationToken = default) { IDictionary currentOperations = null; if (!IsDirty) { return Task.FromResult(0); } - + Task deepSaveTask; string sessionToken; lock (mutex) { @@ -576,10 +575,9 @@ string propertyName } return deepSaveTask.OnSuccess(_ => { - return toAwait; - }).Unwrap().OnSuccess(_ => { return ObjectController.SaveAsync(state, currentOperations, + query, sessionToken, cancellationToken); }).Unwrap().ContinueWith(t => { @@ -593,22 +591,6 @@ string propertyName }).Unwrap(); } - /// - /// Saves this object to the server. - /// - public virtual Task SaveAsync() { - return SaveAsync(CancellationToken.None); - } - - /// - /// Saves this object to the server. - /// - /// The cancellation token. - public virtual Task SaveAsync(CancellationToken cancellationToken) { - return taskQueue.Enqueue(toAwait => SaveAsync(toAwait, cancellationToken), - cancellationToken); - } - internal virtual Task FetchAsyncInternal( Task toAwait, IDictionary queryString,