Пример #1
0
        /// <summary>
        ///     Callback which is fired when a client disconnected from
        /// </summary>
        /// <param name="stopCalled"></param>
        /// <returns></returns>
        public override async Task OnDisconnected(bool stopCalled)
        {
            using (var lifeTimeScope = LifetimeScope.BeginLifetimeScope())
            {
                // Search unit of work from life time scope.
                var unitOfWork = lifeTimeScope.Resolve <IUnitOfWork>();

                // Search for record whose index is the same as connection index.
                var condition = new FindSignalrConnectionViewModel();
                condition.Index       = new TextSearch();
                condition.Index.Mode  = TextComparision.EqualIgnoreCase;
                condition.Index.Value = Context.ConnectionId;

                // Find connections with specific conditions.
                var connections = unitOfWork.RepositorySignalrConnections.Search();
                connections = unitOfWork.RepositorySignalrConnections.Search(connections, condition);

                unitOfWork.RepositorySignalrConnections.Remove(connections);
                await unitOfWork.CommitAsync();
            }
        }
        /// <summary>
        ///     Search records with specific conditions.
        /// </summary>
        /// <param name="connections"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public IQueryable <SignalrConnection> Search(IQueryable <SignalrConnection> connections,
                                                     FindSignalrConnectionViewModel conditions)
        {
            // Conditions is invalid.
            if (conditions == null)
            {
                return(connections);
            }

            if (conditions.Index != null && !string.IsNullOrEmpty(conditions.Index.Value))
            {
                var szIndex = conditions.Index;
                switch (szIndex.Mode)
                {
                case TextComparision.Contain:
                    connections = connections.Where(x => x.Index.Contains(szIndex.Value));
                    break;

                case TextComparision.Equal:
                    connections = connections.Where(x => x.Index.Equals(szIndex.Value));
                    break;

                case TextComparision.EqualIgnoreCase:
                    connections =
                        connections.Where(x => x.Index.Equals(szIndex.Value, StringComparison.InvariantCultureIgnoreCase));
                    break;

                case TextComparision.StartsWith:
                    connections = connections.Where(x => x.Index.StartsWith(szIndex.Value));
                    break;

                case TextComparision.StartsWithIgnoreCase:
                    connections =
                        connections.Where(
                            x => x.Index.StartsWith(szIndex.Value, StringComparison.InvariantCultureIgnoreCase));
                    break;

                case TextComparision.EndsWith:
                    connections = connections.Where(x => x.Index.EndsWith(szIndex.Value));
                    break;

                case TextComparision.EndsWithIgnoreCase:
                    connections =
                        connections.Where(
                            x => x.Index.EndsWith(szIndex.Value, StringComparison.InvariantCultureIgnoreCase));
                    break;

                default:
                    connections = connections.Where(x => x.Index.ToLower().Contains(szIndex.Value.ToLower()));
                    break;
                }
            }

            if (conditions.Owner != null)
            {
                connections = connections.Where(x => x.OwnerIndex == conditions.Owner.Value);
            }

            var timeSearchCreated = conditions.Created;

            if (timeSearchCreated != null)
            {
                var from = timeSearchCreated.From;
                var to   = timeSearchCreated.To;
                if (from != null)
                {
                    connections = connections.Where(x => x.CreatedTime >= from.Value);
                }
                if (to != null)
                {
                    connections = connections.Where(x => x.CreatedTime <= to.Value);
                }
            }

            return(connections);
        }