From 82abbc30c1c589d64cc2151a77143548fedb8552 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Wed, 31 Oct 2018 16:03:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E7=AE=A1=E7=90=86=E9=A1=B5=E9=9D=A2=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9=E9=83=A8=E9=97=A8=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/GroupsController.cs | 10 ++-- Bootstrap.DataAccess.MongoDB/Group.cs | 47 ++++++++++++++++--- .../MongoDbAccessManager.cs | 4 +- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/GroupsController.cs b/Bootstrap.Admin/Controllers/Api/GroupsController.cs index 52fe15da..72d6db33 100644 --- a/Bootstrap.Admin/Controllers/Api/GroupsController.cs +++ b/Bootstrap.Admin/Controllers/Api/GroupsController.cs @@ -62,21 +62,21 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPost("{id}")] - public IEnumerable Post(string id, [FromQuery]string type) + public IEnumerable Post(string id, [FromQuery]string type) { - var ret = new List(); + IEnumerable ret = new List(); switch (type) { case "user": - ret = GroupHelper.RetrieveGroupsByUserId(id).ToList(); + ret = GroupHelper.RetrieveGroupsByUserId(id); break; case "role": - ret = GroupHelper.RetrieveGroupsByRoleId(id).ToList(); + ret = GroupHelper.RetrieveGroupsByRoleId(id); break; default: break; } - return ret; + return ret.Select(p => new { p.Id, p.Checked, p.GroupName, p.Description }); } /// diff --git a/Bootstrap.DataAccess.MongoDB/Group.cs b/Bootstrap.DataAccess.MongoDB/Group.cs index 8f5e149f..b7acebab 100644 --- a/Bootstrap.DataAccess.MongoDB/Group.cs +++ b/Bootstrap.DataAccess.MongoDB/Group.cs @@ -9,13 +9,18 @@ namespace Bootstrap.DataAccess.MongoDB /// public class Group : DataAccess.Group { + /// + /// + /// + public IEnumerable Roles { get; set; } + /// /// /// /// public override IEnumerable RetrieveGroups() { - return MongoDbAccessManager.Groups.Find(FilterDefinition.Empty).ToList(); + return MongoDbAccessManager.Groups.Find(FilterDefinition.Empty).ToList(); } /// @@ -28,12 +33,12 @@ namespace Bootstrap.DataAccess.MongoDB if (p.Id == "0") { p.Id = null; - MongoDbAccessManager.Groups.InsertOne(p); + MongoDbAccessManager.Groups.InsertOne(p as Group); return true; } else { - MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description)); + MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description)); return true; } } @@ -45,10 +50,10 @@ namespace Bootstrap.DataAccess.MongoDB /// public override bool DeleteGroup(IEnumerable value) { - var list = new List>(); + var list = new List>(); foreach (var id in value) { - list.Add(new DeleteOneModel(Builders.Filter.Eq(g => g.Id, id))); + list.Add(new DeleteOneModel(Builders.Filter.Eq(g => g.Id, id))); } MongoDbAccessManager.Groups.BulkWrite(list); return true; @@ -79,6 +84,18 @@ namespace Bootstrap.DataAccess.MongoDB return true; } + /// + /// + /// + /// + /// + public override IEnumerable RetrieveGroupsByRoleId(string roleId) + { + var groups = GroupHelper.RetrieveGroups().Cast().ToList(); + groups.ForEach(p => p.Checked = (p.Roles != null && p.Roles.Contains(roleId)) ? "checked" : ""); + return groups; + } + /// /// /// @@ -87,7 +104,25 @@ namespace Bootstrap.DataAccess.MongoDB /// public override bool SaveGroupsByRoleId(string roleId, IEnumerable groupIds) { - return base.SaveGroupsByRoleId(roleId, groupIds); + var groups = MongoDbAccessManager.Groups.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList(); + + // Remove roles + groups.ForEach(p => + { + var roles = p.Roles == null ? new List() : p.Roles.ToList(); + roles.Remove(roleId); + MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Roles, roles)); + }); + + groups = MongoDbAccessManager.Groups.Find(md => groupIds.Contains(md.Id)).ToList(); + // Add roles + groups.ForEach(p => + { + var roles = p.Roles == null ? new List() : p.Roles.ToList(); + roles.Add(roleId); + MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Roles, roles)); + }); + return true; } } } diff --git a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs index 2398a97f..53e226f7 100644 --- a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs +++ b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs @@ -84,11 +84,11 @@ namespace Bootstrap.DataAccess.MongoDB /// /// /// - public static IMongoCollection Groups + public static IMongoCollection Groups { get { - return DBAccess.GetCollection("Groups"); + return DBAccess.GetCollection("Groups"); } }