forked from BlogEngine/BlogEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageRepository.cs
More file actions
213 lines (181 loc) · 6.55 KB
/
PageRepository.cs
File metadata and controls
213 lines (181 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using BlogEngine.Core.Data.Contracts;
using BlogEngine.Core.Data.Models;
using BlogEngine.Core.Data.Services;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Dynamic;
namespace BlogEngine.Core.Data
{
/// <summary>
/// Page repository
/// </summary>
public class PageRepository : IPageRepository
{
/// <summary>
/// Find page item
/// </summary>
/// <param name="take">Items per page</param>
/// <param name="skip">Items to skip</param>
/// <param name="filter">Filter expression</param>
/// <param name="order">Sort order</param>
/// <returns></returns>
public IEnumerable<PageItem> Find(int take = 10, int skip = 0, string filter = "", string order = "")
{
if (!Security.IsAuthorizedTo(Rights.ViewPublicPages))
throw new UnauthorizedAccessException();
if (string.IsNullOrEmpty(filter)) filter = "1==1";
if (string.IsNullOrEmpty(order)) order = "DateCreated desc";
var items = new List<PageItem>();
var query = Page.Pages.AsQueryable().Where(filter);
// 0 for all
if (take == 0) take = Page.Pages.Count;
foreach (var item in query.OrderBy(order).Skip(skip).Take(take))
items.Add(Json.GetPage(item));
return items;
}
/// <summary>
/// Get single page
/// </summary>
/// <param name="id">Page id</param>
/// <returns>Page object</returns>
public PageDetail FindById(Guid id)
{
if (!Security.IsAuthorizedTo(Rights.ViewPublicPages))
throw new UnauthorizedAccessException();
try
{
return Json.GetPageDetail((from p in Page.Pages.ToList() where p.Id == id select p).FirstOrDefault());
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// Add new page
/// </summary>
/// <param name="detail">Page</param>
/// <returns>Saved page with new ID</returns>
public PageDetail Add(PageDetail detail)
{
if (!Security.IsAuthorizedTo(Rights.CreateNewPages))
throw new UnauthorizedAccessException();
var page = new Page();
if (Save(page, detail))
return Json.GetPageDetail(page);
return null;
}
/// <summary>
/// Update page
/// </summary>
/// <param name="page">Page to update</param>
/// <param name="action">Action to execute</param>
/// <returns>True on success</returns>
public bool Update(PageDetail page, string action)
{
if (!Security.IsAuthorizedTo(Rights.CreateNewPages))
throw new UnauthorizedAccessException();
var corePage = (from p in Page.Pages.ToList() where p.Id == page.Id select p).FirstOrDefault();
if (action == "publish")
{
corePage.IsPublished = true;
corePage.Save();
return true;
}
if (action == "unpublish")
{
corePage.IsPublished = false;
corePage.Save();
return true;
}
if (corePage != null && Save(corePage, page))
return true;
return false;
}
/// <summary>
/// Delete item
/// </summary>
/// <param name="id">ID</param>
/// <returns>True on success</returns>
public bool Remove(Guid id)
{
if (!Security.IsAuthorizedTo(Rights.CreateNewPages))
throw new UnauthorizedAccessException();
var page = (from p in Page.Pages.ToList() where p.Id == id select p).FirstOrDefault();
if (page.HasChildPages)
throw new ApplicationException("Can not delete; page has child pages");
page.Delete();
page.Save();
return true;
}
#region private methods
static bool Save(Page page, PageDetail detail)
{
page.Title = detail.Title;
page.DateCreated = DateTime.ParseExact(detail.DateCreated, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
page.IsPublished = detail.IsPublished;
page.ShowInList = detail.ShowInList;
page.IsDeleted = detail.IsDeleted;
page.Content = detail.Content;
page.Description = GetDescription(detail.Description, detail.Content);
page.Keywords = detail.Keywords;
page.IsFrontPage = detail.IsFrontPage;
page.SortOrder = detail.SortOrder;
// if changing slug, should be unique
if (page.Slug != detail.Slug)
page.Slug = GetUniqueSlug(detail.Slug);
if (detail.Parent.OptionValue == "none")
{
page.Parent = Guid.Empty;
}
else
{
if (detail.Parent != null && detail.Parent.OptionValue != null)
{
try
{
page.Parent = Guid.Parse(detail.Parent.OptionValue);
}
catch (Exception)
{
Utils.Log("Error parsing parent ID while saving page");
}
}
}
page.Save();
return true;
}
static string GetUniqueSlug(string slug)
{
string s = Utils.RemoveIllegalCharacters(slug.Trim());
// will do for up to 100 unique post titles
for (int i = 1; i < 101; i++)
{
if (IsUniqueSlug(s))
break;
s = $"{slug}{i}";
}
return s;
}
private static bool IsUniqueSlug(string slug)
{
return Page.Pages
.FirstOrDefault(p => p.Slug != null && p.Slug.ToLower() == slug.ToLower()) == null ? true : false;
}
// if description not set, use first 100 chars in the post
static string GetDescription(string desc, string content)
{
if (string.IsNullOrEmpty(desc))
{
var p = Utils.StripHtml(content);
if (p.Length > 100)
return p.Substring(0, 100);
return p;
}
return desc;
}
#endregion
}
}