chore: LCQuery#Get(string objectId)

oneRain 2021-01-07 15:53:51 +08:00
parent 58245c18da
commit 346dcb3ba6
1 changed files with 12 additions and 6 deletions

View File

@ -346,12 +346,13 @@ namespace LeanCloud.Storage {
return (int)ret["count"]; return (int)ret["count"];
} }
public Task<T> Get(string objectId) { public async Task<T> Get(string objectId) {
if (string.IsNullOrEmpty(objectId)) { if (string.IsNullOrEmpty(objectId)) {
throw new ArgumentNullException(nameof(objectId)); throw new ArgumentNullException(nameof(objectId));
} }
WhereEqualTo("objectId", objectId); string path = $"classes/{ClassName}/{objectId}";
return First(); Dictionary<string, object> response = await LCApplication.HttpClient.Get<Dictionary<string, object>>(path);
return DecodeLCObject(response);
} }
public async Task<ReadOnlyCollection<T>> Find() { public async Task<ReadOnlyCollection<T>> Find() {
@ -361,9 +362,7 @@ namespace LeanCloud.Storage {
List<object> results = response["results"] as List<object>; List<object> results = response["results"] as List<object>;
List<T> list = new List<T>(); List<T> list = new List<T>();
foreach (object item in results) { foreach (object item in results) {
LCObjectData objectData = LCObjectData.Decode(item as Dictionary<string, object>); T obj = DecodeLCObject(item as Dictionary<string, object>);
T obj = LCObject.Create(ClassName) as T;
obj.Merge(objectData);
list.Add(obj); list.Add(obj);
} }
return list.AsReadOnly(); return list.AsReadOnly();
@ -412,5 +411,12 @@ namespace LeanCloud.Storage {
compositionQuery.ClassName = className; compositionQuery.ClassName = className;
return compositionQuery; return compositionQuery;
} }
private T DecodeLCObject(Dictionary<string, object> data) {
LCObjectData objectData = LCObjectData.Decode(data);
T obj = LCObject.Create(ClassName) as T;
obj.Merge(objectData);
return obj;
}
} }
} }