예제 #1
0
        public async Task AddAndRemove()
        {
            LCObject parent = new LCObject("Parent");
            LCObject c1     = new LCObject("Child");

            parent.AddRelation("children", c1);
            LCObject c2 = new LCObject("Child");

            parent.AddRelation("children", c2);
            await parent.Save();

            LCRelation <LCObject> relation = parent["children"] as LCRelation <LCObject>;
            LCQuery <LCObject>    query    = relation.Query;
            int count = await query.Count();

            TestContext.WriteLine($"count: {count}");
            Assert.AreEqual(count, 2);

            parent.RemoveRelation("children", c2);
            await parent.Save();

            int count2 = await query.Count();

            TestContext.WriteLine($"count: {count2}");
            Assert.AreEqual(count2, 1);
        }
예제 #2
0
        public async Task Delete()
        {
            LCObject world = new LCObject("World");
            await world.Save();

            await world.Delete();
        }
예제 #3
0
        public async Task Get()
        {
            LCQuery <LCObject> query   = new LCQuery <LCObject>("Account");
            LCObject           account = await query.Get("5e0d9f7fd4b56c008e5d048a");

            Assert.AreEqual(account["balance"], 400);
        }
예제 #4
0
        public async Task Geo()
        {
            LCObject   obj      = new LCObject("Todo");
            LCGeoPoint location = new LCGeoPoint(39.9, 116.4);

            obj["location"] = location;
            await obj.Save();

            // near
            LCQuery <LCObject> query = new LCQuery <LCObject>("Todo");
            LCGeoPoint         point = new LCGeoPoint(39.91, 116.41);

            query.WhereNear("location", point);
            ReadOnlyCollection <LCObject> results = await query.Find();

            Assert.Greater(results.Count, 0);

            // in box
            query = new LCQuery <LCObject>("Todo");
            LCGeoPoint southwest = new LCGeoPoint(30, 115);
            LCGeoPoint northeast = new LCGeoPoint(40, 118);

            query.WhereWithinGeoBox("location", southwest, northeast);
            results = await query.Find();

            Assert.Greater(results.Count, 0);
        }
예제 #5
0
        public async Task Query()
        {
            LCObject   geoObj = new LCObject("GeoObj");
            Random     random = new Random();
            LCGeoPoint p1     = new LCGeoPoint(-90 + random.NextDouble() * 180, -180 + random.NextDouble() * 360);

            geoObj["location"] = p1;
            await geoObj.Save();

            LCGeoPoint p2 = new LCGeoPoint(p1.Latitude + 0.01, p1.Longitude + 0.01);

            double km = p1.KilometersTo(p2);

            TestContext.WriteLine($"km: {km}, {Math.Ceiling(km)}");
            LCQuery <LCObject> query = new LCQuery <LCObject>("GeoObj");

            query.WhereWithinKilometers("location", p2, Math.Ceiling(km));
            Assert.Greater((await query.Find()).Count, 0);

            double miles = p1.MilesTo(p2);

            query = new LCQuery <LCObject>("GeoObj");
            query.WhereWithinMiles("location", p2, Math.Ceiling(miles));
            Assert.Greater((await query.Find()).Count, 0);

            double radians = p1.RadiansTo(p2);

            query = new LCQuery <LCObject>("GeoObj");
            query.WhereWithinRadians("location", p2, Math.Ceiling(radians));
            Assert.Greater((await query.Find()).Count, 0);
        }
예제 #6
0
        public async Task UserReadAndWrite()
        {
            await LCUser.Login("hello", "world");

            LCObject account     = new LCObject("Account");
            LCUser   currentUser = await LCUser.GetCurrent();

            LCACL acl = LCACL.CreateWithOwner(currentUser);

            account.ACL        = acl;
            account["balance"] = 512;
            await account.Save();

            Assert.IsTrue(acl.GetUserReadAccess(currentUser));
            Assert.IsTrue(acl.GetUserWriteAccess(currentUser));

            LCQuery <LCObject> query  = new LCQuery <LCObject>("Account");
            LCObject           result = await query.Get(account.ObjectId);

            TestContext.WriteLine(result.ObjectId);
            Assert.NotNull(result.ObjectId);

            await LCUser.Logout();

            result = await query.Get(account.ObjectId);

            Assert.IsNull(result);
        }
예제 #7
0
        public async Task UserReadAndWrite()
        {
            await LCUser.Login(TestPhone, TestPhone);

            account = new Account();
            LCUser currentUser = await LCUser.GetCurrent();

            account.ACL     = LCACL.CreateWithOwner(currentUser);
            account.Balance = 512;
            await account.Save();

            Assert.IsTrue(account.ACL.GetUserReadAccess(currentUser));
            Assert.IsTrue(account.ACL.GetUserWriteAccess(currentUser));

            LCQuery <LCObject> query  = new LCQuery <LCObject>("Account");
            LCObject           result = await query.Get(account.ObjectId);

            TestContext.WriteLine(result.ObjectId);
            Assert.NotNull(result.ObjectId);

            await LCUser.Logout();

            try {
                await query.Get(account.ObjectId);
            } catch (LCException e) {
                Assert.AreEqual(e.Code, 403);
            }
        }
