chore: 简化存储

oneRain 2019-08-07 17:04:17 +08:00
parent 81b2ea993f
commit e1c57e5bc3
2 changed files with 18 additions and 36 deletions

View File

@ -7,62 +7,47 @@ using System.Threading.Tasks;
namespace LeanCloud.Storage.Internal { namespace LeanCloud.Storage.Internal {
public class AVQueryController { 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 { CancellationToken cancellationToken) where T : AVObject {
string sessionToken = user != null ? user.SessionToken : null; IList<object> items = await FindAsync<IList<object>>(query.Path, query.BuildParameters(), "results", cancellationToken);
return from item in items
return FindAsync(query.Path, query.BuildParameters(), sessionToken, cancellationToken).OnSuccess(t => { select AVObjectCoder.Instance.Decode(item as IDictionary<string, object>, AVDecoder.Instance);
var items = t.Result["results"] as IList<object>;
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, AVUser user,
CancellationToken cancellationToken) where T : AVObject { CancellationToken cancellationToken) where T : AVObject {
string sessionToken = user != null ? user.SessionToken : null;
var parameters = query.BuildParameters(); var parameters = query.BuildParameters();
parameters["limit"] = 0; parameters["limit"] = 0;
parameters["count"] = 1; parameters["count"] = 1;
long ret = await FindAsync<long>(query.Path, parameters, "count", cancellationToken);
return FindAsync(query.Path, parameters, sessionToken, cancellationToken).OnSuccess(t => { return Convert.ToInt32(ret);
return Convert.ToInt32(t.Result["count"]);
});
} }
public Task<IObjectState> FirstAsync<T>(AVQuery<T> query, public async Task<IObjectState> FirstAsync<T>(AVQuery<T> query,
AVUser user, AVUser user,
CancellationToken cancellationToken) where T : AVObject { CancellationToken cancellationToken) where T : AVObject {
string sessionToken = user?.SessionToken; string sessionToken = user?.SessionToken;
var parameters = query.BuildParameters(); var parameters = query.BuildParameters();
parameters["limit"] = 1; parameters["limit"] = 1;
IList<object> items = await FindAsync<IList<object>>(query.Path, query.BuildParameters(), "results", cancellationToken);
return FindAsync(query.Path, parameters, sessionToken, cancellationToken).OnSuccess(t => { // Not found. Return empty state.
var items = t.Result["results"] as IList<object>; if (!(items.FirstOrDefault() is IDictionary<string, object> item)) {
var item = items.FirstOrDefault() as IDictionary<string, object>; return (IObjectState)null;
}
// Not found. Return empty state. return AVObjectCoder.Instance.Decode(item, AVDecoder.Instance);
if (item == null) {
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, IDictionary<string, object> parameters,
string sessionToken, string key,
CancellationToken cancellationToken) { CancellationToken cancellationToken) {
var command = new AVCommand { var command = new AVCommand {
Path = $"{path}?{AVClient.BuildQueryString(parameters)}", Path = $"{path}?{AVClient.BuildQueryString(parameters)}",
Method = HttpMethod.Get Method = HttpMethod.Get
}; };
return AVPlugins.Instance.CommandRunner.RunCommandAsync<IDictionary<string, object>>(command, cancellationToken: cancellationToken).OnSuccess(t => { var result = await AVPlugins.Instance.CommandRunner.RunCommandAsync<IDictionary<string, object>>(command, cancellationToken: cancellationToken);
return t.Result.Item2; return (T)result.Item2[key];
});
} }
} }
} }

View File

@ -7,9 +7,6 @@ using System.Threading;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace LeanCloud.Storage.Internal { namespace LeanCloud.Storage.Internal {
/// <summary>
/// Implements `IStorageController` for PCL targets, based off of PCLStorage.
/// </summary>
public class StorageController { public class StorageController {
public class StorageDictionary : IEnumerable<KeyValuePair<string, object>> { public class StorageDictionary : IEnumerable<KeyValuePair<string, object>> {
private readonly string filePath; private readonly string filePath;