From e186cf984c7c4d3a591adbdb265ff6a040797acb Mon Sep 17 00:00:00 2001 From: Refactoring Date: Mon, 30 Mar 2015 20:19:48 +0800 Subject: [PATCH] =?UTF-8?q?#96=20=E5=AE=9E=E7=8E=B0=E7=BB=84=E5=86=85?= =?UTF-8?q?=E7=9A=84=E4=BB=BB=E4=BD=95=E4=B8=80=E4=B8=AA=E4=BA=BA=E9=83=BD?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=88=86=E4=BA=AB=E5=86=85=E5=AE=B9=E5=88=B0?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E7=BB=84=E4=B8=AD.=20=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E4=B8=8D=E8=B6=B3=EF=BC=9A=E5=85=B1=E4=BA=AB=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E4=BB=A5=E5=88=86=E4=BA=AB=E8=80=85=E8=BF=9B=E8=A1=8C=E5=BD=92?= =?UTF-8?q?=E7=B1=BB=E7=BB=84=E7=BB=87.=20=20=E6=9B=B4=E5=A5=BD=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E7=BB=84=E7=BB=87=E6=96=B9=E5=BC=8F=EF=BC=9A?= =?UTF-8?q?=E5=85=B1=E4=BA=AB=E7=BB=99=E7=BB=84=E7=9A=84=E4=BB=A5=E7=BB=84?= =?UTF-8?q?=E5=BD=92=E7=B1=BB=E7=BB=84=E7=BB=87=EF=BC=8C=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E7=BB=99=E4=B8=AA=E4=BA=BA=E7=9A=84=E4=BB=A5=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E8=80=85=E4=B8=AA=E4=BA=BA=E7=BB=84=E7=BB=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/GroupService.go | 31 +++++++++++++++++++++++++++++++ app/service/ShareService.go | 14 ++++---------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/service/GroupService.go b/app/service/GroupService.go index 89c370c..8d72478 100644 --- a/app/service/GroupService.go +++ b/app/service/GroupService.go @@ -61,6 +61,32 @@ func (this *GroupService) GetGroups(userId string) ([]info.Group) { db.ListByQ(db.Groups, bson.M{"UserId": bson.ObjectIdHex(userId)}, &groups) return groups } + + +// 获取包含此用户的组对象数组 +func (this *GroupService) GetGroupsContainOf(userId string) []info.Group { + groupIds := this.GetGroupIdsContainOf(userId) + groups := []info.Group{} + db.ListByQ(db.Groups, bson.M{"_id": bson.M{"$in": groupIds}}, &groups) + return groups +} + +// 获取包含此用户的组Id数组 +func (this *GroupService) GetGroupIdsContainOf(userId string) []bson.ObjectId { + groupUsers := []info.GroupUser{} + db.ListByQWithFields(db.GroupUsers, bson.M{"UserId": bson.ObjectIdHex(userId)}, []string{"GroupId"}, &groupUsers) + if len(groupUsers) == 0 { + return nil + } + + groupIds := make([]bson.ObjectId, len(groupUsers)) + for i, each := range groupUsers { + groupIds[i] = each.GroupId + } + + return groupIds +} + // 得到分组, shareService用 func (this *GroupService) GetGroup(userId, groupId string) (info.Group) { // 得到分组s @@ -131,3 +157,8 @@ func (this *GroupService) DeleteUser(ownUserId, groupId, userId string) (ok bool } return db.Delete(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId), "UserId": bson.ObjectIdHex(userId)}), "" } + +// 判断组中是否包含指定用户 +func (this *GroupService) IsExistsGroupUser(userId, groupId string) (ok bool) { + return db.Has(db.GroupUsers, bson.M{"UserId": bson.ObjectIdHex(userId), "GroupId": bson.ObjectIdHex(groupId)}) +} \ No newline at end of file diff --git a/app/service/ShareService.go b/app/service/ShareService.go index d1c05bb..15552ee 100644 --- a/app/service/ShareService.go +++ b/app/service/ShareService.go @@ -666,8 +666,7 @@ func (this *ShareService) HasReadNotePerm(noteId, userId string) bool { // 得到笔记分享给的groups func (this *ShareService) GetNoteShareGroups(noteId, userId string) []info.ShareNote { - // 得到分组s - groups := groupService.GetGroups(userId) + groups := groupService.GetGroupsContainOf(userId) // 得到有分享的分组 shares := []info.ShareNote{} @@ -694,9 +693,7 @@ func (this *ShareService) GetNoteShareGroups(noteId, userId string) []info.Share // 共享笔记给分组 func (this *ShareService) AddShareNoteGroup(userId, noteId, groupId string, perm int) (bool) { - // 得到group, 是否是我的group - group := groupService.GetGroup(userId, groupId) - if group.GroupId == "" { + if !groupService.IsExistsGroupUser(userId, groupId) { return false } @@ -724,8 +721,7 @@ func (this *ShareService) DeleteShareNoteGroup(userId, noteId, groupId string) b // 得到笔记本分享给的groups func (this *ShareService) GetNotebookShareGroups(notebookId, userId string) []info.ShareNotebook { - // 得到分组s - groups := groupService.GetGroups(userId) + groups := groupService.GetGroupsContainOf(userId) // 得到有分享的分组 shares := []info.ShareNotebook{} @@ -752,9 +748,7 @@ func (this *ShareService) GetNotebookShareGroups(notebookId, userId string) []in } // 共享笔记给分组 func (this *ShareService) AddShareNotebookGroup(userId, notebookId, groupId string, perm int) (bool) { - // 得到group, 是否是我的group - group := groupService.GetGroup(userId, groupId) - if group.GroupId == "" { + if !groupService.IsExistsGroupUser(userId, groupId) { return false }