예제 #1
0
        public string RenderPostList(int page, string action, long pid)
        {
            if (page < 1)
            {
                page = 1;
            }

            string error   = null;
            string success = null;

            if (action == "delete")
            {
                bool rs = _pressService.DeletePost(pid);
                if (!rs)
                {
                    error = "删除失败";
                }
                else
                {
                    success = "删除ID为" + pid + "文章成功";
                }
            }
            else if (action == "visible")
            {
                bool rs = _pressService.SwitchVisibleByID(pid);
                if (!rs)
                {
                    error = "设置失败";
                }
                else
                {
                    success = "设置成功";
                }
            }

            var allPosts = _pressService.ReadPostsFromDatabase();

            int pageSize = 8;

            int maxPage = 0;

            maxPage = (int)Math.Ceiling((double)allPosts.Count / pageSize);

            var tmpPageList = allPosts.Skip((page - 1) * pageSize).Take(pageSize).ToList();

            List <Post> pageList = new List <Post>();

            if (!TinyfxCore.Configuration.Encryption)
            {
                pageList = tmpPageList;
            }
            else
            {
                Faes faes = new Faes();

                foreach (var item in tmpPageList)
                {
                    var citem = new Post();
                    citem.Id      = item.Id;
                    citem.Visible = item.Visible;
                    citem.Content = faes.Decrypt(item.Content);
                    citem.Title   = faes.Decrypt(item.Title);
                    pageList.Add(citem);
                }
            }

            List <string[]> pageData = new List <string[]>();

            for (int i = 0; i < pageList.Count(); i++)
            {
                pageData.Add(new string[] {
                    pageList[i].Id.ToString(),
                    pageList[i].Title,
                    new DateTime(pageList[i].Id).ToString(),
                    pageList[i].Visible.ToString()
                });
            }

            List <string[]> naviData = new List <string[]>();

            if (page > maxPage)
            {
                page = maxPage;
            }

            if (maxPage >= 1)
            {
                PageNumber pageNumber = new PageNumber();
                pageNumber.UrlPrefix = "/admin/post-list?page=";
                naviData             = pageNumber.GetPageNumbers(page, maxPage);
            }

            string postListHtmlTmpl = ResourceHelper.LoadStringResource("postlist.html");
            string adminHtmlTmpl    = ResourceHelper.LoadStringResource("admin.html");

            return(adminHtmlTmpl.AsHtmlFromTemplate(new
            {
                Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
                RenderBody = postListHtmlTmpl.AsHtmlFromTemplate(new
                {
                    PageData = pageData.ToArray(),
                    NaviData = naviData,
                    CurrentPage = page.ToString(),
                    Error = error,
                    Success = success,
                    Configuration = _tinyConfiguration
                }),
                Configuration = _tinyConfiguration
            }));
        }