private async Task ValidateAddingUserToGroupChatAsync(AddUserToGroupChatDto addUserToGroupDto, CancellationToken ct) { User user = await _unitOfWork.UserRepository.GetAsync(addUserToGroupDto.UserId, ct); if (user == null) { _logger.LogWarning("User {UserId} not found", addUserToGroupDto.UserId); throw new UserNotFoundException(); } GroupChat chat = await _unitOfWork.GroupChatRepository.GetAsync(addUserToGroupDto.ChatId, ct); if (chat == null) { _logger.LogWarning("Group chat with id {GroupChatId} not found", addUserToGroupDto.ChatId); throw new ChatNotFoundException(); } if (chat.OwnerId != addUserToGroupDto.AddedByUserId) { _logger.LogWarning("Only owner can add user to group chat. {UserId} is not the owner", addUserToGroupDto.AddedByUserId); throw new AddUserActionForbiddenException(); } UserGroupChat userGroupChat = await _unitOfWork.UserGroupChatRepository.GetUserGroupChatAsync( addUserToGroupDto.ChatId, addUserToGroupDto.UserId, ct); if (userGroupChat != null) { _logger.LogWarning("User with id {UserId} already exist in chat", addUserToGroupDto.UserId, addUserToGroupDto.ChatId); throw new UserAlreadyInGroupChatException(); } }
public async Task AddUserToGroupChatAsync(AddUserToGroupChatDto addUserToGroupDto, CancellationToken ct = default) { _logger.LogInformation("Add user to group chat {AddUserToGroup}", addUserToGroupDto); await ValidateAddingUserToGroupChatAsync(addUserToGroupDto, ct); UserGroupChat addUserToGroup = _mapper.Map <UserGroupChat>(addUserToGroupDto); addUserToGroup.CreatedAt = addUserToGroup.UpdatedAt = DateTime.UtcNow; _unitOfWork.UserGroupChatRepository.Create(addUserToGroup); await _unitOfWork.CommitAsync(ct); }