Beispiel #1
0
        public async Task <GetConversationByUserIdResponse> GetConversationByUserId()
        {
            User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository);

            if (currentUser == null)
            {
                throw new Exception("User not found");
            }

            var listData = new List <ConversationData>();

            var conversations = new List <Conversation>();

            foreach (var item in conversationRepository.GetAll())
            {
                foreach (var i in item.Participants)
                {
                    if (i.MemberId == currentUser.OId)
                    {
                        conversations.Add(item);
                    }
                }
            }

            foreach (Conversation conversation in conversations)
            {
                var latestTextMessage          = (await messagTextRepository.FindListAsync(Builders <MessageText> .Filter.Eq("conversation_id", conversation.OId))).OrderByDescending(x => x.CreatedDate).Take(1);
                var latestImageMessage         = (await messageImageRepository.FindListAsync(Builders <MessageImage> .Filter.Eq("conversation_id", conversation.OId))).OrderByDescending(x => x.CreatedDate).Take(1);
                var latestPostThumbnailMessage = (await messagePostThumbnailRepository.FindListAsync(Builders <MessagePostThumbnail> .Filter.Eq("conversation_id", conversation.OId))).OrderByDescending(x => x.CreatedDate).Take(1);
                var latestConversationActivity = (await messageConversationActivityRepository.FindListAsync(Builders <MessageConversationActivity> .Filter.Eq("conversation_id", conversation.OId))).OrderByDescending(x => x.CreatedDate).Take(1);
                var latestMultiMediaMessage    = (await messageMultiMediaRepository.FindListAsync(Builders <MessageMultiMedia> .Filter.Eq("conversation_id", conversation.OId))).OrderByDescending(x => x.CreatedDate).Take(1);

                var messageVM = new List <MessageViewModel>();
                messageVM.AddRange(mapper.Map <List <MessageViewModel> >(latestTextMessage));
                messageVM.AddRange(mapper.Map <List <MessageViewModel> >(latestImageMessage));
                messageVM.AddRange(mapper.Map <List <MessageViewModel> >(latestPostThumbnailMessage));
                messageVM.AddRange(mapper.Map <List <MessageViewModel> >(latestPostThumbnailMessage));
                messageVM.AddRange(mapper.Map <List <MessageViewModel> >(latestMultiMediaMessage));


                var conversationViewModel = mapper.Map <ConversationViewModel>(conversation);

                listData.Add(new ConversationData()
                {
                    Conversation = conversationViewModel,
                    Messages     = messageVM.OrderByDescending(x => x.CreatedDate).Take(1)
                });
            }
            return(new GetConversationByUserIdResponse()
            {
                Conversations = listData
            });
        }
        public async Task <IEnumerable <MessageViewModel> > GetMessageByConversationId(GetMessageByConversationIdRequest request)
        {
            try
            {
                var result = new List <MessageViewModel>();

                var messageTextBuilder = Builders <MessageText> .Filter;
                var messageTextFilter  = messageTextBuilder.Eq("conversation_id", request.ConversationId) & messageTextBuilder.Eq("status", ItemStatus.Active);
                var messageTexts       = await messageTextRepository.FindListAsync(messageTextFilter);

                var messageTextViewModels = mapper.Map <List <MessageViewModel> >(messageTexts.OrderByDescending(x => x.CreatedDate));
                result.AddRange(messageTextViewModels);

                var messageImageBuilder = Builders <MessageImage> .Filter;
                var messageImageFilter  = messageImageBuilder.Eq("conversation_id", request.ConversationId) & messageImageBuilder.Eq("status", ItemStatus.Active);
                var messageImages       = await messageImageRepository.FindListAsync(messageImageFilter);

                var messageImageViewModels = mapper.Map <List <MessageViewModel> >(messageImages.OrderByDescending(x => x.CreatedDate));
                result.AddRange(messageImageViewModels);

                var messagePostThumbnailBuilder = Builders <MessagePostThumbnail> .Filter;
                var messagePostThumbnailFilter  = messagePostThumbnailBuilder.Eq("conversation_id", request.ConversationId) & messagePostThumbnailBuilder.Eq("status", ItemStatus.Active);
                var messagePostThumnbails       = await messagePostThumbnailRepository.FindListAsync(messagePostThumbnailFilter);

                var messagePostThumbnailViewModels = mapper.Map <List <MessageViewModel> >(messagePostThumnbails.OrderByDescending(x => x.CreatedDate));
                result.AddRange(messagePostThumbnailViewModels);

                var messageMultiMediaBuilder = Builders <MessageMultiMedia> .Filter;
                var messageMultiMediaFilter  = messageMultiMediaBuilder.Eq("conversation_id", request.ConversationId) & messageMultiMediaBuilder.Eq("status", ItemStatus.Active);
                var messageMultiMedias       = await messageMultiMediaRepository.FindListAsync(messageMultiMediaFilter);

                var messageMultiMediaViewModels = mapper.Map <List <MessageViewModel> >(messageMultiMedias.OrderByDescending(x => x.CreatedDate));
                result.AddRange(messageMultiMediaViewModels);

                result = result.OrderByDescending(x => x.CreatedDate).ToList();

                if (request.Skip.HasValue && request.Count.HasValue)
                {
                    result = result.Skip(request.Skip.Value).Take(request.Count.Value).ToList();
                }

                return(result);
            }
            catch (Exception)
            {
                return(null);
            }
        }