public override async Task OnSetToManyRelationshipAsync(DomainGroup group, HasManyAttribute hasManyRelationship, ISet <IIdentifiable> rightResourceIds,
                                                                OperationKind operationKind, CancellationToken cancellationToken)
        {
            _hitCounter.TrackInvocation <DomainGroup>(ResourceDefinitionHitCounter.ExtensibilityPoint.OnSetToManyRelationshipAsync);

            if (hasManyRelationship.Property.Name == nameof(DomainGroup.Users))
            {
                HashSet <Guid> rightUserIds = rightResourceIds.Select(resource => (Guid)resource.GetTypedId()).ToHashSet();

                List <DomainUser> beforeUsers = await _userSet.Include(user => user.Group).Where(user => rightUserIds.Contains(user.Id))
                                                .ToListAsync(cancellationToken);

                foreach (DomainUser beforeUser in beforeUsers)
                {
                    IMessageContent content = null;

                    if (beforeUser.Group == null)
                    {
                        content = new UserAddedToGroupContent
                        {
                            UserId  = beforeUser.Id,
                            GroupId = group.Id
                        };
                    }
                    else if (beforeUser.Group != null && beforeUser.Group.Id != group.Id)
                    {
                        content = new UserMovedToGroupContent
                        {
                            UserId        = beforeUser.Id,
                            BeforeGroupId = beforeUser.Group.Id,
                            AfterGroupId  = group.Id
                        };
                    }

                    if (content != null)
                    {
                        _pendingMessages.Add(OutgoingMessage.CreateFromContent(content));
                    }
                }

                if (group.Users != null)
                {
                    foreach (DomainUser userToRemoveFromGroup in group.Users.Where(user => !rightUserIds.Contains(user.Id)))
                    {
                        var message = OutgoingMessage.CreateFromContent(new UserRemovedFromGroupContent
                        {
                            UserId  = userToRemoveFromGroup.Id,
                            GroupId = group.Id
                        });

                        _pendingMessages.Add(message);
                    }
                }
            }
        }
Example #2
0
        public override Task <IIdentifiable> OnSetToOneRelationshipAsync(DomainUser user, HasOneAttribute hasOneRelationship, IIdentifiable rightResourceId,
                                                                         OperationKind operationKind, CancellationToken cancellationToken)
        {
            _hitCounter.TrackInvocation <DomainUser>(ResourceDefinitionHitCounter.ExtensibilityPoint.OnSetToOneRelationshipAsync);

            if (hasOneRelationship.Property.Name == nameof(DomainUser.Group))
            {
                var             afterGroupId = (Guid?)rightResourceId?.GetTypedId();
                IMessageContent content      = null;

                if (user.Group != null && afterGroupId == null)
                {
                    content = new UserRemovedFromGroupContent
                    {
                        UserId  = user.Id,
                        GroupId = user.Group.Id
                    };
                }
                else if (user.Group == null && afterGroupId != null)
                {
                    content = new UserAddedToGroupContent
                    {
                        UserId  = user.Id,
                        GroupId = afterGroupId.Value
                    };
                }
                else if (user.Group != null && afterGroupId != null && user.Group.Id != afterGroupId)
                {
                    content = new UserMovedToGroupContent
                    {
                        UserId        = user.Id,
                        BeforeGroupId = user.Group.Id,
                        AfterGroupId  = afterGroupId.Value
                    };
                }

                if (content != null)
                {
                    var message = OutgoingMessage.CreateFromContent(content);
                    _pendingMessages.Add(message);
                }
            }

            return(Task.FromResult(rightResourceId));
        }
        public override async Task OnAddToRelationshipAsync(Guid groupId, HasManyAttribute hasManyRelationship, ISet <IIdentifiable> rightResourceIds,
                                                            CancellationToken cancellationToken)
        {
            if (hasManyRelationship.Property.Name == nameof(DomainGroup.Users))
            {
                HashSet <Guid> rightUserIds = rightResourceIds.Select(resource => (Guid)resource.GetTypedId()).ToHashSet();

                List <DomainUser> beforeUsers = await _userSet.Include(user => user.Group).Where(user => rightUserIds.Contains(user.Id))
                                                .ToListAsync(cancellationToken);

                foreach (DomainUser beforeUser in beforeUsers)
                {
                    IMessageContent content = null;

                    if (beforeUser.Group == null)
                    {
                        content = new UserAddedToGroupContent
                        {
                            UserId  = beforeUser.Id,
                            GroupId = groupId
                        };
                    }
                    else if (beforeUser.Group != null && beforeUser.Group.Id != groupId)
                    {
                        content = new UserMovedToGroupContent
                        {
                            UserId        = beforeUser.Id,
                            BeforeGroupId = beforeUser.Group.Id,
                            AfterGroupId  = groupId
                        };
                    }

                    if (content != null)
                    {
                        _pendingMessages.Add(OutgoingMessage.CreateFromContent(content));
                    }
                }
            }
        }