PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 73 lines | 59 code | 6 blank | 8 comment | 5 complexity | cd03b7150156515266b95caf9f0c86f5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace 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. foreach (var p in Page.Pages)
  22. {
  23. switch (type)
  24. {
  25. case "Published":
  26. if (p.IsPublished)
  27. allPages.Add(p);
  28. break;
  29. case "Draft":
  30. if (!p.IsPublished)
  31. allPages.Add(p);
  32. break;
  33. default:
  34. allPages.Add(p);
  35. break;
  36. }
  37. }
  38. allPages.Sort((x, y) => DateTime.Compare(y.DateCreated, x.DateCreated));
  39. foreach (var x in allPages)
  40. {
  41. string prId = "";
  42. string prTtl = "";
  43. if (x.Parent != Guid.Empty)
  44. {
  45. prId = x.Parent.ToString();
  46. prTtl = Page.Pages.FirstOrDefault(p => p.Id.Equals(x.Parent)).Title;
  47. }
  48. var jp = new JsonPage
  49. {
  50. Id = x.Id,
  51. ShowInList = x.ShowInList,
  52. IsPublished = x.IsPublished,
  53. Title = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, System.Web.HttpContext.Current.Server.HtmlEncode(x.Title)),
  54. Date = x.DateCreated.ToString("dd MMM yyyy"),
  55. Time = x.DateCreated.ToString("t"),
  56. ParentId = prId,
  57. ParentTitle = prTtl,
  58. HasChildren = x.HasChildPages,
  59. CanUserDelete = x.CanUserDelete,
  60. CanUserEdit = x.CanUserEdit
  61. };
  62. jPages.Add(jp);
  63. }
  64. return jPages;
  65. }
  66. }
  67. }