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); }
public async Task Delete() { LCObject world = new LCObject("World"); await world.Save(); await world.Delete(); }
public async Task Get() { LCQuery <LCObject> query = new LCQuery <LCObject>("Account"); LCObject account = await query.Get("5e0d9f7fd4b56c008e5d048a"); Assert.AreEqual(account["balance"], 400); }
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); }
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); }
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); }
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); } }
public async Task Attributes() { LCObject followee = (await GetFriends()).First(); followee["group"] = "friend"; await followee.Save(); }
public async Task First() { LCQuery <LCObject> query = new LCQuery <LCObject>("Account"); LCObject account = await query.First(); Assert.NotNull(account.ObjectId); }
static object EncodeLCObject(LCObject obj) { return(new Dictionary <string, object> { { "__type", "Pointer" }, { "className", obj.ClassName }, { "objectId", obj.ObjectId } }); }
public static T ToImpl(LCObject obj) { var retval = new T { Handle = obj.Handle }; retval.ObjectType = retval.DesignatedType(); return(retval); }
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); }
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); } }
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); }
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); }
public async Task Delete() { LCObject.RegisterSubclass <Account>("Account", () => new Account()); Account account = new Account() { Balance = 1024 }; await account.Save(); await account.Delete(); }
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"); }
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); }
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"); }
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); }
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); }
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"); }
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"); }
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); } }
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)); } }
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(); }
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); }
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); } }
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); }
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); }