public async Task <AppUserDto> AddUserToChat(int chatId, string userId)
        {
            var defaultError = new InvalidDataException("Invalid credentials were provided.");

            var FoundConversation = await conversationRepository.GetByIdAsync(chatId);

            if (FoundConversation == null)
            {
                throw defaultError;
            }

            var FoundUser = await usersRepository.GetByIdAsync(userId);

            if (FoundUser == null)
            {
                throw defaultError;
            }

            if (await usersConversationsRepository.Exists(FoundUser.Id, FoundConversation.Id))
            {
                throw new InvalidDataException("User already exists in converation.");
            }

            var addedUser = (await usersConversationsRepository.AddAsync(UsersConversationDataModel.Create(userId, chatId))).User;

            await rolesRepository.AddAsync(ChatRoleDataModel.Create(chatId, userId, ChatRole.NoRole));

            await unitOfWork.Commit();

            return(addedUser.ToAppUserDto());
        }
 public static ChatRoleDto ToChatRole(this ChatRoleDataModel value)
 {
     return(new ChatRoleDto
     {
         ChatId = value.ChatId,
         Role = value.RoleId
     });
 }
        private async Task <Shared.DTO.Conversations.Chat> CreateGroup(
            AppUser creator,
            string chatImageUrl,
            string chatName,
            bool isPublic)
        {
            if (chatName.Length > MaxNameLength)
            {
                throw new InvalidDataException("Chat name was too long.");
            }

            try
            {
                var imageUrl = chatImageUrl ?? chatDataProvider.GetGroupPictureUrl();

                var newChat = new ConversationDataModel
                {
                    IsGroup      = true,
                    Name         = chatName,
                    FullImageUrl = imageUrl,
                    ThumbnailUrl = imageUrl,
                    IsPublic     = isPublic
                };

                await conversationRepository.AddAsync(newChat);

                await usersConversationsRepository.AddAsync(UsersConversationDataModel.Create(
                                                                creator.Id,
                                                                newChat));

                var role = await rolesRepository.AddAsync(
                    ChatRoleDataModel.Create(newChat, creator.Id, ChatRole.Creator));

                newChat.Roles = new List <ChatRoleDataModel>()
                {
                    role
                };
                newChat.participants = new List <AppUser> {
                    creator
                };

                await unitOfWork.Commit();

                return(newChat.ToChatDto(creator.Id));
            }
            catch (Exception e)
            {
                throw new InvalidDataException("Couldn't create group because of unexpected error.", e);
            }
        }