示例#1
0
        public async Task <DeleteSystemUserCommandResponse> Handle(DeleteSystemUserCommand request, CancellationToken cancellationToken)
        {
            var response = new DeleteSystemUserCommandResponse()
            {
                IsSuccessful = false
            };

            var systemUserIds = request.SystemUserIds.Distinct().ToList();
            var systemusers   = await _systemUserRepository.GetSystemUsersByIds(systemUserIds);

            if (systemusers.Count != systemUserIds.Count)
            {
                throw new RulesException("Invalid", @"User not found");
            }

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                foreach (var sysuser in systemusers)
                {
                    foreach (var sysuserassociatedcountry in sysuser.SystemUserAssociatedCountries.ToList())
                    {
                        sysuser.SystemUserAssociatedCountries.Remove(sysuserassociatedcountry);
                        _systemUserRepository.Delete(sysuserassociatedcountry);
                    }
                    _systemUserRepository.DeleteSystemUser(sysuser);
                }

                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var userDocs = _context.GetAll(Constants.SystemUsersDiscriminator);
                foreach (var user in systemusers)
                {
                    //foreach (var country in user.SystemUserAssociatedCountries)
                    //{
                    var eventsourcing = new SystemUserCommandEvent()
                    {
                        id            = userDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId).GetPropertyValue <Guid>("id"),
                        EventType     = ServiceBusEventType.Delete,
                        Discriminator = Constants.SystemUsersDiscriminator,
                        SystemUserId  = user.SystemUserId,
                        PartitionKey  = userDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId).GetPropertyValue <int>("CountryId").ToString()
                    };
                    await _eventcontext.PublishThroughEventBusAsync(eventsourcing);

                    //}
                }
                scope.Complete();
            }
            return(response);
        }