public async Task <List <ValuePair <UserDto, byte[]> > > GetPollVotedUsersAsync(Guid pollId, long conversationId, ConversationType conversationType, byte optionId, long?requestorId, int limit = 30, long navigationUserId = 0) { using (MessengerDbContext context = contextFactory.Create()) { var poll = await context.Polls.FirstOrDefaultAsync(opt => opt.PollId == pollId && opt.ConversationType == conversationType && opt.ConvertsationId == conversationId) .ConfigureAwait(false); if (requestorId.GetValueOrDefault() != poll.CreatorId && !poll.ResultsVisibility && !await conversationsService.IsUserInConversationAsync(conversationType, conversationId, requestorId.GetValueOrDefault()).ConfigureAwait(false)) { throw new PermissionDeniedException(); } List <PollOptionVote> votes = await context.PollsOptionsVotes .Include(opt => opt.User) .Include(opt => opt.User.Phones) .Include(opt => opt.User.Emails) .Include(opt => opt.User.BlackList) .Where(opt => opt.ConversationId == conversationId && opt.ConversationType == conversationType && opt.PollId == pollId && opt.OptionId == optionId && opt.UserId > navigationUserId) .OrderBy(opt => opt.UserId) .Take(limit) .ToListAsync() .ConfigureAwait(false); return(votes.Select(vote => new ValuePair <UserDto, byte[]>(UserConverter.GetUserDto(vote.User), vote.Sign)).ToList()); } }
public async Task <UserDto> CreateOrUpdateUserAsync(UserDto user) { using (MessengerDbContext context = contextFactory.Create()) { User userInfo = await context.Users .Include(opt => opt.UserPublicKeys) .Include(opt => opt.Phones) .Include(opt => opt.Emails) .Include(opt => opt.BlackList) .Include(opt => opt.Tokens) .FirstOrDefaultAsync(opt => opt.Id == user.Id) .ConfigureAwait(false); if (userInfo != null) { userInfo = UserConverter.GetUser(userInfo, user); context.Update(userInfo); } else { userInfo = UserConverter.GetUser(user); await context.AddAsync(userInfo).ConfigureAwait(false); } await context.SaveChangesAsync().ConfigureAwait(false); return(UserConverter.GetUserDto(userInfo, null, null)); } }
private async Task HandleEditUserBlockSegmentAsync(BlockSegmentVm segment) { using (MessengerDbContext _context = CreateContext()) { using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false)) { try { var user = await _context.Users.FirstOrDefaultAsync(_user => _user.Id == segment.SegmentHeader.ObjectId).ConfigureAwait(false); if (user != null) { if (TryDecryptPrivateData <UserVm>(segment, out var userData)) { user = UserConverter.GetUser(UserConverter.GetUserDto(userData)); user.Confirmed = true; _context.Users.Update(user); } } else { if (TryDecryptPrivateData <UserVm>(segment, out var userData)) { user = UserConverter.GetUser(UserConverter.GetUserDto(userData)); } else { user = new User { Id = segment.SegmentHeader.ObjectId, NodeId = segment.NodeId, Confirmed = true }; } await _context.Users.AddAsync(user).ConfigureAwait(false); } await _context.SaveChangesAsync().ConfigureAwait(false); transaction.Commit(); } catch (Exception ex) { AddErrorMessage(nameof(HandleNewUserBlockSegmentAsync), ex.ToString()); transaction.Rollback(); } } } }
public async Task <UserDto> GetAllUserDataAsync(long userId) { using (MessengerDbContext context = contextFactory.Create()) { User user = await context.Users.AsNoTracking() .Include(opt => opt.DialogsFirstU) .ThenInclude(opt => opt.Messages) .ThenInclude(opt => opt.Attachments) .Include(opt => opt.Emails) .Include(opt => opt.Phones) .Include(opt => opt.FilesInfo) .Include(opt => opt.BlackList) .Include(opt => opt.Tokens) .Include(opt => opt.UserPublicKeys) .Include(opt => opt.Contacts) .Include(opt => opt.Favorites) .Include(opt => opt.PollOptionsVotes) .Include(opt => opt.Groups) .ThenInclude(opt => opt.ContactGroups) .ThenInclude(opt => opt.Contact) .FirstOrDefaultAsync(opt => opt.Id == userId) .ConfigureAwait(false); IEnumerable <ChatDto> userChats = await loadChatsService.GetUserChatsAsync(userId).ConfigureAwait(false); IEnumerable <ChannelDto> userChannels = await loadChannelsService.GetUserChannelsAsync(userId).ConfigureAwait(false); foreach (var dialog in user.DialogsFirstU) { dialog.Messages = dialog.Messages.Where(opt => !opt.Deleted).ToList(); } foreach (var chat in userChats) { chat.Messages = chat.Messages.Where(opt => !opt.Deleted).ToList(); } foreach (var channel in userChannels) { channel.Messages = channel.Messages.Where(opt => !opt.Deleted).ToList(); } return(UserConverter.GetUserDto(user, userChats, userChannels)); } }
public async Task CreateOrUpdateUser() { var newUser = new UserDto { Id = 1488, NameFirst = "CRTOUPD", Confirmed = false }; var createdUser = await updateUsersService.CreateOrUpdateUserAsync(newUser); Assert.True(newUser.Id == createdUser.Id && newUser.NameFirst == createdUser.NameFirst && newUser.Confirmed == createdUser.Confirmed); var user = fillTestDbHelper.Users.FirstOrDefault(); user.NameFirst = "updated_user"; var updatedUser = await updateUsersService.CreateOrUpdateUserAsync(UserConverter.GetUserDto(user)); Assert.Equal(user.Id, updatedUser.Id); Assert.Equal(user.NameFirst, updatedUser.NameFirst); }