* ObjectControllerTests.cs: chore: 增加根据条件更新 Object
* ObjectTest.cs: * AVObject.cs: * AVObjectController.cs:
parent
9d3c9dc178
commit
862df7c6fa
|
@ -23,6 +23,16 @@ namespace LeanCloudTests {
|
||||||
Assert.NotNull(obj.UpdatedAt);
|
Assert.NotNull(obj.UpdatedAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task SaveWithQuery() {
|
||||||
|
AVObject account = AVObject.CreateWithoutData("Account", "5d65fa5330863b008065e476");
|
||||||
|
AVQuery<AVObject> query = new AVQuery<AVObject>("Account");
|
||||||
|
query.WhereGreaterThan("balance", 80);
|
||||||
|
account["balance"] = 50;
|
||||||
|
await account.SaveAsync(query);
|
||||||
|
TestContext.Out.WriteLine($"balance: {account["balance"]}");
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task Fetch() {
|
public async Task Fetch() {
|
||||||
AVObject obj = AVObject.CreateWithoutData("Todo", "5d5f6039d5de2b006cf29c8f");
|
AVObject obj = AVObject.CreateWithoutData("Todo", "5d5f6039d5de2b006cf29c8f");
|
||||||
|
|
|
@ -8,13 +8,7 @@ namespace LeanCloudTests {
|
||||||
public class ObjectTests {
|
public class ObjectTests {
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp() {
|
public void SetUp() {
|
||||||
AVClient.Initialize(new AVClient.Configuration {
|
Utils.InitNorthChina();
|
||||||
ApplicationId = "BMYV4RKSTwo8WSqt8q9ezcWF-gzGzoHsz",
|
|
||||||
ApplicationKey = "pbf6Nk5seyjilexdpyrPwjSp",
|
|
||||||
ApiServer = "https://avoscloud.com",
|
|
||||||
RTMServer = "https://router-g0-push.avoscloud.com",
|
|
||||||
});
|
|
||||||
AVClient.HttpLog(TestContext.Out.WriteLine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|
|
@ -35,14 +35,24 @@ namespace LeanCloud.Storage.Internal {
|
||||||
|
|
||||||
public Task<IObjectState> SaveAsync(IObjectState state,
|
public Task<IObjectState> SaveAsync(IObjectState state,
|
||||||
IDictionary<string, IAVFieldOperation> operations,
|
IDictionary<string, IAVFieldOperation> operations,
|
||||||
|
AVQuery<AVObject> query,
|
||||||
string sessionToken,
|
string sessionToken,
|
||||||
CancellationToken cancellationToken) {
|
CancellationToken cancellationToken) {
|
||||||
var objectJSON = AVObject.ToJSONObjectForSaving(operations);
|
var objectJSON = AVObject.ToJSONObjectForSaving(operations);
|
||||||
|
|
||||||
var command = new AVCommand {
|
var command = new AVCommand {
|
||||||
Path = state.ObjectId == null ? $"classes/{Uri.EscapeDataString(state.ClassName)}" : $"classes/{Uri.EscapeDataString(state.ClassName)}/{state.ObjectId}",
|
Path = state.ObjectId == null ? $"classes/{Uri.EscapeDataString(state.ClassName)}" : $"classes/{Uri.EscapeDataString(state.ClassName)}/{state.ObjectId}",
|
||||||
Method = state.ObjectId == null ? HttpMethod.Post : HttpMethod.Put,
|
Method = state.ObjectId == null ? HttpMethod.Post : HttpMethod.Put,
|
||||||
Content = objectJSON
|
Content = objectJSON
|
||||||
};
|
};
|
||||||
|
// 查询条件
|
||||||
|
if (query != null && query.where != null) {
|
||||||
|
Dictionary<string, object> where = new Dictionary<string, object> {
|
||||||
|
{ "where", PointerOrLocalIdEncoder.Instance.Encode(query.where) }
|
||||||
|
};
|
||||||
|
string encode = AVClient.BuildQueryString(where);
|
||||||
|
command.Path = $"{command.Path}?{encode}";
|
||||||
|
}
|
||||||
return AVPlugins.Instance.CommandRunner.RunCommandAsync<IDictionary<string, object>>(command, cancellationToken: cancellationToken).OnSuccess(t => {
|
return AVPlugins.Instance.CommandRunner.RunCommandAsync<IDictionary<string, object>>(command, cancellationToken: cancellationToken).OnSuccess(t => {
|
||||||
var serverState = AVObjectCoder.Instance.Decode(t.Result.Item2, AVDecoder.Instance);
|
var serverState = AVObjectCoder.Instance.Decode(t.Result.Item2, AVDecoder.Instance);
|
||||||
serverState = serverState.MutatedClone(mutableClone => {
|
serverState = serverState.MutatedClone(mutableClone => {
|
||||||
|
|
|
@ -557,13 +557,12 @@ string propertyName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual Task SaveAsync(Task toAwait,
|
public virtual Task SaveAsync(AVQuery<AVObject> query = null, CancellationToken cancellationToken = default) {
|
||||||
CancellationToken cancellationToken) {
|
|
||||||
IDictionary<string, IAVFieldOperation> currentOperations = null;
|
IDictionary<string, IAVFieldOperation> currentOperations = null;
|
||||||
if (!IsDirty) {
|
if (!IsDirty) {
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Task deepSaveTask;
|
Task deepSaveTask;
|
||||||
string sessionToken;
|
string sessionToken;
|
||||||
lock (mutex) {
|
lock (mutex) {
|
||||||
|
@ -576,10 +575,9 @@ string propertyName
|
||||||
}
|
}
|
||||||
|
|
||||||
return deepSaveTask.OnSuccess(_ => {
|
return deepSaveTask.OnSuccess(_ => {
|
||||||
return toAwait;
|
|
||||||
}).Unwrap().OnSuccess(_ => {
|
|
||||||
return ObjectController.SaveAsync(state,
|
return ObjectController.SaveAsync(state,
|
||||||
currentOperations,
|
currentOperations,
|
||||||
|
query,
|
||||||
sessionToken,
|
sessionToken,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
}).Unwrap().ContinueWith(t => {
|
}).Unwrap().ContinueWith(t => {
|
||||||
|
@ -593,22 +591,6 @@ string propertyName
|
||||||
}).Unwrap();
|
}).Unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Saves this object to the server.
|
|
||||||
/// </summary>
|
|
||||||
public virtual Task SaveAsync() {
|
|
||||||
return SaveAsync(CancellationToken.None);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Saves this object to the server.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
public virtual Task SaveAsync(CancellationToken cancellationToken) {
|
|
||||||
return taskQueue.Enqueue(toAwait => SaveAsync(toAwait, cancellationToken),
|
|
||||||
cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal virtual Task<AVObject> FetchAsyncInternal(
|
internal virtual Task<AVObject> FetchAsyncInternal(
|
||||||
Task toAwait,
|
Task toAwait,
|
||||||
IDictionary<string, object> queryString,
|
IDictionary<string, object> queryString,
|
||||||
|
|
Loading…
Reference in New Issue