PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Json/JsonPages.cs

#
C# | 73 lines | 59 code | 6 blank | 8 comment | 5 complexity | cd03b7150156515266b95caf9f0c86f5 MD5 | raw file
 1namespace BlogEngine.Core.Json
 2{
 3    using System;
 4    using System.Collections.Generic;
 5    using System.Linq;
 6    using System.Text;
 7    /// <summary>
 8    /// Json friendly list of pages
 9    /// </summary>
10    public class JsonPages
11    {
12        /// <summary>
13        /// Get list of pages
14        /// </summary>
15        /// <param name="type">Published or Drafts</param>
16        /// <returns>List of pages</returns>
17        public static List<JsonPage> GetPages(string type)
18        {
19            var allPages = new List<Page>();
20            var jPages = new List<JsonPage>();
21
22            foreach (var p in Page.Pages)
23            {
24                switch (type)
25                {
26                    case "Published":
27                        if (p.IsPublished)
28                            allPages.Add(p);
29                        break;
30                    case "Draft":
31                        if (!p.IsPublished)
32                            allPages.Add(p);
33                        break;
34                    default:
35                        allPages.Add(p);
36                        break;
37                }
38            }
39
40            allPages.Sort((x, y) => DateTime.Compare(y.DateCreated, x.DateCreated));
41
42            foreach (var x in allPages)
43            {
44                string prId = "";
45                string prTtl = "";
46
47                if (x.Parent != Guid.Empty)
48                {
49                    prId = x.Parent.ToString();
50                    prTtl = Page.Pages.FirstOrDefault(p => p.Id.Equals(x.Parent)).Title;
51                }
52
53                var jp = new JsonPage
54                {
55                    Id = x.Id,
56                    ShowInList = x.ShowInList,
57                    IsPublished = x.IsPublished,
58                    Title = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, System.Web.HttpContext.Current.Server.HtmlEncode(x.Title)),
59                    Date = x.DateCreated.ToString("dd MMM yyyy"),
60                    Time = x.DateCreated.ToString("t"),
61                    ParentId = prId,
62                    ParentTitle = prTtl,
63                    HasChildren = x.HasChildPages,
64                    CanUserDelete = x.CanUserDelete,
65                    CanUserEdit = x.CanUserEdit
66                };
67                jPages.Add(jp);
68            }
69
70            return jPages;
71        }
72    }
73}