Skip to content

Commit f21c9db

Browse files
committed
Postlit get/save data
1 parent faead3a commit f21c9db

File tree

7 files changed

+165
-14
lines changed

7 files changed

+165
-14
lines changed
Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
11
@model Core.Data.ThemeWidget
2-
@{
3-
var selectedType = "All";
4-
var maxRecords = "10";
5-
string[] listTypes = new string[] { "All", "By author", "By category" };
2+
@using System.Linq
3+
@inject Core.Services.IDataService _db
4+
@{
5+
var keyAuth = $"{Model.Theme}-{Model.Widget.Title}-auth";
6+
var keyCat = $"{Model.Theme}-{Model.Widget.Title}-cat";
7+
var keyMax = $"{Model.Theme}-{Model.Widget.Title}-max";
8+
9+
var authors = _db.Authors.All().Select(a => a.DisplayName).ToArray();
10+
var authorOrdered = authors.ToList().OrderBy(a => a);
11+
var authorList = authorOrdered.ToList();
12+
authorList.Insert(0, "All");
13+
14+
var cats = await _db.BlogPosts.Categories();
15+
var catList = cats.ToList();
16+
catList.Insert(0, "All");
17+
18+
var selectedAuth = _db.CustomFields.GetCustomValue(keyAuth);
19+
var selectedCat = _db.CustomFields.GetCustomValue(keyCat);
20+
var maxRecords = _db.CustomFields.GetCustomValue(keyMax);
21+
22+
if (string.IsNullOrEmpty(selectedAuth)) { selectedAuth = "All"; }
23+
if (string.IsNullOrEmpty(selectedCat)) { selectedCat = "All"; }
24+
if (string.IsNullOrEmpty(maxRecords)) { maxRecords = "10"; }
625
}
7-
<form method="post" asp-antiforgery="true">
26+
<form method="post" action="~/widgets/api/postlist/edit" asp-antiforgery="true">
827
<div class="form-group">
9-
<label class="form-group-label">List Type</label>
10-
<select id="selTypes" name="selTypes" class="form-control">
11-
@foreach (var t in listTypes)
28+
<label class="form-group-label">Authors</label>
29+
<select id="selAuthors" name="selAuthors" class="form-control">
30+
@foreach (var a in authorList)
1231
{
13-
if (selectedType == t)
32+
if (selectedAuth == a)
1433
{
15-
<option value="@t" selected>@t</option>
34+
<option value="@a" selected>@a</option>
1635
}
1736
else
1837
{
19-
<option value="@t">@t</option>
38+
<option value="@a">@a</option>
39+
}
40+
}
41+
</select>
42+
</div>
43+
<div class="form-group">
44+
<label class="form-group-label">Categories</label>
45+
<select id="selCats" name="selCats" class="form-control">
46+
@foreach (var c in catList)
47+
{
48+
if (selectedCat == c)
49+
{
50+
<option value="@c" selected>@c</option>
51+
}
52+
else
53+
{
54+
<option value="@c">@c</option>
2055
}
2156
}
2257
</select>
@@ -28,4 +63,6 @@
2863
<div class="form-group">
2964
<button type="submit" class="btn btn-primary btn-main">Save</button>
3065
</div>
66+
<input type="hidden" id="hdnWidget" name="hdnWidget" value="@Model.Widget.Title" />
67+
<input type="hidden" id="hdnTheme" name="hdnTheme" value="@Model.Theme" />
3168
</form>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<div class="list-group">
33
@foreach (var post in Model)
44
{
5-
<a href="~/posts/@post.Slug" class="list-group-item list-group-item-action" title="">@post.Title</a>
5+
<a href="~/posts/@post.Slug" class="list-group-item list-group-item-action" title="">@post.Title</a>
66
}
77
</div>

plugins/Common/Widgets/PostList.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Core.Services;
22
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using System.Linq;
35

46
namespace Common.Widgets
57
{
@@ -15,9 +17,73 @@ public PostList(IDataService db)
1517

1618
public IViewComponentResult Invoke(string theme, string widget)
1719
{
18-
var model = _db.BlogPosts.All();
20+
var keyAuth = $"{theme}-{widget}-auth";
21+
var keyCat = $"{theme}-{widget}-cat";
22+
var keyMax = $"{theme}-{widget}-max";
23+
24+
var selectedAuth = _db.CustomFields.GetCustomValue(keyAuth);
25+
var selectedCat = _db.CustomFields.GetCustomValue(keyCat);
26+
var maxRecords = _db.CustomFields.GetCustomValue(keyMax);
27+
28+
if (selectedAuth == "All") { selectedAuth = ""; }
29+
if (selectedCat == "All") { selectedCat = ""; }
30+
if (string.IsNullOrEmpty(maxRecords)) { maxRecords = "10"; }
31+
32+
var posts = _db.BlogPosts.All()
33+
.Where(p => p.Published > DateTime.MinValue)
34+
.OrderByDescending(p => p.Published).ToList();
35+
36+
var auth = _db.Authors.All().Where(a => a.DisplayName == selectedAuth).FirstOrDefault();
37+
38+
if (!string.IsNullOrEmpty(selectedAuth) && !string.IsNullOrEmpty(selectedCat))
39+
{
40+
var p1 = posts.Where(p => p.Categories != null && p.Categories.Contains(selectedCat) && p.AuthorId == auth.Id);
41+
if (p1 != null) posts = p1.ToList();
42+
}
43+
else if (!string.IsNullOrEmpty(selectedAuth))
44+
{
45+
var p2 = posts.Where(p => p.AuthorId == auth.Id).ToList();
46+
if (p2 != null) posts = p2.ToList();
47+
}
48+
else if (!string.IsNullOrEmpty(selectedCat))
49+
{
50+
var p3 = posts.Where(p => p.Categories != null && p.Categories.Contains(selectedCat)).ToList();
51+
if (p3 != null) posts = p3.ToList();
52+
}
53+
54+
int maxRec;
55+
if (!int.TryParse(maxRecords, out maxRec))
56+
maxRec = 10;
57+
58+
var model = posts.Take(maxRec).ToList();
1959

2060
return View("~/Views/Widgets/PostList/Index.cshtml", model);
2161
}
2262
}
63+
64+
[Route("widgets/api/postlist")]
65+
public class PostListController : Controller
66+
{
67+
IDataService _db;
68+
69+
public PostListController(IDataService db)
70+
{
71+
_db = db;
72+
}
73+
74+
[HttpPost]
75+
[Route("edit")]
76+
public IActionResult Edit(string selAuthors, string selCats, string txtMaxRecords, string hdnWidget, string hdnTheme)
77+
{
78+
var keyAuth = $"{hdnTheme}-{hdnWidget}-auth";
79+
var keyCat = $"{hdnTheme}-{hdnWidget}-cat";
80+
var keyMax = $"{hdnTheme}-{hdnWidget}-max";
81+
82+
_db.CustomFields.SaveCustomValue(keyAuth, selAuthors);
83+
_db.CustomFields.SaveCustomValue(keyCat, selCats);
84+
_db.CustomFields.SaveCustomValue(keyMax, txtMaxRecords);
85+
86+
return Redirect("~/admin/settings/themes");
87+
}
88+
}
2389
}

