Exemplo n.º 1
0
        public async Task <ConversationViewModel> AddConversation(AddConversationRequest request)
        {
            User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository);

            var firstmember = new ConversationMember()
            {
                DateJoin = DateTime.Now,
                JoinBy   = currentUser.OId,
                MemberId = currentUser.OId,
                Nickname = $"{currentUser.FirstName} {currentUser.LastName}",
                Role     = ConversationRole.Admin
            };


            request.Participants.Add(firstmember);

            Conversation existConversation = new Conversation()
            {
                Participants = new List <ConversationMember>()
            };

            foreach (Conversation conver in conversationRepository.GetAll())
            {
                if (Feature.IsEqual(conver.Participants, request.Participants) == true)
                {
                    existConversation = conver;
                    break;
                }
            }

            if (existConversation.Participants.Count != 0)
            {
                return(mapper.Map <ConversationViewModel>(existConversation));
            }

            Conversation conversation = MessageAdapter.FromRequest(request, httpContextAccessor, userRepository);

            await conversationRepository.AddAsync(conversation);

            ClientGroup clientGroup = new ClientGroup()
            {
                UserIds   = request.Participants.Select(x => x.MemberId).ToList(),
                Name      = conversation.Id.ToString(),
                GroupType = Feature.GetTypeName(conversation)
            };

            await clientGroupRepository.AddAsync(clientGroup);

            Thread.Sleep(1000);
            return(mapper.Map <ConversationViewModel>(conversation));
        }
Exemplo n.º 2
0
        public async Task AddToGroup(AddUserToGroupRequest request)
        {
            if (request.UserIds == null)
            {
                request.UserIds = new List <string>();
            }

            FilterDefinition <ClientGroup> builder = Builders <ClientGroup> .Filter.Eq("name", request.GroupName);

            ClientGroup clientGroup = await clientGroupRepository.FindAsync(builder);

            if (clientGroup != null)
            {
                foreach (string userId in request.UserIds)
                {
                    string _userId = clientGroup.UserIds.FirstOrDefault(x => x == userId);

                    if (string.IsNullOrEmpty(_userId))
                    {
                        clientGroup.UserIds.Add(userId);
                    }
                }

                await clientGroupRepository.UpdateAsync(clientGroup, clientGroup.Id);
            }
            else
            {
                ClientGroup group = new ClientGroup()
                {
                    Name      = request.GroupName,
                    GroupType = request.Type
                };
                group.UserIds.AddRange(request.UserIds.Distinct());

                await clientGroupRepository.AddAsync(group);
            }

            ;
        }
Exemplo n.º 3
0
        public async Task <ClientGroup> CreateClientGroupAsync(ClientGroup clientGroup)
        {
            var addedClientGroup = await _clientGroupRepository.AddAsync(clientGroup);

            return(addedClientGroup);
        }