Beispiel #1
0
        public async Task Array()
        {
            // equal
            LCQuery <LCObject> query = new LCQuery <LCObject>("Book");

            query.WhereEqualTo("pages", 3);
            List <LCObject> results = await query.Find();

            results.ForEach(item => {
                List <object> pages = item["pages"] as List <object>;
                Assert.IsTrue(pages.Contains(3));
            });

            // contain all
            List <int> containAlls = new List <int> {
                1, 2, 3, 4, 5, 6, 7
            };

            query = new LCQuery <LCObject>("Book");
            query.WhereContainsAll("pages", containAlls);
            results = await query.Find();

            results.ForEach(item => {
                List <object> pages = item["pages"] as List <object>;
                pages.ForEach(i => {
                    Assert.IsTrue(pages.Contains(i));
                });
            });

            // contain in
            List <int> containIns = new List <int> {
                4, 5, 6
            };

            query = new LCQuery <LCObject>("Book");
            query.WhereContainedIn("pages", containIns);
            results = await query.Find();

            results.ForEach(item => {
                List <object> pages = item["pages"] as List <object>;
                bool f = false;
                containIns.ForEach(i => {
                    f |= pages.Contains(i);
                });
                Assert.IsTrue(f);
            });

            // size
            query = new LCQuery <LCObject>("Book");
            query.WhereSizeEqualTo("pages", 7);
            results = await query.Find();

            results.ForEach(item => {
                List <object> pages = item["pages"] as List <object>;
                Assert.AreEqual(pages.Count, 7);
            });
        }
Beispiel #2
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");
        }
Beispiel #3
0
        public async Task InQuery()
        {
            LCQuery <LCObject> worldQuery = new LCQuery <LCObject>("World");

            worldQuery.WhereEqualTo("content", "7788");
            LCQuery <LCObject> helloQuery = new LCQuery <LCObject>("Hello");

            helloQuery.WhereMatchesQuery("objectValue", worldQuery);
            helloQuery.Include("objectValue");
            ReadOnlyCollection <LCObject> hellos = await helloQuery.Find();

            Assert.Greater(hellos.Count, 0);
            foreach (LCObject item in hellos)
            {
                LCObject world = item["objectValue"] as LCObject;
                Assert.AreEqual(world["content"], "7788");
            }
        }
Beispiel #4
0
        public async Task WhereObjectEquals()
        {
            World world = new World();
            Hello hello = new Hello {
                World = world
            };
            await hello.Save();

            LCQuery <LCObject> worldQuery = new LCQuery <LCObject>("World");
            LCObject           queryWorld = await worldQuery.Get(world.ObjectId);

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

            helloQuery.WhereEqualTo("objectValue", queryWorld);
            LCObject queryHello = await helloQuery.First();

            TestContext.WriteLine(queryHello.ObjectId);
            Assert.AreEqual(queryHello.ObjectId, hello.ObjectId);
        }
Beispiel #5
0
        public async Task NotInQuery()
        {
            LCQuery <LCObject> worldQuery = new LCQuery <LCObject>("World");

            worldQuery.WhereEqualTo("content", "7788");
            LCQuery <LCObject> helloQuery = new LCQuery <LCObject>("Hello");

            helloQuery.WhereDoesNotMatchQuery("objectValue", worldQuery);
            helloQuery.Include("objectValue");
            ReadOnlyCollection <LCObject> hellos = await helloQuery.Find();

            Assert.Greater(hellos.Count, 0);
            foreach (LCObject item in hellos)
            {
                LCObject world = item["objectValue"] as LCObject;
                Assert.IsTrue(world == null ||
                              world["content"] == null ||
                              world["content"] as string != "7788");
            }
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            WriteLine("Hello World!");

            SingleThreadSynchronizationContext.Run(async() => {
                LCLogger.LogDelegate += Print;
                LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz",
                                         "NUKmuRbdAhg1vrb2wexYo1jo",
                                         "https://ikggdre2.lc-cn-n1-shared.com");

                await LCUser.Login("hello", "world");
                LCQuery <LCUser> userQuery = LCUser.GetQuery();
                userQuery.WhereEqualTo("username", "hello");
                LCLiveQuery userLiveQuery = await userQuery.Subscribe();
                userLiveQuery.OnLogin     = (user) => {
                    WriteLine($"login: {user.Username}");
                };

                LCQuery <LCObject> query = new LCQuery <LCObject>("Account");
                query.WhereGreaterThan("balance", 100);
                LCLiveQuery liveQuery = await query.Subscribe();
                liveQuery.OnCreate    = (obj) => {
                    WriteLine($"create: {obj}");
                };
                liveQuery.OnUpdate = (obj, keys) => {
                    WriteLine($"update: {obj}");
                    WriteLine(keys.Count);
                };
                liveQuery.OnDelete = (objId) => {
                    WriteLine($"delete: {objId}");
                };
                liveQuery.OnEnter = (obj, keys) => {
                    WriteLine($"enter: {obj}");
                    WriteLine(keys.Count);
                };
                liveQuery.OnLeave = (obj, keys) => {
                    WriteLine($"leave: {obj}");
                    WriteLine(keys.Count);
                };
            });
        }
Beispiel #7
0
        public async Task Login()
        {
            TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();

            await LCUser.Login("hello", "world");

            LCQuery <LCUser> userQuery = LCUser.GetQuery();

            userQuery.WhereEqualTo("username", "hello");
            LCLiveQuery userLiveQuery = await userQuery.Subscribe();

            userLiveQuery.OnLogin = (user) => {
                WriteLine($"login: {user}");
                tcs.SetResult(null);
            };

            // 模拟 REST API
            string             url     = "https://ikggdre2.lc-cn-n1-shared.com/1.1/login";
            HttpRequestMessage request = new HttpRequestMessage {
                RequestUri = new Uri(url),
                Method     = HttpMethod.Post
            };

            request.Headers.Add("X-LC-Id", "ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz");
            request.Headers.Add("X-LC-Key", "NUKmuRbdAhg1vrb2wexYo1jo");
            string content = JsonConvert.SerializeObject(new Dictionary <string, object> {
                { "username", "hello" },
                { "password", "world" }
            });
            StringContent requestContent = new StringContent(content);

            requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            request.Content = requestContent;
            HttpClient client = new HttpClient();
            await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

            await tcs.Task;
        }