public async Task <ActionResult <List <ChatMember> > > AddMemberToChat(string chatId, [FromBody] List <ChatMember> members) { try { var appId = (await HttpContext.GetUserInfoAsync()).AppId; var model = new ChatMembersVM() { ChatId = chatId, ChatMembers = members }; var addedMembers = await _chatService.AddMembersToChat(appId, model); return(addedMembers); } catch { } return(NotFound()); }
public async Task <List <ChatMember> > AddMembersToChat(string appId, ChatMembersVM model) { var chat = await _chatRepository.GetAsync(appId, model.ChatId); if (chat != null && chat.ChatType == ChatType.Group) { chat.ChatMembers = chat.ChatMembers != null ? chat.ChatMembers : new List <ChatMember> (); var shouldBeAdded = model.ChatMembers .Where(member => !chat.ChatMembers.Any(oldMember => oldMember.UserId == member.UserId)) .Select(member => new ChatMember() { UserId = member.UserId }).ToList(); chat.ChatMembers.AddRange(shouldBeAdded); await _chatRepository.UpdateAsync(chat.Id, chat); return(shouldBeAdded); } return(null); }