chore: 简化存储
parent
81b2ea993f
commit
e1c57e5bc3
|
@ -7,62 +7,47 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace LeanCloud.Storage.Internal {
|
||||
public class AVQueryController {
|
||||
public Task<IEnumerable<IObjectState>> FindAsync<T>(AVQuery<T> query, AVUser user,
|
||||
public async Task<IEnumerable<IObjectState>> FindAsync<T>(AVQuery<T> query, AVUser user,
|
||||
CancellationToken cancellationToken) where T : AVObject {
|
||||
string sessionToken = user != null ? user.SessionToken : null;
|
||||
|
||||
return FindAsync(query.Path, query.BuildParameters(), sessionToken, cancellationToken).OnSuccess(t => {
|
||||
var items = t.Result["results"] as IList<object>;
|
||||
|
||||
return (from item in items
|
||||
select AVObjectCoder.Instance.Decode(item as IDictionary<string, object>, AVDecoder.Instance));
|
||||
});
|
||||
IList<object> items = await FindAsync<IList<object>>(query.Path, query.BuildParameters(), "results", cancellationToken);
|
||||
return from item in items
|
||||
select AVObjectCoder.Instance.Decode(item as IDictionary<string, object>, AVDecoder.Instance);
|
||||
}
|
||||
|
||||
public Task<int> CountAsync<T>(AVQuery<T> query,
|
||||
public async Task<int> CountAsync<T>(AVQuery<T> query,
|
||||
AVUser user,
|
||||
CancellationToken cancellationToken) where T : AVObject {
|
||||
string sessionToken = user != null ? user.SessionToken : null;
|
||||
var parameters = query.BuildParameters();
|
||||
parameters["limit"] = 0;
|
||||
parameters["count"] = 1;
|
||||
|
||||
return FindAsync(query.Path, parameters, sessionToken, cancellationToken).OnSuccess(t => {
|
||||
return Convert.ToInt32(t.Result["count"]);
|
||||
});
|
||||
long ret = await FindAsync<long>(query.Path, parameters, "count", cancellationToken);
|
||||
return Convert.ToInt32(ret);
|
||||
}
|
||||
|
||||
public Task<IObjectState> FirstAsync<T>(AVQuery<T> query,
|
||||
public async Task<IObjectState> FirstAsync<T>(AVQuery<T> query,
|
||||
AVUser user,
|
||||
CancellationToken cancellationToken) where T : AVObject {
|
||||
string sessionToken = user?.SessionToken;
|
||||
var parameters = query.BuildParameters();
|
||||
parameters["limit"] = 1;
|
||||
|
||||
return FindAsync(query.Path, parameters, sessionToken, cancellationToken).OnSuccess(t => {
|
||||
var items = t.Result["results"] as IList<object>;
|
||||
var item = items.FirstOrDefault() as IDictionary<string, object>;
|
||||
|
||||
// Not found. Return empty state.
|
||||
if (item == null) {
|
||||
return (IObjectState)null;
|
||||
}
|
||||
|
||||
return AVObjectCoder.Instance.Decode(item, AVDecoder.Instance);
|
||||
});
|
||||
IList<object> items = await FindAsync<IList<object>>(query.Path, query.BuildParameters(), "results", cancellationToken);
|
||||
// Not found. Return empty state.
|
||||
if (!(items.FirstOrDefault() is IDictionary<string, object> item)) {
|
||||
return (IObjectState)null;
|
||||
}
|
||||
return AVObjectCoder.Instance.Decode(item, AVDecoder.Instance);
|
||||
}
|
||||
|
||||
private Task<IDictionary<string, object>> FindAsync(string path,
|
||||
private async Task<T> FindAsync<T>(string path,
|
||||
IDictionary<string, object> parameters,
|
||||
string sessionToken,
|
||||
string key,
|
||||
CancellationToken cancellationToken) {
|
||||
var command = new AVCommand {
|
||||
Path = $"{path}?{AVClient.BuildQueryString(parameters)}",
|
||||
Method = HttpMethod.Get
|
||||
};
|
||||
return AVPlugins.Instance.CommandRunner.RunCommandAsync<IDictionary<string, object>>(command, cancellationToken: cancellationToken).OnSuccess(t => {
|
||||
return t.Result.Item2;
|
||||
});
|
||||
var result = await AVPlugins.Instance.CommandRunner.RunCommandAsync<IDictionary<string, object>>(command, cancellationToken: cancellationToken);
|
||||
return (T)result.Item2[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,9 +7,6 @@ using System.Threading;
|
|||
using Newtonsoft.Json;
|
||||
|
||||
namespace LeanCloud.Storage.Internal {
|
||||
/// <summary>
|
||||
/// Implements `IStorageController` for PCL targets, based off of PCLStorage.
|
||||
/// </summary>
|
||||
public class StorageController {
|
||||
public class StorageDictionary : IEnumerable<KeyValuePair<string, object>> {
|
||||
private readonly string filePath;
|
||||
|
|
Loading…
Reference in New Issue