Example #1
0
        public ActionResult All()
        {
            UserInfoOutputDto user   = Session.GetByRedis <UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto();
            List <string>     tags   = PostBll.GetAll().Select(p => p.Label).ToList();//tag
            List <string>     result = new List <string>();

            tags.ForEach(s =>
            {
                if (!string.IsNullOrEmpty(s))
                {
                    result.AddRange(s.Split(',', ','));
                }
            });
            ViewBag.tags = result.GroupBy(t => t).OrderByDescending(g => g.Count()).ThenBy(g => g.Key);
            ViewBag.cats = CategoryBll.GetAll(c => c.Post.Count, false).Select(c => new TagCloudViewModel()
            {
                Id    = c.Id,
                Name  = c.Name,
                Count = c.Post.Count(p => p.Status == Status.Pended || user.IsAdmin)
            }).ToList();//category
            ViewBag.seminars = SeminarBll.GetAll(c => c.Post.Count, false).Select(c => new TagCloudViewModel
            {
                Id    = c.Id,
                Name  = c.Title,
                Count = c.Post.Count(p => p.Status == Status.Pended || user.IsAdmin)
            }).ToList(); //seminars
            return(View());
        }
        public ActionResult Save(Seminar seminar)
        {
            bool contain;

            if (seminar.Id > 0)
            {
                //更新
                contain = SeminarBll.GetAll().Select(s => s.Title).Except(new List <string>()
                {
                    SeminarBll.GetById(seminar.Id).Title
                }).Contains(seminar.Title);
            }
            else
            {
                //添加
                contain = SeminarBll.GetAll().Select(s => s.Title).Contains(seminar.Title);
            }
            if (contain)
            {
                return(ResultData(null, false, $"{seminar.Title} 已经存在了"));
            }
            var b = SeminarBll.AddOrUpdateSaved(s => s.Id, seminar) > 0;

            return(ResultData(null, b, b ? "保存成功" : "保存失败"));
        }
        public ActionResult Index(int id, int page = 1, int size = 15, OrderBy orderBy = OrderBy.ModifyDate)
        {
            IList <Post>      posts;
            UserInfoOutputDto user = Session.GetByRedis <UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto();
            var s    = SeminarBll.GetById(id);
            var temp = s.Post.Where(p => p.Status == Status.Pended || user.IsAdmin).OrderByDescending(p => p.IsFixedTop);

            switch (orderBy)
            {
            case OrderBy.CommentCount:
                posts = temp.ThenByDescending(p => p.Comment.Count).Skip(size * (page - 1)).Take(size).ToList();
                break;

            case OrderBy.PostDate:
                posts = temp.ThenByDescending(p => p.PostDate).Skip(size * (page - 1)).Take(size).ToList();
                break;

            case OrderBy.ViewCount:
                posts = temp.ThenByDescending(p => p.PostAccessRecord.Sum(r => r.ClickCount)).Skip(size * (page - 1)).Take(size).ToList();
                break;

            case OrderBy.VoteCount:
                posts = temp.ThenByDescending(p => p.VoteUpCount).Skip(size * (page - 1)).Take(size).ToList();
                break;

            default:
                posts = temp.ThenByDescending(p => p.ModifyDate).Skip(size * (page - 1)).Take(size).ToList();
                break;
            }
            ViewBag.Total    = posts.Count;
            ViewBag.Title    = s.Title;
            ViewBag.Desc     = s.Description;
            ViewBag.SubTitle = s.SubTitle;
            return(View(posts.Mapper <IList <PostOutputDto> >()));
        }
        public ActionResult GetPageData(int page, int size)
        {
            List <SeminarOutputDto> list = SeminarBll.LoadPageEntitiesNoTracking <int, SeminarOutputDto>(page, size, out int total, s => true, s => s.Id, false).ToList();
            var pageCount = Math.Ceiling(total * 1.0 / size).ToInt32();

            return(PageResult(list, pageCount, total));
        }
Example #5
0
        public ActionResult RemoveSeminar(int id, int sid)
        {
            var     post    = PostBll.GetById(id);
            Seminar seminar = SeminarBll.GetById(sid);

            post.Seminar.Remove(seminar);
            bool b = PostBll.UpdateEntitySaved(post);

            return(ResultData(null, b, b ? $"已将文章【{post.Title}】从【{seminar.Title}】专题移除" : "添加失败"));
        }
        public ActionResult RemovePost(int id, int pid)
        {
            Seminar seminar = SeminarBll.GetById(id);
            Post    post    = PostBll.GetById(pid);

            seminar.Post.Remove(post);
            bool b = SeminarBll.UpdateEntitySaved(seminar);

            return(ResultData(null, b, b ? $"已成功将【{post.Title}】从专题【{seminar.Title}】移除" : "添加失败!"));
        }