src/App/app.db

0 Bytes
Binary file not shown.

src/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<Version>2.1.0.9</Version>
5+
<Version>2.1.1.0</Version>
66
</PropertyGroup>
77

88
<ItemGroup>

src/Core/Data/Repositories/CustomFieldRepository.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public interface ICustomFieldRepository : IRepository<CustomField>
77
{
88
Task<BlogItem> GetBlogSettings();
99
Task SaveBlogSettings(BlogItem blog);
10+
11+
string GetCustomValue(string name);
12+
Task SaveCustomValue(string name, string value);
1013
}
1114

1215
public class CustomFieldRepository : Repository<CustomField>, ICustomFieldRepository
@@ -65,5 +68,25 @@ public async Task SaveBlogSettings(BlogItem blog)
6568

6669
await _db.SaveChangesAsync();
6770
}
71+
72+
public string GetCustomValue(string name)
73+
{
74+
var field = _db.CustomFields.Where(f => f.Name == name).FirstOrDefault();
75+
return field == null ? "" : field.Content;
76+
}
77+
78+
public async Task SaveCustomValue(string name, string value)
79+
{
80+
var field = _db.CustomFields.Where(f => f.Name == name).FirstOrDefault();
81+
if(field == null)
82+
{
83+
_db.CustomFields.Add(new CustomField { Name = name, Content = value, AuthorId = 0 });
84+
}
85+
else
86+
{
87+
field.Content = value;
88+
}
89+
await _db.SaveChangesAsync();
90+
}
6891
}
6992
}

src/Core/Data/Repositories/PostRepository.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +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();
2021
}
2122

2223
public class PostRepository : Repository<BlogPost>, IPostRepository
@@ -224,6 +225,30 @@ public async Task SaveCover(int postId, string asset)
224225
await _db.SaveChangesAsync();
225226
}
226227

228+
public async Task<IEnumerable<string>> Categories()
229+
{
230+
var cats = new List<string>();
231+
232+
if (_db.BlogPosts.Any())
233+
{
234+
foreach (var p in _db.BlogPosts.Where(p => p.Categories != null))
235+
{
236+
var postcats = p.Categories.Split(',');
237+
if (postcats.Any())
238+
{
239+
foreach (var pc in postcats)
240+
{
241+
if (!cats.Exists(c => c == pc))
242+
{
243+
cats.Add(pc);
244+
}
245+
}
246+
}
247+
}
248+
}
249+
return await Task.FromResult(cats.OrderBy(c => c));
250+
}
251+
227252
PostItem PostToItem(BlogPost p)
228253
{
229254
var post = new PostItem

0 commit comments

Comments
 (0)