예제 #8
0
        public async Task Attributes()
        {
            LCObject followee = (await GetFriends()).First();

            followee["group"] = "friend";
            await followee.Save();
        }
예제 #9
0
        public async Task First()
        {
            LCQuery <LCObject> query   = new LCQuery <LCObject>("Account");
            LCObject           account = await query.First();

            Assert.NotNull(account.ObjectId);
        }
예제 #10
0
 static object EncodeLCObject(LCObject obj)
 {
     return(new Dictionary <string, object> {
         { "__type", "Pointer" },
         { "className", obj.ClassName },
         { "objectId", obj.ObjectId }
     });
 }
예제 #11
0
            public static T ToImpl(LCObject obj)
            {
                var retval = new T {
                    Handle = obj.Handle
                };

                retval.ObjectType = retval.DesignatedType();
                return(retval);
            }
예제 #12
0
        static LCObject DecodeObject(IDictionary dict)
        {
            string       className  = dict["className"].ToString();
            LCObject     obj        = LCObject.Create(className);
            LCObjectData objectData = LCObjectData.Decode(dict as Dictionary <string, object>);

            obj.Merge(objectData);
            return(obj);
        }
예제 #13
0
 public void UpdateItemData(object data)
 {
     if (data is LCObject obj)
     {
         friendship = obj;
         LCUser friend = friendship["followee"] as LCUser;
         nameText.text = friend.GetNickname();
         gameObject.SetActive(true);
     }
 }
예제 #14
0
        public async Task Query()
        {
            await LCUser.Login("game", "play");

            LCQuery <LCObject> query   = new LCQuery <LCObject>("Account");
            LCObject           account = await query.Get("5e144525dd3c13006a8f8de2");

            TestContext.WriteLine(account.ObjectId);
            Assert.NotNull(account.ObjectId);
        }
예제 #15
0
        public async Task Create()
        {
            LCObject.RegisterSubclass <Account>("Account", () => new Account());
            Account account = new Account();

            account.Balance = 1000;
            await account.Save();

            TestContext.WriteLine(account.ObjectId);
            Assert.NotNull(account.ObjectId);
        }
예제 #16
0
        public async Task Delete()
        {
            LCObject.RegisterSubclass <Account>("Account", () => new Account());
            Account account = new Account()
            {
                Balance = 1024
            };
            await account.Save();

            await account.Delete();
        }
예제 #17
0
        public async Task RelateObject()
        {
            LCUser user = await LCUser.LoginByMobilePhoneNumber("15101006007", "112358");

            LCObject account = new LCObject("Account");

            account["user"] = user;
            await account.Save();

            Assert.AreEqual(user.ObjectId, "5e0d5c667d5774006a5c1177");
        }
예제 #18
0
        public async Task DeleteAll()
        {
            List <World> list = new List <World> {
                new World(),
                new World(),
                new World(),
                new World(),
            };
            await LCObject.SaveAll(list);

            await LCObject.DeleteAll(list);
        }
예제 #19
0
        public async Task Include()
        {
            LCQuery <LCObject> query = new LCQuery <LCObject>("Hello");

            query.Include("objectValue");
            LCObject hello = await query.Get("5e0d55aedd3c13006a53cd87");

            LCObject world = hello["objectValue"] as LCObject;

            TestContext.WriteLine(world["content"]);
            Assert.AreEqual(world["content"], "7788");
        }
예제 #20
0
        private static async Task <object> Invoke(MethodInfo mi, Dictionary <string, object> dict)
        {
            LCObjectData objectData = LCObjectData.Decode(dict["object"] as Dictionary <string, object>);

            objectData.ClassName = "_User";

            LCObject user = LCObject.Create("_User");

            user.Merge(objectData);

            return(await LCEngine.Invoke(mi, new object[] { user }) as LCObject);
        }
예제 #21
0
        public async Task DeleteAll()
        {
            List <LCObject> list = new List <LCObject> {
                new LCObject("World"),
                new LCObject("World"),
                new LCObject("World"),
                new LCObject("World")
            };
            await LCObject.SaveAll(list);

            await LCObject.DeleteAll(list);
        }
예제 #22
0
        public async Task Fetch()
        {
            LCObject hello = LCObject.CreateWithoutData("Hello", "5e14392743c257006fb769d5");
            await hello.Fetch(includes : new List <string> {
                "objectValue"
            });

            LCObject world = hello["objectValue"] as LCObject;

            TestContext.WriteLine(world["content"]);
            Assert.AreEqual(world["content"], "7788");
        }
