Пример #1
0
        public async Task <IEnumerable <User> > GetFriends(int userId)
        {
            var chatsUsers = await _chatUserRepository.FindByAsync(cu => cu.UserId == userId);

            var chatIds = chatsUsers.Select(cu => cu.ChatId).ToList();
            var chats   = await _chatRepository.FindByAsync(c => chatIds.Contains(c.Id));

            var dialogs     = chats.Where(c => c.Type.Equals("dialog")).Select(d => d.Id).ToList();
            var friendfulCU = await _chatUserRepository.FindByAsync(cu => (dialogs.Contains(cu.ChatId) && (cu.UserId != userId)));

            var friendIds = friendfulCU.Select(c => c.UserId).ToList();
            var friends   = await _userRepository.FindByAsync(u => friendIds.Contains(u.Id));

            return(friends);
        }
Пример #2
0
        public async Task <IEnumerable <Message> > GetAll(int offset = 0)
        {
            var authenticationHeader = Request?.Headers["Authorization"];
            var token   = authenticationHeader?.FirstOrDefault().Split(' ')[1];
            var subject = _jwtFormater.GetSubject(token);
            var user    = _userRepository.GetSingleByUsername(subject);

            IEnumerable <Message> messages = new List <Message>();

            try
            {
                var chats = await _chatUserRepository.FindByAsync(cu => cu.UserId == user.Id);

                var chatIds = chats.Select(cu => cu.ChatId);
                messages = await _messageRepository.FindByAsync(message => chatIds.Contains(message.ChatId));

                messages = messages.ToList();
            }
            catch (Exception ex)
            {
                _loggingRepository.Add(new Error()
                {
                    Severity    = "Error",
                    Message     = ex.Message,
                    StackTrace  = ex.StackTrace,
                    DateCreated = DateTime.Now
                });
                _loggingRepository.Commit();
            }

            return(messages.Skip(messages.Count() - offset - 20).Take(20));
        }