Skip to content

Commit b023d52

Browse files
committed
Added dashboard view model
1 parent 1ebcff3 commit b023d52

8 files changed

Lines changed: 261 additions & 0 deletions

File tree

BlogEngine/BlogEngine.Core/BlogEngine.Core.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
<Reference Include="System.Xml" />
120120
</ItemGroup>
121121
<ItemGroup>
122+
<Compile Include="Data\Contracts\IDashboardRepository.cs" />
123+
<Compile Include="Data\DashboardRepository.cs" />
124+
<Compile Include="Data\ViewModels\DashboardVM.cs" />
122125
<Compile Include="Data\ViewModels\SettingsVM.cs" />
123126
<Compile Include="Services\Syndication\BlogML\BaseReader.cs" />
124127
<Compile Include="Services\Syndication\BlogML\BlogImporter.cs" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using BlogEngine.Core.Data.ViewModels;
2+
3+
namespace BlogEngine.Core.Data.Contracts
4+
{
5+
/// <summary>
6+
/// Dashboard repository
7+
/// </summary>
8+
public interface IDashboardRepository
9+
{
10+
/// <summary>
11+
/// Get all dashboard items
12+
/// </summary>
13+
/// <returns>Dashboard view model</returns>
14+
DashboardVM Get();
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using BlogEngine.Core.Data.ViewModels;
2+
using BlogEngine.Core.Data.Contracts;
3+
4+
namespace BlogEngine.Core.Data
5+
{
6+
/// <summary>
7+
/// Dashboard repository
8+
/// </summary>
9+
public class DashboardRepository : IDashboardRepository
10+
{
11+
/// <summary>
12+
/// Get all dashboard items
13+
/// </summary>
14+
/// <returns>Dashboard view model</returns>
15+
public DashboardVM Get()
16+
{
17+
return new DashboardVM();
18+
}
19+
}
20+
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
using BlogEngine.Core.Data.Models;
2+
using BlogEngine.Core.Notes;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Linq.Dynamic;
7+
using System.Web.Hosting;
8+
9+
namespace BlogEngine.Core.Data.ViewModels
10+
{
11+
public class DashboardVM
12+
{
13+
private List<Post> _posts;
14+
private List<Page> _pages;
15+
private List<Comment> _comments;
16+
private List<TrashItem> _trash;
17+
18+
public DashboardVM()
19+
{
20+
_posts = new List<Post>();
21+
_pages = new List<Page>();
22+
_comments = new List<Comment>();
23+
_trash = new List<TrashItem>();
24+
25+
LoadProperties();
26+
}
27+
28+
#region Properties
29+
30+
public List<Post> DraftPosts { get; set; }
31+
public int PostPublishedCnt { get; set; }
32+
public int PostDraftCnt { get; set; }
33+
34+
public List<Comment> Comments
35+
{
36+
get
37+
{
38+
return _comments.AsQueryable().OrderBy("DateCreated desc").Take(5).ToList();
39+
}
40+
}
41+
public int ApprovedCommentsCnt { get; set; }
42+
public int PendingCommentsCnt { get; set; }
43+
public int SpamCommentsCnt { get; set; }
44+
45+
public List<Page> DraftPages { get; set; }
46+
public int PagePublishedCnt { get; set; }
47+
public int PageDraftCnt { get; set; }
48+
49+
public List<TrashItem> Trash { get; set; }
50+
51+
public List<QuickNote> Notes
52+
{
53+
get
54+
{
55+
return new QuickNotes(Security.CurrentUser.Identity.Name).Notes;
56+
}
57+
}
58+
59+
public List<SelectOption> Logs
60+
{
61+
get
62+
{
63+
return GetLogFile().ToList();
64+
}
65+
}
66+
67+
#endregion
68+
69+
private void LoadProperties()
70+
{
71+
LoadPosts();
72+
LoadPages();
73+
LoadTrash();
74+
}
75+
76+
private void LoadPosts()
77+
{
78+
var posts = Post.ApplicablePosts.Where(p => p.IsVisible);
79+
DraftPosts = posts.Where(p => p.IsPublished == false).ToList();
80+
PostDraftCnt = DraftPosts.Count;
81+
PostPublishedCnt = posts.Where(p => p.IsPublished).ToList().Count;
82+
83+
foreach (var p in posts)
84+
{
85+
ApprovedCommentsCnt += p.Comments.Where(c => c.IsPublished && !c.IsDeleted).Count();
86+
PendingCommentsCnt += p.Comments.Where(c => !c.IsPublished && !c.IsSpam && !c.IsDeleted).Count();
87+
SpamCommentsCnt += p.Comments.Where(c => !c.IsPublished && c.IsSpam && !c.IsDeleted).Count();
88+
_comments.AddRange(p.Comments);
89+
}
90+
}
91+
92+
private void LoadPages()
93+
{
94+
var pages = Page.Pages.Where(p => p.IsVisible);
95+
DraftPages = pages.Where(p => p.IsPublished == false).ToList();
96+
PageDraftCnt = DraftPages.Count;
97+
PagePublishedCnt = pages.Where(p => p.IsPublished).ToList().Count;
98+
}
99+
100+
private void LoadTrash()
101+
{
102+
var posts = Post.ApplicablePosts.Where(p => p.IsDeleted);
103+
if (posts.Count() > 0)
104+
{
105+
foreach (var p in posts)
106+
{
107+
_trash.Add(new TrashItem
108+
{
109+
Id = p.Id,
110+
Title = System.Web.HttpContext.Current.Server.HtmlEncode(p.Title),
111+
RelativeUrl = p.RelativeLink,
112+
ObjectType = "Post",
113+
DateCreated = p.DateCreated.ToString("MM/dd/yyyy HH:mm")
114+
}
115+
);
116+
}
117+
}
118+
var pages = Page.Pages.Where(p => p.IsDeleted);
119+
if (pages.Count() > 0)
120+
{
121+
foreach (var page in pages)
122+
{
123+
_trash.Add(new TrashItem
124+
{
125+
Id = page.Id,
126+
Title = System.Web.HttpContext.Current.Server.HtmlEncode(page.Title),
127+
RelativeUrl = page.RelativeLink,
128+
ObjectType = "Page",
129+
DateCreated = page.DateCreated.ToString("MM/dd/yyyy HH:mm")
130+
}
131+
);
132+
}
133+
}
134+
135+
var comms = _comments.Where(c => c.IsDeleted);
136+
if (comms.Count() > 0)
137+
{
138+
foreach (var c in comms)
139+
{
140+
_trash.Add(new TrashItem
141+
{
142+
Id = c.Id,
143+
Title = c.Author + ": " + c.Teaser,
144+
RelativeUrl = c.RelativeLink,
145+
ObjectType = "Comment",
146+
DateCreated = c.DateCreated.ToString("MM/dd/yyyy HH:mm")
147+
}
148+
);
149+
}
150+
}
151+
}
152+
153+
IEnumerable<SelectOption> GetLogFile()
154+
{
155+
string fileLocation = HostingEnvironment.MapPath(Path.Combine(BlogConfig.StorageLocation, "logger.txt"));
156+
var items = new List<SelectOption>();
157+
158+
if (File.Exists(fileLocation))
159+
{
160+
using (var sw = new StreamReader(fileLocation))
161+
{
162+
string line;
163+
string logItem = "";
164+
int count = 1;
165+
while ((line = sw.ReadLine()) != null)
166+
{
167+
if (line.Contains("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"))
168+
{
169+
// new log item
170+
if (!string.IsNullOrEmpty(logItem))
171+
{
172+
var item = new SelectOption();
173+
item.OptionName = "Line" + count.ToString();
174+
item.OptionValue = logItem;
175+
items.Add(item);
176+
logItem = "";
177+
count++;
178+
}
179+
}
180+
else
181+
{
182+
// append line to log item
183+
logItem = logItem + line + "<br/>";
184+
}
185+
}
186+
sw.Close();
187+
return items;
188+
}
189+
}
190+
else
191+
{
192+
return new List<SelectOption>();
193+
}
194+
}
195+
}
196+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using App_Code;
2+
using BlogEngine.Core.Data.Contracts;
3+
using BlogEngine.Core.Data.ViewModels;
4+
using System.Net;
5+
using System.Web.Http;
6+
7+
public class DashboardController : ApiController
8+
{
9+
readonly IDashboardRepository repository;
10+
11+
public DashboardController(IDashboardRepository repository)
12+
{
13+
if (!WebUtils.CheckRightsForAdminSettingsPage(true))
14+
throw new HttpResponseException(HttpStatusCode.Unauthorized);
15+
16+
this.repository = repository;
17+
}
18+
19+
public DashboardVM Get()
20+
{
21+
return repository.Get();
22+
}
23+
}

BlogEngine/BlogEngine.NET/AppCode/App_Start/BlogEngineConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ static void RegisterDiCintainer()
229229
container.Register<IRolesRepository, RolesRepository>(Lifestyle.Transient);
230230
container.Register<IFileManagerRepository, FileManagerRepository>(Lifestyle.Transient);
231231
container.Register<ICommentFilterRepository, CommentFilterRepository>(Lifestyle.Transient);
232+
container.Register<IDashboardRepository, DashboardRepository>(Lifestyle.Transient);
232233

233234
container.Verify();
234235

BlogEngine/BlogEngine.NET/BlogEngine.NET.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,7 @@
19451945
<Compile Include="AppCode\Api\CategoriesController.cs" />
19461946
<Compile Include="AppCode\Api\CommentsController.cs" />
19471947
<Compile Include="AppCode\Api\CustomFieldsController.cs" />
1948+
<Compile Include="AppCode\Api\DashboardController.cs" />
19481949
<Compile Include="AppCode\Api\FileManagerController.cs" />
19491950
<Compile Include="AppCode\Api\FileSystemDataStreamProvider.cs" />
19501951
<Compile Include="AppCode\Api\LogsController.cs" />

BlogEngine/BlogEngine.NET/Web.Config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
<mimeMap fileExtension=".ico" mimeType="image/x-icon" />
221221
<remove fileExtension=".woff" />
222222
<mimeMap fileExtension=".woff" mimeType="font/x-woff" />
223+
<remove fileExtension=".svg" />
223224
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
224225
</staticContent>
225226
</system.webServer>

0 commit comments

Comments
 (0)