Skip to content

Commit 2f12198

Browse files
committed
Admin to use APIs
1 parent 02ad3a2 commit 2f12198

File tree

6 files changed

+161
-92
lines changed

6 files changed

+161
-92
lines changed

src/App/Controllers/BlogController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public async Task<IActionResult> Index(int page = 1, string term = "")
6262
model.PostListType = PostListType.Search;
6363
}
6464

65+
model.Blog.Cover = $"{Url.Content("~/")}{model.Blog.Cover}";
66+
6567
return View(string.Format(_listView, blog.Theme), model);
6668
}
6769

@@ -120,12 +122,13 @@ public async Task<IActionResult> Categories(string name, int page = 1)
120122
{
121123
var blog = await _db.CustomFields.GetBlogSettings();
122124
var pager = new Pager(page, blog.ItemsPerPage);
123-
var posts = await _db.BlogPosts.GetListByCategory(name, pager);
125+
var posts = await _db.BlogPosts.GetList(pager, 0, name);
124126

125127
if (pager.ShowOlder) pager.LinkToOlder = $"categories/{name}?page={pager.Older}";
126128
if (pager.ShowNewer) pager.LinkToNewer = $"categories/{name}?page={pager.Newer}";
127129

128-
var model = new ListModel {
130+
var model = new ListModel
131+
{
129132
PostListType = PostListType.Category,
130133
Posts = posts,
131134
Pager = pager

src/App/Pages/Admin/Posts/Index.cshtml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
el: "#app",
9999
data: {
100100
isAdmin: isAdmin(),
101-
term: "",
102101
status: "A",
103102
page: 1,
104103
cntFrom: 0,
@@ -107,9 +106,10 @@
107106
},
108107
methods: {
109108
load: function () {
110-
var url = getUrl('api/posts?page=' + this.page);
111-
if(this.term){ url += '&term=' + this.term; }
112-
if(this.status !== "A"){ url += '&status=' + this.status; }
109+
var term = fromQueryString('term');
110+
var url = term ? getUrl('api/posts/search/' + term + '?page=' + this.page) : getUrl('api/posts?page=' + this.page);
111+
if(this.status == "D") { url += '&include=d'; }
112+
if(this.status == "P"){ url += '&include=fp'; }
113113
114114
axios.get(url)
115115
.then(response => {
@@ -145,18 +145,19 @@
145145
},
146146
search: function(){
147147
var term = $('#txtSearch').val();
148+
$('.bf-posts-form-search').submit();
148149
if(!term)
149150
return false;
150151
151-
window.location.href = getUrl('admin/posts?term=' + term);
152-
},
153-
filter: function(status){
154-
var url = getUrl('admin/posts?status=' + status);
155-
if(this.term && this.term.length > 0){
156-
url += "&term=" + this.term;
157-
}
152+
var url = getUrl('admin/posts?term=' + term + '&page=' + this.page);
153+
if(this.status == "D") { url += '&include=d'; }
154+
if(this.status == "P"){ url += '&include=fp'; }
155+
158156
window.location.href = url;
159157
},
158+
filter: function (status) {
159+
window.location.href = getUrl('admin/posts?status=' + status);
160+
},
160161
toDate: function(dt){
161162
return dt.startsWith('0001-01-01') || dt.startsWith('01-01-01') ? 'DRAFT' : getDate(dt);
162163
},
@@ -175,7 +176,6 @@
175176
},
176177
beforeMount() {
177178
if(fromQueryString('page')){ this.page = fromQueryString('page'); }
178-
if(fromQueryString('term')){ this.term = fromQueryString('term'); }
179179
if(fromQueryString('status')){ this.status = fromQueryString('status'); }
180180
this.load();
181181
}

src/Core/Api/PostsController.cs

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,61 @@ public PostsController(IDataService data)
2222
}
2323

2424
/// <summary>
25-
/// Get list of blog posts
25+
/// Search blog posts by term
2626
/// </summary>
2727
/// <param name="term">Search term</param>
28-
/// <param name="status">Status; P - published, D - drafts</param>
28+
/// <param name="author">Author</param>
29+
/// <param name="include">Posts to include: all by default; F - featured, D - drafts, P - published</param>
2930
/// <param name="page">Page number</param>
3031
/// <returns>Model with list of posts and pager</returns>
31-
[HttpGet]
32-
public async Task<ActionResult<PageListModel>> Get([FromQuery]string term = "", [FromQuery]string status = "", [FromQuery]int page = 1)
32+
[HttpGet("search/{term}")]
33+
public async Task<ActionResult<PageListModel>> Search(
34+
string term,
35+
[FromQuery]string author = "",
36+
[FromQuery]string include = "",
37+
[FromQuery]int page = 1)
3338
{
3439
try
3540
{
3641
var blog = await _data.CustomFields.GetBlogSettings();
42+
IEnumerable<PostItem> results;
3743
var pager = new Pager(page, blog.ItemsPerPage);
38-
var author = _data.Authors.Single(a => a.AppUserName == User.Identity.Name);
44+
var authorId = GetUserId(author);
45+
46+
results = await _data.BlogPosts.Search(pager, term, authorId, include, !User.Identity.IsAuthenticated);
47+
48+
return Ok(new PageListModel { Posts = results, Pager = pager });
49+
}
50+
catch (Exception)
51+
{
52+
return StatusCode(StatusCodes.Status500InternalServerError, "Database Failure");
53+
}
54+
}
55+
56+
/// <summary>
57+
/// Get list of blog posts
58+
/// </summary>
59+
/// <param name="author">Post author</param>
60+
/// <param name="category">Post category</param>
61+
/// <param name="include">Posts to include: all by default; F - featured, D - drafts, P - published</param>
62+
/// <param name="page">Page number</param>
63+
/// <returns>Model with list of posts and pager</returns>
64+
[HttpGet]
65+
public async Task<ActionResult<PageListModel>> Get(
66+
[FromQuery]string author = "",
67+
[FromQuery]string category = "",
68+
[FromQuery]string include = "",
69+
[FromQuery]int page = 1)
70+
{
71+
try
72+
{
73+
var blog = await _data.CustomFields.GetBlogSettings();
3974
IEnumerable<PostItem> results;
75+
var pager = new Pager(page, blog.ItemsPerPage);
76+
int authorId = GetUserId(author);
77+
78+
results = await _data.BlogPosts.GetList(pager, authorId, category, include, !User.Identity.IsAuthenticated);
4079

41-
if(!string.IsNullOrEmpty(term))
42-
{
43-
results = author.IsAdmin ?
44-
await _data.BlogPosts.Search(pager, term) :
45-
await _data.BlogPosts.Search(pager, term, author.Id);
46-
}
47-
else
48-
{
49-
if(!author.IsAdmin)
50-
{
51-
if(status == "P")
52-
results = await _data.BlogPosts.GetList(p => p.Published > DateTime.MinValue && p.AuthorId == author.Id, pager);
53-
else if(status == "D")
54-
results = await _data.BlogPosts.GetList(p => p.Published == DateTime.MinValue && p.AuthorId == author.Id, pager);
55-
else
56-
results = await _data.BlogPosts.GetList(p => p.AuthorId == author.Id, pager);
57-
}
58-
else
59-
{
60-
if(status == "P")
61-
results = await _data.BlogPosts.GetList(p => p.Published > DateTime.MinValue, pager);
62-
else if(status == "D")
63-
results = await _data.BlogPosts.GetList(p => p.Published == DateTime.MinValue, pager);
64-
else
65-
results = await _data.BlogPosts.GetList(p => p.Id > 0, pager);
66-
}
67-
}
6880
return Ok(new PageListModel { Posts = results, Pager = pager });
6981
}
7082
catch (Exception)
@@ -234,5 +246,17 @@ async Task<string> GetSlug(int id, string title)
234246

235247
return await Task.FromResult(slug);
236248
}
249+
250+
int GetUserId(string author)
251+
{
252+
int id = 0;
253+
if (!string.IsNullOrEmpty(author))
254+
{
255+
var objAuthor = _data.Authors.Single(a => a.AppUserName == author);
256+
if (objAuthor != null)
257+
id = objAuthor.Id;
258+
}
259+
return id;
260+
}
237261
}
238262
}

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.2</TargetFramework>
5-
<Version>2.3.1.3</Version>
5+
<Version>2.3.1.4</Version>
66
</PropertyGroup>
77

88
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

src/Core/CoreAPI.xml

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)