Skip to content

Commit f97c9b3

Browse files
committed
Post edit updates
1 parent 7d10f75 commit f97c9b3

8 files changed

Lines changed: 229 additions & 293 deletions

File tree

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

Lines changed: 149 additions & 74 deletions
Large diffs are not rendered by default.

src/App/Pages/Admin/Posts/Edit.cshtml.cs

Lines changed: 0 additions & 134 deletions
This file was deleted.

src/App/Resources/admin.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,5 +428,17 @@
428428
"en-US": "Read",
429429
"ru-RU": "Читать"
430430
}
431+
},
432+
"updated": {
433+
"Values": {
434+
"en-US": "Updated",
435+
"ru-RU": "Запись обновлена"
436+
}
437+
},
438+
"created": {
439+
"Values": {
440+
"en-US": "Created",
441+
"ru-RU": "Запись создана"
442+
}
431443
}
432444
}
Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,4 @@
1-
var editorController = function (dataService) {
2-
3-
function save(publish) {
4-
$('#PostItem_Content').val(simplemde.value());
5-
$('#PostItem_Status').val(publish === 'P' ? 2 : 1);
6-
$('#frmEditor').submit();
7-
}
8-
9-
function publish() {
10-
$('#PostItem_Content').val(simplemde.value());
11-
$('#PostItem_Status').val(2);
12-
$('#frmEditor').submit();
13-
}
14-
15-
function unpublish() {
16-
$('#PostItem_Content').val(simplemde.value());
17-
$('#PostItem_Status').val(3);
18-
$('#frmEditor').submit();
19-
}
20-
21-
function remove() {
22-
$('.loading').fadeIn('fast');
23-
dataService.remove("admin/removepost/" + $('#PostItem_Id').val(), removeCallback, fail);
24-
}
25-
26-
function removeCallback(data) {
27-
toastr.success('Deleted');
28-
setTimeout(function () {
29-
$('.loading').fadeOut('fast');
30-
window.location.href = webRoot + 'admin';
31-
}, 1000);
32-
}
33-
34-
function loadCover() {
35-
var postId = $('#hdnPostId').val();
36-
var postImg = $('#hdnPostImg').val();
37-
$('#post-image').empty();
38-
if (!postImg.length > 0) {
39-
var btn = '<button type="button" title="Add Cover" class="btn btn-secondary btn-block" data-placement="bottom" onclick="return editorController.openFilePicker(' + postId + ');">Upload Post Cover</button >';
40-
}
41-
$('#post-image').append(btn);
42-
if (postImg.length > 0) {
43-
var dd = '<div class="admin-editor-cover-image"><img src="' + postImg + '" /></div>';
44-
dd += '<button type="button" class="btn btn-danger btn-block" onclick="return editorController.resetPostImage();">Remove Cover</button>';
45-
}
46-
$('#post-image').append(dd);
47-
}
48-
49-
function loadButtons(id) {
50-
if (id > 0) {
51-
$('.act-upd').css('display', 'inline-block');
52-
$('.act-new').css('display', 'none');
53-
}
54-
else {
55-
$('.act-new').css('display', 'inline-block');
56-
$('.act-upd').css('display', 'none');
57-
}
58-
}
59-
60-
function settings() {
61-
$('#postSettings').modal();
62-
return false;
63-
}
64-
65-
return {
66-
save: save,
67-
publish: publish,
68-
unpublish: unpublish,
69-
remove: remove,
70-
loadCover: loadCover,
71-
loadButtons: loadButtons,
72-
settings: settings
73-
};
74-
}(DataService);
75-
76-
function getEditor() {
1+
function getEditor(post) {
772
var simplemde = new SimpleMDE({
783
toolbar: [
794
"bold", "italic", "heading-2", "|",
@@ -127,7 +52,7 @@ function getEditor() {
12752
syncSideBySidePreviewScroll: false
12853
});
12954

130-
var txt = $('#PostItem_Content').val();
55+
var txt = post.content ? post.content : '';
13156

13257
simplemde.value(txt
13358
.replace(/&#xA;/g, '\r\n')
@@ -162,4 +87,4 @@ var scrollDiv = document.createElement("div");
16287
scrollDiv.className = "scrollbar-measure";
16388
document.body.appendChild(scrollDiv);
16489
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
165-
document.body.removeChild(scrollDiv);
90+
document.body.removeChild(scrollDiv);

src/App/wwwroot/admin/js/app/fileManagerController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
}
3535
var url = 'assets/' + id;
3636
if (callBack.name === 'updatePostCoverCallback') {
37-
url = 'assets/pick?type=postCover&asset=' + id + '&post=' + $('#PostItem_Id').val();
37+
url = 'assets/pick?type=postCover&asset=' + id + '&post=' + $('#Post_Id').val();
3838
}
3939
else if (callBack.name === 'updateAppCoverCallback') {
4040
url = 'assets/pick?type=appCover&asset=' + id;

src/Core/Api/PostsController.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ await _data.BlogPosts.Search(pager, term) :
6565
}
6666
}
6767

68+
[HttpGet("{id}")]
69+
public async Task<PostItem> GetPost(int id)
70+
{
71+
if (id > 0)
72+
{
73+
return await _data.BlogPosts.GetItem(p => p.Id == id);
74+
}
75+
else
76+
{
77+
var author = await _data.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
78+
var blog = await _data.CustomFields.GetBlogSettings();
79+
return new PostItem { Author = author, Cover = blog.Cover };
80+
}
81+
}
82+
6883
[HttpPut("publish")]
6984
[Authorize]
7085
public async Task<ActionResult> Publish(int id, string flag)
@@ -107,7 +122,23 @@ public async Task<ActionResult> Feature(int id, string flag)
107122
}
108123
await Task.CompletedTask;
109124

110-
return Ok(Resources.Updated);
125+
return Ok("Updated");
126+
}
127+
catch (Exception ex)
128+
{
129+
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
130+
}
131+
}
132+
133+
[HttpPost]
134+
[Authorize]
135+
public async Task<ActionResult<PostItem>> Post(PostItem post)
136+
{
137+
try
138+
{
139+
post.Slug = await GetSlug(post.Id, post.Title);
140+
var saved = await _data.BlogPosts.SaveItem(post);
141+
return Created($"admin/posts/edit?id={saved.Id}", saved);
111142
}
112143
catch (Exception ex)
113144
{
@@ -139,5 +170,34 @@ public async Task<IActionResult> Remove(int id)
139170
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
140171
}
141172
}
173+
174+
async Task<string> GetSlug(int id, string title)
175+
{
176+
string slug = title.ToSlug();
177+
BlogPost post;
178+
179+
if (id == 0)
180+
post = _data.BlogPosts.Single(p => p.Slug == slug);
181+
else
182+
post = _data.BlogPosts.Single(p => p.Slug == slug && p.Id != id);
183+
184+
if (post == null)
185+
return await Task.FromResult(slug);
186+
187+
for (int i = 2; i < 100; i++)
188+
{
189+
if (id == 0)
190+
post = _data.BlogPosts.Single(p => p.Slug == $"{slug}{i}");
191+
else
192+
post = _data.BlogPosts.Single(p => p.Slug == $"{slug}{i}" && p.Id != id);
193+
194+
if (post == null)
195+
{
196+
return await Task.FromResult(slug + i.ToString());
197+
}
198+
}
199+
200+
return await Task.FromResult(slug);
201+
}
142202
}
143203
}

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.0</Version>
5+
<Version>2.3.1.1</Version>
66
</PropertyGroup>
77

88
<ItemGroup>

src/Core/Data/Repositories/PostRepository.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public async Task<PostItem> SaveItem(PostItem item)
178178
var field = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogCover).FirstOrDefault();
179179
var cover = field == null ? "" : field.Content;
180180

181-
if(item.Id == 0)
181+
if (item.Id == 0)
182182
{
183183
post = new BlogPost
184184
{
@@ -202,17 +202,15 @@ public async Task<PostItem> SaveItem(PostItem item)
202202
{
203203
post = _db.BlogPosts.Single(p => p.Id == item.Id);
204204

205-
post.Title = item.Title;
206205
post.Slug = item.Slug;
206+
post.Title = item.Title;
207207
post.Content = item.Content;
208208
post.Description = item.Description ?? item.Title;
209209
post.Categories = item.Categories;
210210
post.AuthorId = item.Author.Id;
211211
post.Published = item.Published;
212212
post.IsFeatured = item.Featured;
213213
await _db.SaveChangesAsync();
214-
215-
item.Slug = post.Slug;
216214
}
217215
return await Task.FromResult(item);
218216
}

0 commit comments

Comments
 (0)