示例#1
0
 public EventPreviewListViewModel(EventsPreviewWithLoadDto eventList)
 {
     Events      = eventList.Events;
     CanLoadMore = eventList.CanLoadMore;
     AvatarPath  = eventList.AvatarPath;
 }
示例#2
0
        public async Task <EventsPreviewWithLoadDto> GetEventPreviewList(Guid userId, int page = 0, int sizeEvents = 5, int sizeComments = 5, string category = null, string query = null)
        {
            var eventList = await _unitOfWork.EventRepository.GetEventPreviewList(page, sizeEvents, category, query);

            string connectionId = null;

            var found = EventHub.userIdConnectionId.TryGetValue(userId.ToString(), out connectionId);

            foreach (var e in eventList)
            {
                await _hubContext.Groups.AddToGroupAsync(connectionId, $"event-{e.Id}");
            }

            var userAvatarPath = await _unitOfWork.UserRepository.GetUserById(userId.ToString()).ContinueWith(x => x.Result?.AvatarPath);

            var canLoadMore = eventList.Count() > sizeEvents ? true : false;

            eventList = canLoadMore == true?eventList.Take(sizeEvents) : eventList;

            var EventsPreviewDtoList = new EventsPreviewWithLoadDto()
            {
                CanLoadMore = canLoadMore,
                AvatarPath  = userAvatarPath
            };

            foreach (var eventEntity in eventList)
            {
                var userReaction = await _unitOfWork.EventReactionRepository.GetUserReactionAsync(userId, eventEntity.Id);

                var reactions = await _unitOfWork.EventReactionRepository.GetEventReactions(eventEntity.Id);

                var groupedReactions = reactions
                                       .GroupBy(x => x.Type)
                                       .Select(x => new EventReactionDto()
                {
                    Count        = x.Count(),
                    ReactionType = x.Key.ToString().ToLower()
                })
                                       .OrderByDescending(y => y.Count);

                var commentsCount = await _unitOfWork.CommentRepository.GetCommentCountAsync(eventEntity.Id);

                var comments = await _unitOfWork.CommentRepository.GetEventCommentsAsync(eventEntity.Id, 0, sizeComments);

                var commentsDto = comments.Select(x => new CommentDto(x));

                var moreCommentsDto = new MoreCommentsDto(commentsDto, sizeComments)
                {
                    TotalCount = commentsCount
                };

                EventsPreviewDtoList.Events.Add(new EventsPreviewDto(eventEntity, moreCommentsDto)
                {
                    CurrentReaction = userReaction?.Type.ToString().ToLower(),
                    CommentsCount   = commentsCount,
                    Reactions       = groupedReactions,
                    ReactionsCount  = reactions.Count(),
                });
            }
            return(EventsPreviewDtoList);
        }