Example #7
0
        public ActionResult Write(PostInputDto post, string Seminars, DateTime?timespan, bool schedule = false)
        {
            post.Content = ReplaceImgSrc(Regex.Replace(post.Content.Trim(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>")).Replace("/thumb150/", "/large/");//提取img标签,提取src属性并重新创建个只包含src属性的img标签
            if (!CategoryBll.Any(c => c.Id == post.CategoryId && c.Status == Status.Available))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }
            if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null"))
            {
                post.Label = null;
            }
            else if (post.Label.Trim().Length > 50)
            {
                post.Label = post.Label.Replace(",", ",");
                post.Label = post.Label.Trim().Substring(0, 50);
            }
            else
            {
                post.Label = post.Label.Replace(",", ",");
            }
            if (!post.IsWordDocument)
            {
                post.ResourceName = null;
            }
            if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }
            post.Status     = Status.Pended;
            post.PostDate   = DateTime.Now;
            post.ModifyDate = DateTime.Now;
            Post p = post.Mapper <Post>();

            if (!string.IsNullOrEmpty(Seminars))
            {
                var tmp = Seminars.Split(',').Distinct();
                tmp.ForEach(s =>
                {
                    var id = s.ToInt32();
                    p.Seminar.Add(SeminarBll.GetById(id));
                });
            }
            p.PostAccessRecord.Add(new PostAccessRecord()
            {
                AccessTime = DateTime.Today,
                ClickCount = 0
            });
            if (schedule)
            {
                if (timespan.HasValue && timespan.Value > DateTime.Now)
                {
                    p.Status = Status.Schedule;
                    HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.PublishPost), args: p);
                    return(ResultData(p.Mapper <PostOutputDto>(), message: schedule ? $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!" : "文章发表成功!"));
                }
                return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
            }
            p = PostBll.AddEntitySaved(p);
            if (p != null)
            {
                var    cast = BroadcastBll.LoadEntities(c => c.Status == Status.Subscribed).ToList();
                string link = Request.Url?.Scheme + "://" + Request.Url?.Authority + "/" + p.Id;
                cast.ForEach(c =>
                {
                    var ts         = DateTime.Now.GetTotalMilliseconds();
                    string content = System.IO.File.ReadAllText(Request.MapPath("/template/broadcast.html")).Replace("{{link}}", link + "?email=" + c.Email).Replace("{{time}}", post.PostDate.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{title}}", post.Title).Replace("{{author}}", post.Author).Replace("{{content}}", post.Content.RemoveHtmlTag(150)).Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new { c.Email, act = "cancel", validate = c.ValidateCode, timespan = ts, hash = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(ConfigurationManager.AppSettings["BaiduAK"]) }, Request.Url.Scheme));
                    BackgroundJob.Schedule(() => SendMail(GetSettings("Title") + "博客有新文章发布了", content, c.Email), (p.PostDate - DateTime.Now));
                });
                HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.UpdateLucene));
                return(ResultData(null, true, "文章发表成功!"));
            }
            return(ResultData(null, false, "文章发表失败!"));
        }
Example #8
0
        public ActionResult Edit(PostInputDto post, string Seminars, bool notify = true)
        {
            post.Content = ReplaceImgSrc(Regex.Replace(post.Content.Trim(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>")).Replace("/thumb150/", "/large/");
            if (!CategoryBll.Any(c => c.Id == post.CategoryId && c.Status == Status.Available))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }
            if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null"))
            {
                post.Label = null;
            }
            else if (post.Label.Trim().Length > 50)
            {
                post.Label = post.Label.Replace(",", ",");
                post.Label = post.Label.Trim().Substring(0, 50);
            }
            else
            {
                post.Label = post.Label.Replace(",", ",");
            }
            if (!post.IsWordDocument)
            {
                post.ResourceName = null;
            }

            if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }
            post.ModifyDate = DateTime.Now;
            Post p       = PostBll.GetById(post.Id);
            var  history = p.Mapper <PostHistoryVersion>();

            p.PostHistoryVersion.Add(history);
            Mapper.Map(post, p);
            if (!string.IsNullOrEmpty(Seminars))
            {
                var tmp = Seminars.Split(',').Distinct();
                p.Seminar.Clear();
                tmp.ForEach(s =>
                {
                    p.Seminar.Add(SeminarBll.GetFirstEntity(e => e.Title.Equals(s)));
                });
            }

            bool b = PostBll.UpdateEntitySaved(p);

            if (b)
            {
#if !DEBUG
                if (notify)
                {
                    var    cast = BroadcastBll.LoadEntities(c => c.Status == Status.Subscribed).ToList();
                    string link = Request.Url?.Scheme + "://" + Request.Url?.Authority + "/" + p.Id;
                    cast.ForEach(c =>
                    {
                        var ts         = DateTime.Now.GetTotalMilliseconds();
                        string content = System.IO.File.ReadAllText(Request.MapPath("/template/broadcast.html")).Replace("{{link}}", link + "?email=" + c.Email).Replace("{{time}}", post.PostDate.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{title}}", post.Title).Replace("{{author}}", post.Author).Replace("{{content}}", post.Content.RemoveHtmlTag(150)).Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new { c.Email, act = "cancel", validate = c.ValidateCode, timespan = ts, hash = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(ConfigurationManager.AppSettings["BaiduAK"]) }, Request.Url.Scheme));
                        BackgroundJob.Schedule(() => SendMail(GetSettings("Title") + "博客有新文章发布了", content, c.Email), (p.PostDate - DateTime.Now));
                    });
                }
#endif
                HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.UpdateLucene));
                return(ResultData(p.Mapper <PostOutputDto>(), message: "文章修改成功!"));
            }
            return(ResultData(null, false, "文章修改失败!"));
        }
        public ActionResult Delete(int id)
        {
            bool b = SeminarBll.DeleteByIdSaved(id);

            return(ResultData(null, b, b ? "删除成功" : "删除失败"));
        }
        public ActionResult GetAll()
        {
            List <SeminarOutputDto> list = SeminarBll.GetAll <string, SeminarOutputDto>(s => s.Title).ToList();

            return(ResultData(list));
        }
        public ActionResult Get(int id)
        {
            Seminar seminar = SeminarBll.GetById(id);

            return(ResultData(seminar.Mapper <SeminarOutputDto>()));
        }