Пример #1
0
        static async Task SaveBatches(Stack <LCBatch> batches)
        {
            while (batches.Count > 0)
            {
                LCBatch batch = batches.Pop();

                // 特殊处理 File 依赖
                IEnumerable <LCFile> dirtyFiles = batch.objects.Where(item => (item is LCFile) && item.IsDirty)
                                                  .Cast <LCFile>();
                foreach (LCFile file in dirtyFiles)
                {
                    await file.Save();
                }

                List <LCObject> dirtyObjects = batch.objects.Where(item => item.IsDirty)
                                               .ToList();
                if (dirtyObjects.Count == 0)
                {
                    continue;
                }

                List <Dictionary <string, object> > requestList = dirtyObjects.Select(item => {
                    string path = item.ObjectId == null ?
                                  $"/1.1/classes/{item.ClassName}" :
                                  $"/1.1/classes/{item.ClassName}/{item.ClassName}";
                    string method = item.ObjectId == null ? "POST" : "PUT";
                    Dictionary <string, object> body = LCEncoder.Encode(item.operationDict) as Dictionary <string, object>;
                    return(new Dictionary <string, object> {
                        { "path", path },
                        { "method", method },
                        { "body", body }
                    });
                }).ToList();

                Dictionary <string, object> data = new Dictionary <string, object> {
                    { "requests", LCEncoder.Encode(requestList) }
                };

                List <Dictionary <string, object> > results = await LCCore.HttpClient.Post <List <Dictionary <string, object> > >("batch", data : data);

                List <LCObjectData> resultList = results.Select(item => {
                    if (item.TryGetValue("error", out object error))
                    {
                        Dictionary <string, object> err = error as Dictionary <string, object>;
                        int code       = (int)err["code"];
                        string message = (string)err["error"];
                        throw new LCException(code, message as string);
                    }
                    return(LCObjectData.Decode(item["success"] as IDictionary));
                }).ToList();

                for (int i = 0; i < dirtyObjects.Count; i++)
                {
                    LCObject     obj     = dirtyObjects[i];
                    LCObjectData objData = resultList[i];
                    obj.Merge(objData);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Deserializes a JSON string to a LCObject.
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static LCObject ParseObject(string json)
        {
            LCObjectData objectData = LCObjectData.Decode(JsonConvert.DeserializeObject <Dictionary <string, object> >(json));
            LCObject     obj        = Create(objectData.ClassName);

            obj.Merge(objectData);
            return(obj);
        }
Пример #3
0
        /// <summary>
        /// Creates a reference to an existing LCObject.
        /// </summary>
        /// <param name="className">The className for the LCObject.</param>
        /// <param name="objectId">The object id for the LCObject.</param>
        /// <returns></returns>
        public static LCObject CreateWithoutData(string className, string objectId)
        {
            if (string.IsNullOrEmpty(objectId))
            {
                throw new ArgumentNullException(nameof(objectId));
            }
            LCObject obj = Create(className);

            obj.data.ObjectId = objectId;
            obj.IsDirty       = false;
            return(obj);
        }
Пример #4
0
        public async Task <LCSearchResponse <T> > Find()
        {
            string path = "search/select";
            Dictionary <string, object> queryParams = new Dictionary <string, object> {
                { "clazz", className },
                { "limit", limit },
                { "skip", skip }
            };

            if (queryString != null)
            {
                queryParams["q"] = queryString;
            }
            if (highlights != null && highlights.Count() > 0)
            {
                queryParams["highlights"] = string.Join(",", highlights);
            }
            if (includeKeys != null && includeKeys.Count() > 0)
            {
                queryParams["include"] = string.Join(",", includeKeys);
            }
            if (!string.IsNullOrEmpty(sid))
            {
                queryParams["sid"] = sid;
            }
            if (orders != null && orders.Count() > 0)
            {
                queryParams["order"] = string.Join(",", orders);
            }
            if (sortBuilder != null)
            {
                queryParams["sort"] = sortBuilder.Build();
            }

            Dictionary <string, object> response = await LCCore.HttpClient.Get <Dictionary <string, object> >(path, queryParams : queryParams);

            LCSearchResponse <T> ret = new LCSearchResponse <T>();

            ret.Hits = (int)response["hits"];
            ret.Sid  = (string)response["sid"];
            List <object> results = response["results"] as List <object>;
            List <T>      list    = new List <T>();

            foreach (object data in results)
            {
                LCObjectData objectData = LCObjectData.Decode(data as Dictionary <string, object>);
                T            obj        = LCObject.Create(className) as T;
                obj.Merge(objectData);
                list.Add(obj);
            }
            ret.Results = list.AsReadOnly();
            return(ret);
        }
Пример #5
0
        /// <summary>
        /// Removes a <see cref="LCRelation{T}"/> value for a key.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void RemoveRelation(string key, LCObject value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            LCRemoveRelationOperation op = new LCRemoveRelationOperation(value);

            ApplyOperation(key, op);
        }
Пример #6
0
        public async Task <List <T> > Find()
        {
            string path = $"classes/{ClassName}";
            Dictionary <string, object> parameters = BuildParams();
            Dictionary <string, object> response   = await LeanCloud.HttpClient.Get <Dictionary <string, object> >(path, queryParams : parameters);

            List <object> results = response["results"] as List <object>;
            List <T>      list    = new List <T>();

            foreach (object item in results)
            {
                LCObjectData objectData = LCObjectData.Decode(item as Dictionary <string, object>);
                T            obj        = LCObject.Create(ClassName) as T;
                obj.Merge(objectData);
                list.Add(obj);
            }
            return(list);
        }
Пример #7
0
        /// <summary>
        /// Requests to add a friend.
        /// </summary>
        /// <param name="userId">The user id to add.</param>
        /// <param name="attributes">The additional attributes for the friendship.</param>
        /// <returns></returns>
        public static async Task Request(string userId, Dictionary <string, object> attributes = null)
        {
            LCUser user = await LCUser.GetCurrent();

            if (user == null)
            {
                throw new ArgumentNullException("current user");
            }
            string   path   = "users/friendshipRequests";
            LCObject friend = LCObject.CreateWithoutData("_User", userId);
            Dictionary <string, object> data = new Dictionary <string, object> {
                { "user", LCEncoder.EncodeLCObject(user) },
                { "friend", LCEncoder.EncodeLCObject(friend) },
            };

            if (attributes != null)
            {
                data["friendship"] = attributes;
            }
            await LCCore.HttpClient.Post <Dictionary <string, object> >(path, data : data);
        }
Пример #8
0
 /// <summary>
 /// 相关
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public LCQuery <T> WhereRelatedTo(LCObject parent, string key)
 {
     condition.WhereRelatedTo(parent, key);
     return(this);
 }
Пример #9
0
        /// <summary>
        /// Gets the followers and followees of the currently logged in user.
        /// </summary>
        /// <param name="includeFollower"></param>
        /// <param name="includeFollowee"></param>
        /// <param name="returnCount"></param>
        /// <returns></returns>
        public async Task <LCFollowersAndFollowees> GetFollowersAndFollowees(bool includeFollower = false,
                                                                             bool includeFollowee = false, bool returnCount = false)
        {
            Dictionary <string, object> queryParams = new Dictionary <string, object>();

            if (returnCount)
            {
                queryParams["count"] = 1;
            }
            if (includeFollower || includeFollowee)
            {
                List <string> includes = new List <string>();
                if (includeFollower)
                {
                    includes.Add("follower");
                }
                if (includeFollowee)
                {
                    includes.Add("followee");
                }
                queryParams["include"] = string.Join(",", includes);
            }
            string path = $"users/{ObjectId}/followersAndFollowees";
            Dictionary <string, object> response = await LCCore.HttpClient.Get <Dictionary <string, object> >(path,
                                                                                                              queryParams : queryParams);

            LCFollowersAndFollowees result = new LCFollowersAndFollowees();

            if (response.TryGetValue("followers", out object followersObj) &&
                (followersObj is List <object> followers))
            {
                result.Followers = new List <LCObject>();
                foreach (object followerObj in followers)
                {
                    LCObjectData objectData = LCObjectData.Decode(followerObj as IDictionary);
                    LCObject     follower   = new LCObject("_Follower");
                    follower.Merge(objectData);
                    result.Followers.Add(follower);
                }
            }
            if (response.TryGetValue("followees", out object followeesObj) &&
                (followeesObj is List <object> followees))
            {
                result.Followees = new List <LCObject>();
                foreach (object followeeObj in followees)
                {
                    LCObjectData objectData = LCObjectData.Decode(followeeObj as IDictionary);
                    LCObject     followee   = new LCObject("_Followee");
                    followee.Merge(objectData);
                    result.Followees.Add(followee);
                }
            }
            if (response.TryGetValue("followers_count", out object followersCountObj) &&
                (followersCountObj is int followersCount))
            {
                result.FollowersCount = followersCount;
            }
            if (response.TryGetValue("followees_count", out object followeesCountObj) &&
                (followeesCountObj is int followeesCount))
            {
                result.FolloweesCount = followeesCount;
            }
            return(result);
        }