コード例 #1
0
ファイル: PostController.cs プロジェクト: plu2/Masuit.MyBlogs
        public ActionResult Details(int id, string kw)
        {
            List <string> list = new List <string>();

            if (!string.IsNullOrEmpty(kw))
            {
                list = LuceneHelper.CutKeywords(kw);
                if (Regex.Match(kw, BanRegex).Length > 0 || Regex.Match(kw, ModRegex).Length > 0)
                {
                    ViewBag.Keyword = "";
                    return(RedirectToAction("Details", "Post", new { id }));
                }
            }
            Post post = PostBll.GetById(id);

            if (post != null)
            {
                ViewBag.Keyword = post.Keyword + "," + post.Label;
                UserInfoOutputDto user       = Session.GetByRedis <UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto();
                DateTime          modifyDate = post.ModifyDate;
                ViewBag.Next = PostBll.GetFirstEntityFromL2CacheNoTracking(p => p.ModifyDate > modifyDate && (p.Status == Status.Pended || user.IsAdmin), p => p.ModifyDate);
                ViewBag.Prev = PostBll.GetFirstEntityFromL2CacheNoTracking(p => p.ModifyDate < modifyDate && (p.Status == Status.Pended || user.IsAdmin), p => p.ModifyDate, false);
                if (user.IsAdmin)
                {
                    return(View("Details_Admin", post));
                }
                if (post.Status != Status.Pended)
                {
                    return(RedirectToAction("Post", "Home"));
                }
                if (string.IsNullOrEmpty(Session.GetByRedis <string>("post" + id)))
                {
                    //post.ViewCount++;
                    var record = post.PostAccessRecord.FirstOrDefault(r => r.AccessTime == DateTime.Today);
                    if (record != null)
                    {
                        record.ClickCount += 1;
                    }
                    else
                    {
                        post.PostAccessRecord.Add(new PostAccessRecord
                        {
                            ClickCount = 1,
                            AccessTime = DateTime.Today
                        });
                    }
                    PostBll.UpdateEntitySaved(post);
                    Session.SetByRedis("post" + id, id.ToString());
                }
                foreach (var s in list)
                {
                    if (s.Contains(new[] { @"\?", @"\*", @"\+", @"\-", @"\[", @"\]", @"\{", @"\}", @"\(", @"\)", "�" }))
                    {
                        continue;
                    }
                    post.Content = Regex.IsMatch(s, @"^\p{P}.*") ? post.Content.Replace(s, $"<span style='color:red;background-color:yellow;font-size: 1.1em;font-weight:700;'>{s}</span>") : Regex.Replace(post.Content, s, $"<span style='color:red;background-color:yellow;font-size: 1.1em;font-weight:700;'>{s}</span>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
                }
                return(View(post));
            }
            return(RedirectToAction("Index", "Error"));
        }