예제 #23
0
        public async Task WhereObjectEquals()
        {
            LCQuery <LCObject> worldQuery = new LCQuery <LCObject>("World");
            LCObject           world      = await worldQuery.Get("5e0d55ae21460d006a1ec931");

            LCQuery <LCObject> helloQuery = new LCQuery <LCObject>("Hello");

            helloQuery.WhereEqualTo("objectValue", world);
            LCObject hello = await helloQuery.First();

            TestContext.WriteLine(hello.ObjectId);
            Assert.AreEqual(hello.ObjectId, "5e0d55aedd3c13006a53cd87");
        }
예제 #24
0
        public async Task FetchAll()
        {
            LCQuery <LCObject>     query = new LCQuery <LCObject>("Hello");
            IEnumerable <string>   ids   = (await query.Find()).Select(obj => obj.ObjectId);
            IEnumerable <LCObject> list  = ids.Select(id => LCObject.CreateWithoutData("Hello", id));
            await LCObject.FetchAll(list);

            Assert.Greater(list.Count(), 0);
            foreach (LCObject obj in list)
            {
                Assert.NotNull(obj.CreatedAt);
            }
        }
예제 #25
0
        public async Task <object> Hook(string className, string hookName, JsonElement body)
        {
            try {
                LCLogger.Debug($"Hook: {className}#{hookName}");
                LCLogger.Debug(body.ToString());

                LCEngine.CheckHookKey(Request);

                string classHookName = GetClassHookName(className, hookName);
                if (ClassHooks.TryGetValue(classHookName, out MethodInfo mi))
                {
                    Dictionary <string, object> data = LCEngine.Decode(body);

                    LCObjectData objectData = LCObjectData.Decode(data["object"] as Dictionary <string, object>);
                    objectData.ClassName = className;
                    LCObject obj = LCObject.Create(className);
                    obj.Merge(objectData);

                    // 避免死循环
                    if (hookName.StartsWith("before"))
                    {
                        obj.DisableBeforeHook();
                    }
                    else
                    {
                        obj.DisableAfterHook();
                    }

                    LCEngine.InitRequestContext(Request);

                    LCUser user = null;
                    if (data.TryGetValue("user", out object userObj) &&
                        userObj != null)
                    {
                        user = new LCUser();
                        user.Merge(LCObjectData.Decode(userObj as Dictionary <string, object>));
                        LCEngineRequestContext.CurrentUser = user;
                    }

                    LCObject result = await LCEngine.Invoke(mi, new object[] { obj }) as LCObject;

                    if (result != null)
                    {
                        return(LCCloud.Encode(result));
                    }
                }
                return(body);
            } catch (Exception e) {
                return(StatusCode(500, e.Message));
            }
        }
예제 #26
0
        public async Task Setup()
        {
            LCLogger.LogDelegate += Print;
            LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz",
                                     "NUKmuRbdAhg1vrb2wexYo1jo",
                                     "https://ikggdre2.lc-cn-n1-shared.com");

            LCObject.RegisterSubclass("Account", () => new Account());

            LCQuery <LCObject> query = new LCQuery <LCObject>("Account");

            query.WhereGreaterThan("balance", 100);
            liveQuery = await query.Subscribe();
        }
예제 #27
0
        public async Task GetObject()
        {
            LCObject hello = new LCObject("Hello");
            await hello.Save();

            object reponse = await LCCloud.RPC("getObject", new Dictionary <string, object> {
                { "className", "Hello" },
                { "id", hello.ObjectId }
            });

            LCObject obj = reponse as LCObject;

            Assert.AreEqual(obj.ObjectId, hello.ObjectId);
        }
예제 #28
0
        public async Task GetObjectMap()
        {
            object response = await LCCloud.RPC("getObjectMap");

            Dictionary <string, object> dict = response as Dictionary <string, object>;

            TestContext.WriteLine(dict.Count);
            Assert.Greater(dict.Count, 0);
            foreach (KeyValuePair <string, object> kv in dict)
            {
                LCObject obj = kv.Value as LCObject;
                Assert.AreEqual(kv.Key, obj.ObjectId);
            }
        }
예제 #29
0
        public async Task PrivateReadAndWrite()
        {
            LCObject account = new LCObject("Account");
            LCACL    acl     = new LCACL();

            acl.PublicReadAccess  = false;
            acl.PublicWriteAccess = false;
            account.ACL           = acl;
            account["balance"]    = 1024;
            await account.Save();

            Assert.IsFalse(acl.PublicReadAccess);
            Assert.IsFalse(acl.PublicWriteAccess);
        }
예제 #30
0
        public async Task Decrement()
        {
            LCQuery <LCObject> query   = new LCQuery <LCObject>("Account");
            LCObject           account = await query.Get("5e154a5143c257006fbff63f");

            TestContext.WriteLine(account["balance"]);
            int balance = (int)account["balance"];

            account.Increment("balance", -10);
            await account.Save();

            TestContext.WriteLine(account["balance"]);
            Assert.AreEqual((int)account["balance"], balance - 10);
        }