private async Task HandleEditChatBlockSegmentAsync(BlockSegmentVm segment)
        {
            using (MessengerDbContext _context = CreateContext())
            {
                using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
                {
                    try
                    {
                        EditChatBlockData blockData = (EditChatBlockData)segment.PublicData;
                        var editedChat = await _context.Chats.FirstOrDefaultAsync(chat => chat.Id == blockData.Chat.Id).ConfigureAwait(false);

                        if (editedChat != null)
                        {
                            editedChat = ChatConverter.GetChat(editedChat, ChatConverter.GetChatDto(blockData.Chat));
                            _context.Chats.Update(editedChat);
                            await _context.SaveChangesAsync().ConfigureAwait(false);

                            transaction.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrorMessage(nameof(HandleEditChatBlockSegmentAsync), ex.ToString());
                        transaction.Rollback();
                    }
                }
            }
        }
示例#2
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);
        }