public async Task <Channel> AddChannel(Channel channel, params ChannelMember[] members) { bool saved = false; do { try { long id = SkynetRandom.Id(); channel.ChannelId = id; Channels.Add(channel); foreach (ChannelMember member in members) { member.ChannelId = id; } ChannelMembers.AddRange(members); await SaveChangesAsync().ConfigureAwait(false); saved = true; } catch (DbUpdateException ex) when(ex?.InnerException is MySqlException mex && mex.Number == 1062) { } } while (!saved); return(channel); } #endregion }
public async Task CheckIfUserExistInChannelAsyncTest() { // Arrange var channel = new Channel { Id = Guid.NewGuid(), IsClosed = true, CreatorId = _memberId, Created = DateTimeOffset.UtcNow, Name = "test", Type = ChannelType.Public, Description = "test", WelcomeMessage = "test", MembersCount = 10 }; var channel2 = new Channel { Id = Guid.NewGuid(), IsClosed = true, CreatorId = _memberId2, Created = DateTimeOffset.UtcNow, Name = "test2", Type = ChannelType.Public, Description = "test", WelcomeMessage = "test", MembersCount = 10 }; var channelMember = new ChannelMembers { ChannelId = channel.Id, MemberId = _memberId, LastReadMessageId = null }; // Act await UnitOfWork.ChannelRepository.AddChannelAsync(channel); await UnitOfWork.ChannelRepository.AddChannelAsync(channel2); await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(channelMember); var ensureExist = await UnitOfWork.ChannelRepository.CheckIfMemberExistInChannelAsync(_memberId, channel.Id); var ensureNonExist = await UnitOfWork.ChannelRepository.CheckIfMemberExistInChannelAsync(_memberId, channel2.Id); // Assert Assert.True(ensureExist); Assert.False(ensureNonExist); }
public async Task AddChannelMemberAsync(ChannelMembers channelMember) { using (var connection = _sqlConnectionFactory.CreateConnection()) { await connection.OpenAsync(); var sqlQuery = @" INSERT INTO ChannelMembers(ChannelId, MemberId, LastReadMessageId, IsMuted) VALUES (@ChannelId, @MemberId, @LastReadMessageId, @IsMuted)"; await connection.ExecuteScalarAsync(sqlQuery, channelMember); } }
public static ChannelMemberResponse ToChannelMemberResponse(this ChannelMembers channelMember) { var channelMemberResponse = new ChannelMemberResponse(); if (channelMember != null) { channelMemberResponse.MemberId = channelMember.MemberId; channelMemberResponse.ChannelId = channelMember.ChannelId; channelMemberResponse.IsMuted = channelMember.IsMuted; channelMemberResponse.LastReadMessageId = channelMember.LastReadMessageId; } return(channelMemberResponse); }
public async Task UpdateChannelMemberAsync(ChannelMembers channelMember) { using (var connection = _sqlConnectionFactory.CreateConnection()) { await connection.OpenAsync(); var sqlQuery = @"UPDATE ChannelMembers SET ChannelId = @ChannelId, MemberId = @MemberId, LastReadMessageId = @LastReadMessageId, IsMuted = @IsMuted WHERE ChannelId = @ChannelId AND MemberId = @MemberId"; await connection.ExecuteAsync(sqlQuery, channelMember); } }
public async Task GetOnlineMembersInChannelAsyncTest() { // Arrange var member = new Member { Id = _memberId2, Email = "test", Role = UserRole.Admin, IsAfk = true, IsBanned = true, LastNudged = DateTimeOffset.UtcNow, LastActivity = DateTimeOffset.UtcNow, Name = "test2", SaasUserId = "test", Status = UserStatus.Active }; var channelMember = new ChannelMembers { MemberId = member.Id, ChannelId = _channelId, LastReadMessageId = null }; // Act await UnitOfWork.MemberRepository.AddMemberAsync(member); await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(channelMember); var members = await UnitOfWork.MemberRepository.GetOnlineMembersInChannelAsync(_channelId); // Assert Assert.NotNull(members); Assert.NotEmpty(members); Assert.True(members.Count == 1); Assert.Equal(member.Id, members.First().Id); Assert.Equal(member.Email, members.First().Email); Assert.Equal(member.Role, members.First().Role); Assert.Equal(member.IsAfk, members.First().IsAfk); Assert.Equal(member.IsBanned, members.First().IsBanned); Assert.Equal(member.Name, members.First().Name); Assert.Equal(member.SaasUserId, members.First().SaasUserId); Assert.Equal(member.Status, members.First().Status); }
public async Task DeleteChannelMemberAsyncTest() { // Arrange var channelMember = new ChannelMembers { MemberId = _memberId, ChannelId = _channelId, LastReadMessageId = null }; // Act await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(channelMember); await UnitOfWork.ChannelMemberRepository.DeleteChannelMemberAsync(channelMember.MemberId, channelMember.ChannelId); var newChannelMembers = await UnitOfWork.ChannelMemberRepository.GetChannelMembersAsync(channelMember.ChannelId); // Assert Assert.Empty(newChannelMembers); }
public async Task JoinToChannelAsync(JoinToChannelRequest request) { var channel = await UnitOfWork.ChannelRepository.GetChannelByIdAsync(request.ChannelId); Ensure.That(channel) .WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Channel does not exist."))) .IsNotNull(); var member = await UnitOfWork.MemberRepository.GetMemberBySaasUserIdAsync(request.SaasUserId); Ensure.That(member) .WithException(x => new ServiceException(new ErrorDto(ErrorCode.NotFound, "Member does not exist."))) .IsNotNull(); var members = await _channelMemberService.GetChannelMembersAsync(new ChannelRequest(request.SaasUserId, request.ChannelId)); if (members.Any(x => x.MemberId == member.Id)) { throw new ConflictException(new ErrorDto(ErrorCode.ConflictError, "You have been already joined to the channel.")); } // Throw if the channel is private but the user isn't allowed if (channel.Type == ChannelType.Private && channel.CreatorId != member.Id) { throw new ConflictException(new ErrorDto(ErrorCode.ConflictError, "This channel is not available for you.")); } var channelMember = new ChannelMembers { ChannelId = channel.Id, MemberId = member.Id, LastReadMessageId = null }; using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(channelMember); await UnitOfWork.ChannelRepository.IncrementChannelMembersCount(channel.Id); transactionScope.Complete(); } }
public async Task GetChannelMemberByIdAsync() { // Arrange var channelMember = new ChannelMembers { MemberId = _memberId, ChannelId = _channelId, LastReadMessageId = null }; // Act await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(channelMember); var newChannelMember = await UnitOfWork.ChannelMemberRepository.GetChannelMemberAsync(channelMember.MemberId, channelMember.ChannelId); // Assert Assert.NotNull(newChannelMember); Assert.Equal(channelMember.MemberId, newChannelMember.MemberId); Assert.Equal(channelMember.ChannelId, newChannelMember.ChannelId); Assert.Equal(channelMember.IsMuted, newChannelMember.IsMuted); }
public async Task <ChannelResponse> InviteMemberAsync(InviteMemberRequest request) { var channel = await UnitOfWork.ChannelRepository.GetChannelByIdAsync(request.ChannelId); Ensure.That(channel).WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Channel does not exist."))).IsNotNull(); var member = await UnitOfWork.MemberRepository.GetMemberByIdAsync(request.MemberId); Ensure.That(member).WithException(x => new ServiceException(new ErrorDto(ErrorCode.NotFound, "Member does not exist."))).IsNotNull(); var channelMembers = await GetChannelMembersAsync(new ChannelRequest(member.SaasUserId, channel.Id)); if (channelMembers.Any(x => x.Id == request.MemberId)) { throw new ConflictException(new ErrorDto(ErrorCode.ConflictError, "User already exists in channel.")); } var channelMember = new ChannelMembers { ChannelId = request.ChannelId, MemberId = request.MemberId, LastReadMessageId = null }; using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(channelMember); await UnitOfWork.ChannelRepository.IncrementChannelMembersCount(channel.Id); transactionScope.Complete(); } var newChannel = await UnitOfWork.ChannelRepository.GetChannelByIdAsync(channel.Id); return(newChannel.ToChannelResponse(_configuration)); }
public async Task <ChannelSummaryResponse> CreateChannelAsync(CreateChannelRequest request) { var profile = await UnitOfWork.MemberRepository.GetMemberBySaasUserIdAsync(request.SaasUserId); Ensure.That(profile) .WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Member does not exist."))) .IsNotNull(); string permanentChannelImageUrl = await CopyImageToDestinationContainerAsync(request.PhotoUrl); var newChannel = new Domain.Channel.Channel { Id = Guid.NewGuid(), Created = DateTimeOffset.UtcNow, Name = request.Name, Description = request.Description, WelcomeMessage = request.WelcomeMessage, Type = request.Type, Members = new List <ChannelMembers>(), CreatorId = profile.Id, Creator = profile, MembersCount = 0, PhotoUrl = permanentChannelImageUrl }; var creator = new ChannelMembers { ChannelId = newChannel.Id, MemberId = profile.Id, LastReadMessageId = null, IsMuted = false }; newChannel.Members.Add(creator); if (request.Type == ChannelType.Private && request.AllowedMembers.Any()) { foreach (var saasUserId in request.AllowedMembers) { var member = await UnitOfWork.MemberRepository.GetMemberBySaasUserIdAsync(saasUserId); Ensure.That(member).WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Member does not exist."))).IsNotNull(); var model = new ChannelMembers { ChannelId = newChannel.Id, MemberId = member.Id, LastReadMessageId = null }; newChannel.Members.Add(model); } } var channelMembers = newChannel.Members.DistinctBy(x => x.MemberId); using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await UnitOfWork.ChannelRepository.AddChannelAsync(newChannel); foreach (var member in channelMembers) { await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(member); await UnitOfWork.ChannelRepository.IncrementChannelMembersCount(newChannel.Id); } transactionScope.Complete(); } var channel = await UnitOfWork.ChannelRepository.GetChannelByIdAsync(newChannel.Id); return(channel.ToChannelSummaryResponse(creator.IsMuted, null, null, _configuration)); }