From 83ad15a8ecdfb7b607afd1221fd99ab51aac0f4c Mon Sep 17 00:00:00 2001 From: oneRain Date: Thu, 27 Feb 2020 12:31:48 +0800 Subject: [PATCH] =?UTF-8?q?*=20RelationTest.cs:=20chore:=20=E5=AE=8C?= =?UTF-8?q?=E5=96=84=20Relation=20=E5=92=8C=20Role=20=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=92=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * LCRole.cs: * LCObject.cs: * LCRelation.cs: * RoleTest.cs: * LCDecoder.cs: * LCEncoder.cs: * LCAddRelationOperation.cs: * LCRemoveRelationOperation.cs: --- Storage/Storage.Test/RelationTest.cs | 76 ++++++++++++------- Storage/Storage.Test/RoleTest.cs | 47 ++++++++---- Storage/Storage/Internal/Codec/LCDecoder.cs | 2 +- Storage/Storage/Internal/Codec/LCEncoder.cs | 2 +- .../Operation/LCAddRelationOperation.cs | 2 +- .../Operation/LCRemoveRelationOperation.cs | 2 +- Storage/Storage/LCObject.cs | 22 ++++++ Storage/Storage/LCRelation.cs | 27 ++++++- Storage/Storage/LCRole.cs | 60 ++++++++++++++- 9 files changed, 189 insertions(+), 51 deletions(-) diff --git a/Storage/Storage.Test/RelationTest.cs b/Storage/Storage.Test/RelationTest.cs index 4640fc9..6ac0904 100644 --- a/Storage/Storage.Test/RelationTest.cs +++ b/Storage/Storage.Test/RelationTest.cs @@ -1,46 +1,66 @@ using NUnit.Framework; -using LeanCloud; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; +using LeanCloud.Storage; namespace LeanCloud.Test { public class RelationTest { [SetUp] public void SetUp() { - Utils.InitNorthChina(); + Logger.LogDelegate += Utils.Print; + LeanCloud.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + } + + [TearDown] + public void TearDown() { + Logger.LogDelegate -= Utils.Print; } [Test] - public async Task CreateRelation() { - AVObject tag1 = new AVObject("Tag") { - ["name"] = "hello" - }; - await tag1.SaveAsync(); - AVObject tag2 = new AVObject("Tag") { - { "name", "world" } - }; - await tag2.SaveAsync(); - AVObject todo = new AVObject("Todo"); - AVRelation relation = todo.GetRelation("tags"); - relation.Add(tag1); - relation.Add(tag2); - await todo.SaveAsync(); + 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 relation = parent["children"] as LCRelation; + LCQuery 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); } [Test] - public async Task QueryRelation() { - AVQuery query = new AVQuery("Todo"); - query.OrderByDescending("createdAt"); - AVObject todo = await query.FirstAsync(); - AVRelation relation = todo.GetRelation("tags"); - AVQuery tagQuery = relation.Query; - IEnumerable tags = await tagQuery.FindAsync(); - Assert.Greater(tags.Count(), 0); - TestContext.Out.WriteLine($"count: {tags.Count()}"); - foreach (AVObject tag in tags) { - TestContext.Out.WriteLine($"{tag.ObjectId}, {tag["name"]}"); - } + public async Task Query() { + LCQuery query = new LCQuery("Parent"); + LCObject parent = await query.Get("5e13112021b47e0070ed0922"); + LCRelation relation = parent["children"] as LCRelation; + + TestContext.WriteLine(relation.Key); + TestContext.WriteLine(relation.Parent); + TestContext.WriteLine(relation.TargetClass); + + Assert.NotNull(relation.Key); + Assert.NotNull(relation.Parent); + Assert.NotNull(relation.TargetClass); + + LCQuery relationQuery = relation.Query; + List list = await relationQuery.Find(); + list.ForEach(item => { + TestContext.WriteLine(item.ObjectId); + Assert.NotNull(item.ObjectId); + }); } } } diff --git a/Storage/Storage.Test/RoleTest.cs b/Storage/Storage.Test/RoleTest.cs index afa460b..ccabcef 100644 --- a/Storage/Storage.Test/RoleTest.cs +++ b/Storage/Storage.Test/RoleTest.cs @@ -1,28 +1,49 @@ using NUnit.Framework; +using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; -using LeanCloud; +using System.Linq; +using LeanCloud.Storage; namespace LeanCloud.Test { [TestFixture] public class RoleTest { [SetUp] public void SetUp() { - Utils.InitNorthChina(true); + Logger.LogDelegate += Utils.Print; + LeanCloud.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + } + + [TearDown] + public void TearDown() { + Logger.LogDelegate -= Utils.Print; } [Test] - public async Task GetUsersFromRole() { - AVQuery query = new AVQuery(); - AVRole role = await query.FirstAsync(); - AVQuery userQuery = role.Users.Query; - IEnumerable users = await userQuery.FindAsync(); - Assert.Greater(users.Count(), 0); - TestContext.Out.WriteLine($"count: {users.Count()}"); - foreach (AVUser user in users) { - TestContext.Out.WriteLine($"{user.ObjectId}, {user.Username}"); - } + public async Task NewRole() { + LCUser currentUser = await LCUser.Login("game", "play"); + LCACL acl = new LCACL(); + acl.PublicReadAccess = true; + acl.SetUserWriteAccess(currentUser, true); + string name = $"role_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"; + LCRole role = LCRole.Create(name, acl); + role.AddRelation("users", currentUser); + await role.Save(); + } + + [Test] + public async Task Query() { + LCQuery query = LCRole.GetQuery(); + List list = await query.Find(); + list.ForEach(item => { + TestContext.WriteLine($"{item.ObjectId} : {item.Name}"); + Assert.NotNull(item.ObjectId); + Assert.NotNull(item.Name); + TestContext.WriteLine(item.Roles.GetType()); + TestContext.WriteLine(item.Users.GetType()); + Assert.IsTrue(item.Roles is LCRelation); + Assert.IsTrue(item.Users is LCRelation); + }); } } } diff --git a/Storage/Storage/Internal/Codec/LCDecoder.cs b/Storage/Storage/Internal/Codec/LCDecoder.cs index 16307c1..06d0a50 100644 --- a/Storage/Storage/Internal/Codec/LCDecoder.cs +++ b/Storage/Storage/Internal/Codec/LCDecoder.cs @@ -63,7 +63,7 @@ namespace LeanCloud.Storage.Internal.Codec { static LCRelation DecodeRelation(IDictionary dict) { LCRelation relation = new LCRelation(); - relation.targetClass = dict["className"].ToString(); + relation.TargetClass = dict["className"].ToString(); return relation; } diff --git a/Storage/Storage/Internal/Codec/LCEncoder.cs b/Storage/Storage/Internal/Codec/LCEncoder.cs index 24cae00..efe49c1 100644 --- a/Storage/Storage/Internal/Codec/LCEncoder.cs +++ b/Storage/Storage/Internal/Codec/LCEncoder.cs @@ -100,7 +100,7 @@ namespace LeanCloud.Storage.Internal.Codec { static object EncodeRelation(LCRelation relation) { return new Dictionary { { "__type", "Relation" }, - { "className", relation.targetClass } + { "className", relation.TargetClass } }; } diff --git a/Storage/Storage/Internal/Operation/LCAddRelationOperation.cs b/Storage/Storage/Internal/Operation/LCAddRelationOperation.cs index da5c960..b0668e1 100644 --- a/Storage/Storage/Internal/Operation/LCAddRelationOperation.cs +++ b/Storage/Storage/Internal/Operation/LCAddRelationOperation.cs @@ -32,7 +32,7 @@ namespace LeanCloud.Storage.Internal.Operation { public object Apply(object oldValue, string key) { LCRelation relation = new LCRelation(); - relation.targetClass = valueList[0].ClassName; + relation.TargetClass = valueList[0].ClassName; return relation; } diff --git a/Storage/Storage/Internal/Operation/LCRemoveRelationOperation.cs b/Storage/Storage/Internal/Operation/LCRemoveRelationOperation.cs index f143b85..ab92c1e 100644 --- a/Storage/Storage/Internal/Operation/LCRemoveRelationOperation.cs +++ b/Storage/Storage/Internal/Operation/LCRemoveRelationOperation.cs @@ -31,7 +31,7 @@ namespace LeanCloud.Storage.Internal.Operation { public object Apply(object oldValue, string key) { LCRelation relation = new LCRelation(); - relation.targetClass = valueList[0].ClassName; + relation.TargetClass = valueList[0].ClassName; return relation; } diff --git a/Storage/Storage/LCObject.cs b/Storage/Storage/LCObject.cs index 059ab0f..7019726 100644 --- a/Storage/Storage/LCObject.cs +++ b/Storage/Storage/LCObject.cs @@ -140,6 +140,28 @@ namespace LeanCloud.Storage { ApplyOperation(key, deleteOp); } + public void AddRelation(string key, LCObject value) { + if (string.IsNullOrEmpty(key)) { + throw new ArgumentNullException(nameof(key)); + } + if (value == null) { + throw new ArgumentNullException(nameof(value)); + } + LCAddRelationOperation op = new LCAddRelationOperation(new List { value }); + ApplyOperation(key, op); + } + + 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); + } + static async Task SaveBatches(Stack batches) { while (batches.Count > 0) { LCBatch batch = batches.Pop(); diff --git a/Storage/Storage/LCRelation.cs b/Storage/Storage/LCRelation.cs index 58031a3..9552bc7 100644 --- a/Storage/Storage/LCRelation.cs +++ b/Storage/Storage/LCRelation.cs @@ -1,20 +1,41 @@ -using System; - +/// +/// 关系类 +/// namespace LeanCloud.Storage { public class LCRelation where T : LCObject { + /// + /// 字段名 + /// public string Key { get; set; } + /// + /// 父对象 + /// public LCObject Parent { get; set; } - public string targetClass { + /// + /// 关联类型名 + /// + public string TargetClass { get; set; } public LCRelation() { } + + /// + /// 获取 Relation 的查询对象 + /// + public LCQuery Query { + get { + LCQuery query = new LCQuery(TargetClass); + query.WhereRelatedTo(Parent, Key); + return query; + } + } } } diff --git a/Storage/Storage/LCRole.cs b/Storage/Storage/LCRole.cs index 53d955e..39a344e 100644 --- a/Storage/Storage/LCRole.cs +++ b/Storage/Storage/LCRole.cs @@ -1,10 +1,64 @@ -using System; - -namespace LeanCloud.Storage { +namespace LeanCloud.Storage { + /// + /// 角色 + /// public class LCRole : LCObject { public const string CLASS_NAME = "_Role"; + /// + /// 名字 + /// + public string Name { + get { + return this["name"] as string; + } set { + this["name"] = value; + } + } + + /// + /// 关联角色 + /// + public LCRelation Roles { + get { + LCRelation roles = this["roles"] as LCRelation; + return new LCRelation { + Parent = roles.Parent, + Key = "roles" + }; + } + } + + /// + /// 关联用户 + /// + public LCRelation Users { + get { + LCRelation users = this["users"] as LCRelation; + return new LCRelation { + Parent = users.Parent, + Key = "users" + }; + } + } + public LCRole() : base(CLASS_NAME) { } + + public static LCRole Create(string name, LCACL acl) { + LCRole role = new LCRole() { + Name = name, + ACL = acl + }; + return role; + } + + /// + /// 获取角色查询对象 + /// + /// + public static LCQuery GetQuery() { + return new LCQuery(CLASS_NAME); + } } }