public async Task <List <ChatVm> > FindChatsByStringQueryAsync(string searchQuery, long?navigationId = 0, bool?direction = true, long?userId = null)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                ExpressionsHelper helper = new ExpressionsHelper();
                var searchExpression     = helper.GetChatExpression(searchQuery);
                IQueryable <Chat> query  = context.Chats
                                           .Where(opt => !opt.Deleted)
                                           .AsNoTracking();
                if (userId == null)
                {
                    query = query.Where(opt => (opt.Type == (short)ChatType.Public));
                }
                else
                {
                    query = query.Where(opt => opt.ChatUsers
                                        .Any(chatUser => chatUser.UserId == userId && !chatUser.Banned && !chatUser.Deleted) || (opt.Type == (short)ChatType.Public));
                }

                query = query.Where(searchExpression);
                if (direction.GetValueOrDefault() == true)
                {
                    query = query.OrderBy(opt => opt.Id)
                            .Where(opt => opt.Id > navigationId.GetValueOrDefault());
                }
                else
                {
                    query = query.OrderByDescending(opt => opt.Id)
                            .Where(opt => opt.Id < navigationId.GetValueOrDefault());
                }
                List <Chat> chats = await query.ToListAsync().ConfigureAwait(false);

                return(ChatConverter.GetChatsVm(chats));
            }
        }
        public async Task <List <ChatVm> > FindChatsAsync(SearchChatVm template, int limit = 100, long navigationChatId = 0, long?nodeId = null)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                IQueryable <Chat> query;
                if (template != null)
                {
                    ExpressionsHelper expressionsHelper = new ExpressionsHelper();
                    query = context.Chats
                            .AsNoTracking()
                            .OrderBy(chat => chat.Id)
                            .Where(expressionsHelper.GetChatExpression(template))
                            .Take(limit);
                }
                else
                {
                    query = context.Chats
                            .AsNoTracking()
                            .OrderBy(chat => chat.Id)
                            .Take(limit);
                }
                if (nodeId != null)
                {
                    query = query.Where(chat => chat.NodesId.Contains(nodeId.Value));
                }
                List <Chat> result = await query
                                     .Where(chat => chat.Deleted == false && chat.Id > navigationChatId && (ChatType)chat.Type != ChatType.Private)
                                     .ToListAsync()
                                     .ConfigureAwait(false);

                return(ChatConverter.GetChatsVm(result));
            }
        }
 public async Task <List <ChatVm> > GetChatsNodeAsync(long userId = 0, byte limit = 100, long navigationId = 0)
 {
     try
     {
         using (MessengerDbContext context = contextFactory.Create())
         {
             List <Chat> chats;
             if (userId == 0)
             {
                 chats = await context.Chats
                         .AsNoTracking()
                         .OrderBy(chat => chat.Id)
                         .Where(chat => chat.Id > navigationId && ((ChatType)chat.Type) != ChatType.Private)
                         .Take(limit)
                         .ToListAsync()
                         .ConfigureAwait(false);
             }
             else
             {
                 var query = from chat in context.Chats
                             join chatUser in context.ChatUsers on chat.Id equals chatUser.ChatId
                             where chatUser.UserId == userId && chatUser.Deleted == false
                             select chat;
                 chats = await query
                         .AsNoTracking()
                         .OrderBy(chat => chat.Id)
                         .Where(chat => chat.Id > navigationId)
                         .Take(limit)
                         .ToListAsync()
                         .ConfigureAwait(false);
             }
             return(ChatConverter.GetChatsVm(chats));
         }
     }
     catch (Exception ex)
     {
         throw new GetConversationsException("Error when retrieving chat list.", ex);
     }
 }
        public async Task <List <ChatVm> > GetChatsByIdAsync(IEnumerable <long> chatsId, long?userId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var chatsCondition = PredicateBuilder.New <Chat>();
                chatsCondition = chatsId.Aggregate(chatsCondition,
                                                   (current, value) => current.Or(opt => opt.Id == value && !opt.Deleted).Expand());
                var query = context.Chats
                            .Where(chatsCondition);
                if (userId != null)
                {
                    query = query.Where(opt => opt.ChatUsers.Any(user => (user.UserId == userId && !user.Banned && !user.Deleted) || opt.Type == (short)ChatType.Public));
                }
                else
                {
                    query = query.Where(opt => opt.Type == (short)ChatType.Public);
                }
                var chats = await query.ToListAsync().ConfigureAwait(false);

                return(ChatConverter.GetChatsVm(chats));
            }
        }