Skip to content

Commit 513ebcf

Browse files
committed
Refactoring in dashboard back-end
1 parent fb8d32d commit 513ebcf

8 files changed

Lines changed: 378 additions & 287 deletions

File tree

BlogEngine/BlogEngine.Core/BlogEngine.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<ItemGroup>
122122
<Compile Include="Data\Contracts\IDashboardRepository.cs" />
123123
<Compile Include="Data\DashboardRepository.cs" />
124+
<Compile Include="Data\Services\Json.cs" />
124125
<Compile Include="Data\ViewModels\DashboardVM.cs" />
125126
<Compile Include="Data\ViewModels\SettingsVM.cs" />
126127
<Compile Include="Services\Syndication\BlogML\BaseReader.cs" />

BlogEngine/BlogEngine.Core/Data/CommentsRepository.cs

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using BlogEngine.Core.Data.Contracts;
22
using BlogEngine.Core.Data.Models;
3+
using BlogEngine.Core.Data.Services;
34
using System;
45
using System.Collections.Generic;
5-
using System.Globalization;
66
using System.Linq;
77
using System.Linq.Dynamic;
88

@@ -24,8 +24,8 @@ public class CommentsRepository : ICommentsRepository
2424
/// <returns>List of comments</returns>
2525
public IEnumerable<CommentItem> GetComments(CommentType commentType = CommentType.All, int take = 10, int skip = 0, string filter = "", string order = "")
2626
{
27-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.ViewPublicComments))
28-
throw new System.UnauthorizedAccessException();
27+
if (!Security.IsAuthorizedTo(Rights.ViewPublicComments))
28+
throw new UnauthorizedAccessException();
2929

