Beispiel #1
0
        public async Task CreateChats()
        {
            var    creator = fillTestDbHelper.Users.FirstOrDefault();
            var    users   = fillTestDbHelper.Users.Where(opt => !opt.BlackList.Any(p => p.BadUid == creator.Id) && !creator.BlackList.Any(p => p.BadUid == opt.Id)).Take(5).ToList();
            ChatVm newChat = new ChatVm
            {
                About = "Create chat test 2",
                Name  = "Chat",
                Users = users.Select(opt => new ChatUserVm
                {
                    UserId   = opt.Id,
                    UserRole = opt.Id == creator.Id ? ObjectsLibrary.Enums.UserRole.Creator : ObjectsLibrary.Enums.UserRole.Moderator
                }).ToList()
            };
            var userChats     = fillTestDbHelper.Chats.Where(opt => opt.ChatUsers.Any(p => p.UserId == creator.Id));
            var expectedChats = ChatConverter.GetChatsDto(userChats).Append(ChatConverter.GetChatDto(newChat)).ToList();
            var actualChats   = await createChatsService.CreateOrUpdateUserChatsAsync(expectedChats);

            Assert.Equal(expectedChats.Count, actualChats.Count);
        }
        public async Task <List <ChatDto> > GetUserChatsAsync(long userId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var query = from chat in context.Chats
                            join chatUser in context.ChatUsers on chat.Id equals chatUser.ChatId
                            where chatUser.UserId == userId &&
                            !chatUser.Deleted &&
                            !chatUser.Banned &&
                            !chat.Deleted
                            select chat;
                var chats = await query.AsNoTracking()
                            .Include(opt => opt.ChatUsers)
                            .Include(opt => opt.Messages)
                            .ThenInclude(opt => opt.Attachments)
                            .ToListAsync()
                            .ConfigureAwait(false);

                return(ChatConverter.GetChatsDto(chats));
            }
        }
Beispiel #3
0
        public async Task <List <ChatDto> > CreateOrUpdateUserChatsAsync(List <ChatDto> userChats)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var chatsCondition = PredicateBuilder.New <Chat>();
                chatsCondition = userChats.Aggregate(chatsCondition,
                                                     (current, value) => current.Or(opt => opt.Id == value.Id).Expand());
                List <ChatDto> resultChats   = new List <ChatDto>();
                List <Chat>    existingChats = await context.Chats
                                               .Where(chatsCondition)
                                               .ToListAsync()
                                               .ConfigureAwait(false);

                if (existingChats.Any())
                {
                    for (int i = 0; i < existingChats.Count; i++)
                    {
                        var editedChat = userChats.FirstOrDefault(opt => opt.Id == existingChats[i].Id);
                        editedChat.ChatUsers = null;
                        existingChats[i]     = ChatConverter.GetChat(existingChats[i], editedChat);
                    }
                    context.Chats.UpdateRange(existingChats);
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    resultChats.AddRange(ChatConverter.GetChatsDto(existingChats));
                }
                List <ChatDto> nonExistingChats = userChats.Where(chat => !existingChats.Any(opt => opt.Id == chat.Id))?.ToList();
                if (nonExistingChats != null && nonExistingChats.Any())
                {
                    List <Chat> newChats = nonExistingChats.Select(chat => ChatConverter.GetChat(null, chat))?.ToList();
                    await context.Chats.AddRangeAsync(newChats).ConfigureAwait(false);

                    await context.SaveChangesAsync().ConfigureAwait(false);

                    resultChats.AddRange(ChatConverter.GetChatsDto(newChats));
                }
                return(resultChats);
            }
        }