diff --git a/Storage/Storage/Internal/Query/Controller/AVQueryController.cs b/Storage/Storage/Internal/Query/Controller/AVQueryController.cs index c9289e5..9228fde 100644 --- a/Storage/Storage/Internal/Query/Controller/AVQueryController.cs +++ b/Storage/Storage/Internal/Query/Controller/AVQueryController.cs @@ -7,62 +7,47 @@ using System.Threading.Tasks; namespace LeanCloud.Storage.Internal { public class AVQueryController { - public Task> FindAsync(AVQuery query, AVUser user, + public async Task> FindAsync(AVQuery 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; - - return (from item in items - select AVObjectCoder.Instance.Decode(item as IDictionary, AVDecoder.Instance)); - }); + IList items = await FindAsync>(query.Path, query.BuildParameters(), "results", cancellationToken); + return from item in items + select AVObjectCoder.Instance.Decode(item as IDictionary, AVDecoder.Instance); } - public Task CountAsync(AVQuery query, + public async Task CountAsync(AVQuery 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(query.Path, parameters, "count", cancellationToken); + return Convert.ToInt32(ret); } - public Task FirstAsync(AVQuery query, + public async Task FirstAsync(AVQuery 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; - var item = items.FirstOrDefault() as IDictionary; - - // Not found. Return empty state. - if (item == null) { - return (IObjectState)null; - } - - return AVObjectCoder.Instance.Decode(item, AVDecoder.Instance); - }); + IList items = await FindAsync>(query.Path, query.BuildParameters(), "results", cancellationToken); + // Not found. Return empty state. + if (!(items.FirstOrDefault() is IDictionary item)) { + return (IObjectState)null; + } + return AVObjectCoder.Instance.Decode(item, AVDecoder.Instance); } - private Task> FindAsync(string path, + private async Task FindAsync(string path, IDictionary parameters, - string sessionToken, + string key, CancellationToken cancellationToken) { var command = new AVCommand { Path = $"{path}?{AVClient.BuildQueryString(parameters)}", Method = HttpMethod.Get }; - return AVPlugins.Instance.CommandRunner.RunCommandAsync>(command, cancellationToken: cancellationToken).OnSuccess(t => { - return t.Result.Item2; - }); + var result = await AVPlugins.Instance.CommandRunner.RunCommandAsync>(command, cancellationToken: cancellationToken); + return (T)result.Item2[key]; } } } diff --git a/Storage/Storage/Internal/Storage/StorageController.cs b/Storage/Storage/Internal/Storage/StorageController.cs index b5dcb27..6dd4d60 100644 --- a/Storage/Storage/Internal/Storage/StorageController.cs +++ b/Storage/Storage/Internal/Storage/StorageController.cs @@ -7,9 +7,6 @@ using System.Threading; using Newtonsoft.Json; namespace LeanCloud.Storage.Internal { - /// - /// Implements `IStorageController` for PCL targets, based off of PCLStorage. - /// public class StorageController { public class StorageDictionary : IEnumerable> { private readonly string filePath;