Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
        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));
            }
        }