3030
if (string.IsNullOrEmpty(filter)) filter = "1==1";
3131
if (string.IsNullOrEmpty(order)) order = "DateCreated desc";
@@ -34,7 +34,7 @@ public IEnumerable<CommentItem> GetComments(CommentType commentType = CommentTyp
3434
var query = items.AsQueryable().Where(filter);
3535
var comments = new List<CommentItem>();
3636

37-
var all = Security.IsAuthorizedTo(BlogEngine.Core.Rights.EditOtherUsersPosts);
37+
var all = Security.IsAuthorizedTo(Rights.EditOtherUsersPosts);
3838

3939
foreach (var p in Post.Posts)
4040
{
@@ -68,7 +68,7 @@ public IEnumerable<CommentItem> GetComments(CommentType commentType = CommentTyp
6868

6969
foreach (var item in itemList)
7070
{
71-
comments.Add(CreateJsonCommentFromComment(item, itemList));
71+
comments.Add(Json.GetComment(item, itemList));
7272
}
7373

7474
return comments;
@@ -85,24 +85,24 @@ public IEnumerable<CommentItem> GetComments(CommentType commentType = CommentTyp
8585
/// </returns>
8686
public CommentItem FindById(Guid id)
8787
{
88-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.ViewPublicComments))
89-
throw new System.UnauthorizedAccessException();
88+
if (!Security.IsAuthorizedTo(Rights.ViewPublicComments))
89+
throw new UnauthorizedAccessException();
9090

9191
return (from p in Post.Posts
9292
from c in p.AllComments
9393
where c.Id == id
94-
select CreateJsonCommentFromComment(c, p.AllComments)).FirstOrDefault();
94+
select Json.GetComment(c, p.AllComments)).FirstOrDefault();
9595
}
9696

9797
/// <summary>
9898
/// Add item
9999
/// </summary>
100100
/// <param name="item">Comment</param>
101101
/// <returns>Comment object</returns>
102-
public CommentItem Add(Data.Models.CommentItem item)
102+
public CommentItem Add(CommentItem item)
103103
{
104104
if (!Security.IsAuthorizedTo(Rights.CreateComments))
105-
throw new System.UnauthorizedAccessException();
105+
throw new UnauthorizedAccessException();
106106

107107
return null;
108108
}
@@ -116,7 +116,7 @@ public CommentItem Add(Data.Models.CommentItem item)
116116
public bool Update(CommentItem item, string action)
117117
{
118118
if (!Security.IsAuthorizedTo(Rights.ModerateComments))
119-
throw new System.UnauthorizedAccessException();
119+
throw new UnauthorizedAccessException();
120120

121121
foreach (var p in Post.Posts.ToArray())
122122
{
@@ -215,62 +215,6 @@ public bool DeleteAll(string commentType)
215215

216216
#region Private methods
217217

218-
private CommentItem CreateJsonCommentFromComment(Comment c, List<Comment> postComments)
219-
{
220-
var jc = new CommentItem();
221-
222-
jc.Id = c.Id;
223-
jc.IsApproved = c.IsApproved;
224-
jc.IsSpam = c.IsSpam;
225-
jc.IsPending = !c.IsApproved && !c.IsSpam;
226-
jc.Email = c.Email == "trackback" ? "pingback" : c.Email;
227-
jc.Author = c.Author;
228-
jc.Title = c.Teaser;
229-
jc.Website = c.Website == null ? "" : c.Website.ToString();
230-
jc.AuthorAvatar = c.Avatar;
231-
jc.Ip = c.IP;
232-
jc.DateCreated = c.DateCreated.ToString("yyyy-MM-dd HH:mm");
233-
jc.RelativeLink = c.RelativeLink;
234-
jc.HasChildren = postComments.Where(pc => pc.ParentId == c.Id).FirstOrDefault() != null;
235-
jc.Avatar = Gravatar(c);
236-
237-
return jc;
238-
}
239-
240-
private Comment GetCoreFromJson(BlogEngine.Core.Data.Models.CommentItem c)
241-
{
242-
Comment item = (from p in Post.Posts
243-
from cmn in p.AllComments where cmn.Id == c.Id select cmn).FirstOrDefault();
244-
245-
if (c.IsPending)
246-
{
247-
item.IsApproved = false;
248-
item.IsSpam = false;
249-
}
250-
if (c.IsApproved)
251-
{
252-
item.IsApproved = true;
253-
item.IsSpam = false;
254-
}
255-
if (c.IsSpam)
256-
{
257-
item.IsApproved = false;
258-
item.IsSpam = true;
259-
}
260-
261-
item.Email = c.Email;
262-
item.Author = c.Author;
263-
item.Website = string.IsNullOrEmpty(c.Website) ? null : new Uri(c.Website);
264-
item.IP = c.Ip;
265-
return item;
266-
}
267-
268-
private string Gravatar(Comment comment)
269-
{
270-
var website = comment.Website == null ? "" : comment.Website.ToString();
271-
return Services.Avatar.GetSrc(comment.Email, website);
272-
}
273-
274218
// delete all pending comments
275219
private void DeletePending()
276220
{

BlogEngine/BlogEngine.Core/Data/PageRepository.cs

Lines changed: 14 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BlogEngine.Core.Data.Contracts;
22
using BlogEngine.Core.Data.Models;
3+
using BlogEngine.Core.Data.Services;
34
using System;
45
using System.Collections.Generic;
56
using System.Globalization;
@@ -23,8 +24,8 @@ public class PageRepository : IPageRepository
2324
/// <returns></returns>
2425
public IEnumerable<PageItem> Find(int take = 10, int skip = 0, string filter = "", string order = "")
2526
{
26-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.ViewPublicPages))
27-
throw new System.UnauthorizedAccessException();
27+
if (!Security.IsAuthorizedTo(Rights.ViewPublicPages))
28+
throw new UnauthorizedAccessException();
2829

2930
if (string.IsNullOrEmpty(filter)) filter = "1==1";
3031
if (string.IsNullOrEmpty(order)) order = "DateCreated desc";
@@ -36,7 +37,7 @@ public IEnumerable<PageItem> Find(int take = 10, int skip = 0, string filter = "
3637
if (take == 0) take = Page.Pages.Count;
3738

3839
foreach (var item in query.OrderBy(order).Skip(skip).Take(take))
39-
items.Add(ToJson((BlogEngine.Core.Page)item));
40+
items.Add(Json.GetPage(item));
4041

4142
return items;
4243
}
@@ -48,11 +49,11 @@ public IEnumerable<PageItem> Find(int take = 10, int skip = 0, string filter = "
4849
/// <returns>Page object</returns>
4950
public PageDetail FindById(Guid id)
5051
{
51-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.ViewPublicPages))
52-
throw new System.UnauthorizedAccessException();
52+
if (!Security.IsAuthorizedTo(Rights.ViewPublicPages))
53+
throw new UnauthorizedAccessException();
5354
try
5455
{
55-
return ToJsonDetail((from p in Page.Pages.ToList() where p.Id == id select p).FirstOrDefault());
56+
return Json.GetPageDetail((from p in Page.Pages.ToList() where p.Id == id select p).FirstOrDefault());
5657
}
5758
catch (Exception)
5859
{
@@ -67,12 +68,12 @@ public PageDetail FindById(Guid id)
6768
/// <returns>Saved page with new ID</returns>
6869
public PageDetail Add(PageDetail detail)
6970
{
70-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.CreateNewPages))
71-
throw new System.UnauthorizedAccessException();
71+
if (!Security.IsAuthorizedTo(Rights.CreateNewPages))
72+
throw new UnauthorizedAccessException();
7273

7374
var page = new Page();
7475
if (Save(page, detail))
75-
return ToJsonDetail(page);
76+
return Json.GetPageDetail(page);
7677

7778
return null;
7879
}
@@ -85,8 +86,8 @@ public PageDetail Add(PageDetail detail)
8586
/// <returns>True on success</returns>
8687
public bool Update(PageDetail page, string action)
8788
{
88-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.CreateNewPages))
89-
throw new System.UnauthorizedAccessException();
89+
if (!Security.IsAuthorizedTo(Rights.CreateNewPages))
90+
throw new UnauthorizedAccessException();
9091

9192
var corePage = (from p in Page.Pages.ToList() where p.Id == page.Id select p).FirstOrDefault();
9293

@@ -116,8 +117,8 @@ public bool Update(PageDetail page, string action)
116117
/// <returns>True on success</returns>
117118
public bool Remove(Guid id)
118119
{
119-
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.CreateNewPages))
120-
throw new System.UnauthorizedAccessException();
120+
if (!Security.IsAuthorizedTo(Rights.CreateNewPages))
121+
throw new UnauthorizedAccessException();
121122

