public static ChannelUser GetChannelUser(ChannelUserVm channelUser) { return(new ChannelUser { ChannelId = channelUser.ChannelId.GetValueOrDefault(), ChannelUserRole = channelUser.ChannelUserRole.GetValueOrDefault(), Deleted = channelUser.Deleted.GetValueOrDefault(), UserId = channelUser.UserId, Banned = channelUser.Banned.GetValueOrDefault(), IsMuted = channelUser.IsMuted.GetValueOrDefault() }); }
public static ChannelUserDto GetChannelUserDto(ChannelUserVm channelUserVm) { if (channelUserVm == null) { return(null); } return(new ChannelUserDto { Banned = channelUserVm.Banned.GetValueOrDefault(), ChannelId = channelUserVm.ChannelId.GetValueOrDefault(), ChannelUserRole = channelUserVm.ChannelUserRole.GetValueOrDefault(), Deleted = channelUserVm.Deleted.GetValueOrDefault(), IsMuted = channelUserVm.IsMuted.GetValueOrDefault(), SubscribedTime = channelUserVm.SubscribedTime.GetValueOrDefault(), UserId = channelUserVm.UserId }); }
public async Task <List <ChannelUserVm> > EditChannelUsersAsync(List <ChannelUserVm> channelUsers, long editorUserId, long channelId) { using (MessengerDbContext context = contextFactory.Create()) { ExpressionStarter <ChannelUser> channelUsersContidion = PredicateBuilder.New <ChannelUser>(); channelUsersContidion = channelUsers.Aggregate(channelUsersContidion, (current, value) => current.Or(opt => opt.ChannelId == value.ChannelId && opt.UserId == value.UserId)); Channel currentChannel = await context.Channels.FirstOrDefaultAsync(opt => opt.ChannelId == channelId && !opt.Deleted).ConfigureAwait(false); if (currentChannel == null) { throw new ObjectDoesNotExistsException("Channel not found."); } ChannelUser editorChannelUser = await context.ChannelUsers .AsNoTracking() .FirstOrDefaultAsync(opt => opt.ChannelId == channelId && opt.UserId == editorUserId) .ConfigureAwait(false); List <ChannelUser> editableChannelUsers = await context.ChannelUsers .Where(channelUsersContidion) .ToListAsync() .ConfigureAwait(false); if (editorChannelUser == null) { throw new PermissionDeniedException(); } if (editableChannelUsers == null || editableChannelUsers.Count < channelUsers.Count()) { throw new ObjectDoesNotExistsException("Editable users are not in channel."); } if (editorChannelUser.ChannelUserRole == ChannelUserRole.Subscriber && channelUsers.Count() != 1 && channelUsers.ElementAt(0).UserId != editorChannelUser.UserId || editorChannelUser.Banned) { throw new PermissionDeniedException(); } editableChannelUsers.ForEach(channelUser => { ChannelUserVm edited = channelUsers.FirstOrDefault(opt => opt.UserId == channelUser.UserId); if (editorChannelUser.ChannelUserRole > channelUser.ChannelUserRole) { channelUser.ChannelUserRole = edited.ChannelUserRole < editorChannelUser.ChannelUserRole && editorChannelUser.UserId != channelUser.UserId ? edited.ChannelUserRole.GetValueOrDefault() : channelUser.ChannelUserRole; channelUser.Deleted = channelUser.ChannelUserRole != ChannelUserRole.Creator && edited.Deleted.GetValueOrDefault(channelUser.Deleted); channelUser.Banned = channelUser.ChannelUserRole != ChannelUserRole.Creator && edited.Banned.GetValueOrDefault(channelUser.Banned); } else if (editorChannelUser.UserId == channelUser.UserId) { channelUser.ChannelUserRole = edited.ChannelUserRole < channelUser.ChannelUserRole ? edited.ChannelUserRole.GetValueOrDefault() : channelUser.ChannelUserRole; channelUser.Deleted = edited.Deleted.GetValueOrDefault(false); } else { throw new PermissionDeniedException(); } }); context.ChannelUsers.UpdateRange(editableChannelUsers); if (channelUsers.Any(opt => opt.Deleted.GetValueOrDefault() || opt.Banned.GetValueOrDefault())) { var query = from channel in context.Channels join channelUser in context.ChannelUsers on channel.ChannelId equals channelUser.ChannelId join user in context.Users on channelUser.UserId equals user.Id where channel.ChannelId == channelId && !channelUser.Banned && !channelUser.Deleted select user.NodeId; var channelNodesId = (await query.ToListAsync().ConfigureAwait(false)).Distinct(); currentChannel.NodesId = channelNodesId.Select(id => id.GetValueOrDefault()).ToArray(); context.Channels.Update(currentChannel); } await context.SaveChangesAsync().ConfigureAwait(false); return(ChannelConverter.GetChannelUsers(editableChannelUsers)); } }