Exemple #1
0
        public ActionResult SingleNews(int id)
        {
            //获取用户访问新闻行为
            string strUserId = _cookieService.GetCookie(Request, "loginInfo", "userId");

            if (strUserId != string.Empty)
            {
                _acquireBehavior.AddUserBehavior(id, Convert.ToInt32(strUserId), "SingleNews");
            }

            #region 获取相关新闻列表
            SingleNewsViewModel result = new SingleNewsViewModel();
            result.News = _newsService.GetSingleNews(id);
            IEnumerable <News> tmpRecomList = null;
            tmpRecomList = _recomService.AcquireRelatedNews(id);
            Mapper.CreateMap <News, RecomNewsViewModel>();
            result.RelatedList = Mapper.Map <IEnumerable <News>, IEnumerable <RecomNewsViewModel> >(tmpRecomList);
            #endregion

            #region 获取看过该新闻的用户看过新闻
            if (!string.IsNullOrEmpty(strUserId))
            {
                string newsType        = _newsService.GetNewsType(id);
                var    seenRelatedList = _newsService.GetRandomNews(10, newsType);
                result.SeenRelatedList = Mapper.Map <IEnumerable <News>, IEnumerable <RecomNewsViewModel> >(seenRelatedList);
            }
            #endregion
            return(View(result));
        }
        public ActionResult Single(int?id)
        {
            if (id == null)
            {
                return(HttpNotFound("ID is missing"));
            }

            News news = db.News.Find(id);

            if (news == null)
            {
                return(HttpNotFound("This news does not exists"));
            }

            SingleNewsViewModel vm = new SingleNewsViewModel
            {
                News             = news,
                NewsTags         = db.NewsTags.ToList(),
                NewsToTags       = db.NewsToTags.ToList(),
                NewsSingleHeader = db.NewsSingleHeaders.First(),
                NewsComments     = db.NewsComments.ToList(),
                RecentNewsVM     = new RecentNewsViewModel
                {
                    News = db.News.Where(n => n.ID != id).OrderByDescending(n => n.UpdatedAt).Take(5).ToList()
                }
            };

            return(View(vm));
        }
        public async Task <IActionResult> ById(string id)
        {
            SingleNewsViewModel viewModel = (await this.newsService.GetAll <SingleNewsViewModel>()).FirstOrDefault(x => x.Id == id);

            if (viewModel == null)
            {
                return(this.StatusCode(404));
            }

            viewModel.Comments = viewModel.Comments.OrderByDescending(x => x.CreatedOn);
            foreach (var comment in viewModel.Comments)
            {
                comment.Avatar = await this.profilePicturesService.GetAvatarByUserId(comment.UserId);
            }

            return(this.View(viewModel));
        }
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(HttpNotFound("ID is missing"));
            }

            News news = db.News.Find(id);

            if (news == null)
            {
                return(HttpNotFound("ID was not found"));
            }

            SingleNewsViewModel singleNewsVM = new SingleNewsViewModel
            {
                News       = news,
                NewsToTags = db.NewsToTags.Where(n => n.NewsID == news.ID).ToList()
            };

            return(View(singleNewsVM));
        }