122123
var page = (from p in Page.Pages.ToList() where p.Id == id select p).FirstOrDefault();
123124

@@ -171,62 +172,6 @@ static bool Save(Page page, PageDetail detail)
171172
return true;
172173
}
173174

174-
static PageItem ToJson(Page page)
175-
{
176-
Page parent = null;
177-
SelectOption parentOption = null;
178-
179-
if (page.Parent != Guid.Empty)
180-
{
181-
parent = Page.Pages.FirstOrDefault(p => p.Id.Equals(page.Parent));
182-
parentOption = new SelectOption { IsSelected = false, OptionName = parent.Title, OptionValue = parent.Id.ToString() };
183-
}
184-
return new PageItem
185-
{
186-
Id = page.Id,
187-
ShowInList = page.ShowInList,
188-
Title = page.Title,
189-
Slug = page.Slug,
190-
Parent = parentOption,
191-
Keywords = page.Keywords,
192-
DateCreated = page.DateCreated.ToString("yyyy-MM-dd HH:mm"),
193-
HasChildren = page.HasChildPages,
194-
IsPublished = page.IsPublished,
195-
IsFrontPage = page.IsFrontPage,
196-
SortOrder = page.SortOrder,
197-
};
198-
}
199-
200-
static PageDetail ToJsonDetail(Page page)
201-
{
202-
Page parent = null;
203-
SelectOption parentOption = null;
204-
205-
if (page.Parent != Guid.Empty)
206-
{
207-
parent = Page.Pages.FirstOrDefault(p => p.Id.Equals(page.Parent));
208-
parentOption = new SelectOption { IsSelected = false, OptionName = parent.Title, OptionValue = parent.Id.ToString() };
209-
}
210-
return new PageDetail
211-
{
212-
Id = page.Id,
213-
ShowInList = page.ShowInList,
214-
Title = page.Title,
215-
Slug = page.Slug,
216-
RelativeLink = page.RelativeLink,
217-
Content = page.Content,
218-
Parent = parentOption,
219-
Description = page.Description,
220-
Keywords = page.Keywords,
221-
DateCreated = page.DateCreated.ToString("yyyy-MM-dd HH:mm"),
222-
HasChildren = page.HasChildPages,
223-
IsPublished = page.IsPublished,
224-
IsFrontPage = page.IsFrontPage,
225-
IsDeleted = page.IsDeleted,
226-
SortOrder = page.SortOrder,
227-
};
228-
}
229-
230175
static string GetUniqueSlug(string slug)
231176
{
232177
string s = Utils.RemoveIllegalCharacters(slug.Trim());

0 commit comments

Comments
 (0)