Skip to content

Commit 36ec80e

Browse files
committed
Category item to include posts counter
1 parent 7407c88 commit 36ec80e

File tree

8 files changed

+49
-17
lines changed

8 files changed

+49
-17
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@{
2-
foreach (var cat in Model)
2+
foreach (var item in Model.Categories)
33
{
4-
@Html.Raw(string.Format(cat))
4+
var cat = (Core.Data.CategoryItem)item;
5+
@Html.Raw(string.Format(Model.Template, cat.Category, cat.PostCount))
56
}
67
}

plugins/Common/Views/Widgets/PostList/Edit.cshtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
authorList.Insert(0, "All");
1414

1515
var cats = await _db.BlogPosts.Categories();
16-
var catList = cats.ToList();
16+
var catList = new List<string>();
17+
foreach (var cat in cats)
18+
{
19+
catList.Add(cat.Category);
20+
}
1721
catList.Insert(0, "All");
1822

1923
var selectedAuth = _db.CustomFields.GetCustomValue(keyAuth);

plugins/Common/Widgets/Categories.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Core.Services;
1+
using Core.Data;
2+
using Core.Services;
23
using Microsoft.AspNetCore.Mvc;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -28,13 +29,14 @@ public async Task<IViewComponentResult> InvokeAsync(string theme, string widget)
2829
if (string.IsNullOrEmpty(tmpl)) tmpl = "<a href=\"/categories/{0}\" class=\"list-group-item list-group-item-action\">{0}</a>";
2930

3031
var cats = await _db.BlogPosts.Categories();
31-
var model = new List<string>();
32+
var model = new CategoryWidgetModel { Template = tmpl, Categories = new List<CategoryItem>() };
3233

33-
foreach (var cat in cats.Take(int.Parse(max)))
34+
var catList = cats.Cast<CategoryItem>().ToList();
35+
36+
foreach (var cat in catList.Take(int.Parse(max)))
3437
{
35-
model.Add(string.Format(tmpl, cat));
38+
model.Categories.Add(cat);
3639
}
37-
3840
return View("~/Views/Widgets/Categories/Index.cshtml", model);
3941
}
4042
}
@@ -62,4 +64,10 @@ public IActionResult Edit(string selAuthors, string selCats, string txtMaxRecord
6264
return Redirect("~/admin/settings/themes");
6365
}
6466
}
67+
68+
public class CategoryWidgetModel
69+
{
70+
public string Template { get; set; }
71+
public List<CategoryItem> Categories { get; set; }
72+
}
6573
}

src/App/Views/Themes/moments/List.cshtml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@
149149
<h4 class="widget-title"><span>Categories</span></h4>
150150
<div class="widget-body">
151151
<ul>
152-
<li><a href="#">catname <span>(25)</span></a></li>
153-
<li><a href="#">catname <span>(25)</span></a></li>
154-
<li><a href="#">catname <span>(25)</span></a></li>
152+
@await Component.InvokeAsync("Categories", new { theme = "moments", widget = "Categories" })
155153
</ul>
156154
</div>
157155
</div>
@@ -160,7 +158,7 @@
160158
<h4 class="widget-title"><span>Tags</span></h4>
161159
<div class="widget-body">
162160
<ul class="clearfix">
163-
@await Component.InvokeAsync("Categories", new { theme = "moments", widget = "Categories" })
161+
@await Component.InvokeAsync("Categories", new { theme = "moments", widget = "Tags" })
164162
</ul>
165163
</div>
166164
</div>

src/App/Views/Themes/moments/Moments.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
{
1515
"Widget": "Categories",
1616
"Title": "Categories"
17+
},
18+
{
19+
"Widget": "Categories",
20+
"Title": "Tags"
1721
}
1822
]

src/App/app.db

0 Bytes
Binary file not shown.

src/Core/Data/Models/PostModel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ public string IsChecked(string status)
118118
}
119119
}
120120

121+
public class CategoryItem: IComparable<CategoryItem>
122+
{
123+
public string Category { get; set; }
124+
public int PostCount { get; set; }
125+
126+
public int CompareTo(CategoryItem other)
127+
{
128+
return Category.CompareTo(other.Category);
129+
}
130+
}
131+
121132
public enum SaveStatus
122133
{
123134
Saving = 1, Publishing = 2, Unpublishing = 3

src/Core/Data/Repositories/PostRepository.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IPostRepository : IRepository<BlogPost>
1717
Task<PostModel> GetModel(string slug);
1818
Task<PostItem> SaveItem(PostItem item);
1919
Task SaveCover(int postId, string asset);
20-
Task<IEnumerable<string>> Categories();
20+
Task<IEnumerable<CategoryItem>> Categories();
2121
}
2222

2323
public class PostRepository : Repository<BlogPost>, IPostRepository
@@ -225,9 +225,9 @@ public async Task SaveCover(int postId, string asset)
225225
await _db.SaveChangesAsync();
226226
}
227227

228-
public async Task<IEnumerable<string>> Categories()
228+
public async Task<IEnumerable<CategoryItem>> Categories()
229229
{
230-
var cats = new List<string>();
230+
var cats = new List<CategoryItem>();
231231

232232
if (_db.BlogPosts.Any())
233233
{
@@ -238,9 +238,15 @@ public async Task<IEnumerable<string>> Categories()
238238
{
239239
foreach (var pc in postcats)
240240
{
241-
if (!cats.Exists(c => c == pc))
241+
if (!cats.Exists(c => c.Category == pc))
242242
{
243-
cats.Add(pc);
243+
cats.Add(new CategoryItem { Category = pc, PostCount = 1 });
244+
}
245+
else
246+
{
247+
// updae post count
248+
var tmp = cats.Where(c => c.Category == pc).FirstOrDefault();
249+
tmp.PostCount++;
244250
}
245251
}
246252
}

0 commit comments

Comments
 (0)