示例#1
0
        public IActionResult GetGroupNews([FromBody] GetNewsViewModel model)
        {
            var claims  = User.Claims;
            var userId  = claims.FirstOrDefault().Value;
            var groupId = int.Parse(model.Group);
            var news    = _context.GroupNews.Where(x => x.GroupId == groupId).OrderByDescending(x => x.DateOfCreate).Take(10);

            return(Ok(new NewsViewModel()
            {
                News = news.Select(t => new GetNewsModel()
                {
                    Content = t.Content,
                    DateOfCreate = t.DateOfCreate.ToString("dd.MM.yyyy"),
                    Id = t.Id,
                    Teacher = t.TeacherProfile.BaseProfile.Name + " " + t.TeacherProfile.BaseProfile.Surname + " " + t.TeacherProfile.BaseProfile.LastName,
                    Topic = t.Topic
                }).ToList()
            }));
        }
示例#2
0
        public async Task <IActionResult> GetNews([FromBody] GetNewsViewModel newsVm)
        {
            if (!ModelState.IsValid)
            {
                return(new BadResponseResult(ModelState));
            }

            Logger.LogInformation($"{nameof(NewsQuestionsController)}.{nameof(GetNews)}.Start");
            var userId          = newsVm.UserId;
            var questionsOffset = newsVm.PageParams.Offset;
            var questionsCount  = newsVm.PageParams.Count;

            var askedList       = new List <NewsAskedQuestionViewModel>();
            var answeredList    = new List <NewsAnsweredQuestionViewModel>();
            var favoriteList    = new List <NewsFavoriteQuestionViewModel>();
            var commentedList   = new List <NewsCommentedQuestionViewModel>();
            var recommentedList = new List <NewsRecommendedQuestionViewModel>();

            Parallel.Invoke(() =>
            {
                using (var context = new TwoButtonsContext(DbOptions))
                {
                    askedList = GetNewsAskedQuestionsAsync(context, userId, newsVm.BackgroundSizeType);
                }
            }, () =>
            {
                using (var context = new TwoButtonsContext(DbOptions))
                {
                    answeredList = GetNewsAnsweredQuestionsAsync(context, userId, newsVm.BackgroundSizeType);
                }
            }, () =>
            {
                using (var context = new TwoButtonsContext(DbOptions))
                {
                    favoriteList = GetNewsFavoriteQuestionsAsync(context, userId, newsVm.BackgroundSizeType);
                }
            }, () =>
            {
                using (var context = new TwoButtonsContext(DbOptions))
                {
                    commentedList = GetNewsCommentedQuestions(context, userId, newsVm.BackgroundSizeType);
                }
            }, () =>
            {
                using (var context = new TwoButtonsContext(DbOptions))
                {
                    recommentedList = TryGetNewsRecommendedQuestions(context, userId, newsVm.BackgroundSizeType);
                }
            });

            var newsList = new List <NewsQuestionBaseViewModel>(askedList.Count + answeredList.Count + commentedList.Count +
                                                                favoriteList.Count + recommentedList.Count);

            newsList.AddRange(askedList);
            newsList.AddRange(answeredList);
            newsList.AddRange(commentedList);
            newsList.AddRange(favoriteList);
            newsList.AddRange(recommentedList);

            var mainList = newsList.OrderBy(x => x.Author.UserId).ToList();

            var removeList = new List <NewsQuestionBaseViewModel>();
            var compare    = new NewsQuestionBaseCompare();

            for (var i = 0; i < mainList.Count - 1; i++)
            {
                if (compare.GetHashCode(mainList[i]) == compare.GetHashCode(mainList[i + 1]) ||
                    compare.Equals(mainList[i], mainList[i + 1]))
                {
                    removeList.Add(mainList[i].Priority > mainList[i + 1].Priority ? mainList[i + 1] : mainList[i]);
                }
            }

            var resultList = mainList.Except(removeList).OrderByDescending(x => x.Priority).Skip(questionsOffset)
                             .Take(questionsCount).ToList();

            var answeredListResultList    = new List <NewsAnsweredQuestionViewModel>();
            var favoriteListResultList    = new List <NewsFavoriteQuestionViewModel>();
            var commentedListResultList   = new List <NewsCommentedQuestionViewModel>();
            var recommentedListResultList = new List <NewsRecommendedQuestionViewModel>();

            var askedListResultList = new List <NewsAskedQuestionViewModel>();

            var length = questionsCount > resultList.Count ? resultList.Count : questionsCount;

            for (var i = 0; i < length; i++)
            {
                resultList[i].Position = i + questionsOffset;
                switch (resultList[i].NewsType)
                {
                case NewsType.Answered:
                    answeredListResultList.Add((NewsAnsweredQuestionViewModel)resultList[i]);
                    break;

                case NewsType.Asked:
                    askedListResultList.Add((NewsAskedQuestionViewModel)resultList[i]);
                    break;

                case NewsType.Commented:
                    commentedListResultList.Add((NewsCommentedQuestionViewModel)resultList[i]);
                    break;

                case NewsType.Favorite:
                    favoriteListResultList.Add((NewsFavoriteQuestionViewModel)resultList[i]);
                    break;

                case NewsType.Recommended:
                    recommentedListResultList.Add((NewsRecommendedQuestionViewModel)resultList[i]);
                    break;
                }
            }

            var result = new NewsViewModel
            {
                NewsAskedQuestions       = askedListResultList,
                NewsAnsweredQuestions    = answeredListResultList,
                NewsFavoriteQuestions    = favoriteListResultList,
                NewsCommentedQuestions   = commentedListResultList,
                NewsRecommendedQuestions = recommentedListResultList
            };

            await Hub.Monitoring.UpdateUrlMonitoring(newsVm.UserId, UrlMonitoringType.GetsQuestionsNews);

            Logger.LogInformation($"{nameof(NewsQuestionsController)}.{nameof(GetNews)}.End");
            return(new OkResponseResult(result));
        }