feat: add post title edit capability
This commit is contained in:
parent
593036a712
commit
7cb6e9d463
|
@ -1,4 +1,5 @@
|
|||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -42,10 +43,15 @@ public sealed class BlogPostController : ControllerBase
|
|||
return new JsonResult(new { status = 404, message = "Not Found" });
|
||||
}
|
||||
|
||||
using var reader = new StreamReader(Request.Body, Encoding.UTF8);
|
||||
string content = await reader.ReadToEndAsync();
|
||||
var body = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(Request.Body);
|
||||
if (body is null)
|
||||
{
|
||||
Response.StatusCode = 400;
|
||||
return new JsonResult(new { status = 400, message = "Bad Request" });
|
||||
}
|
||||
|
||||
post.Body = content;
|
||||
post.Body = body["content"];
|
||||
post.Title = body["title"];
|
||||
_blogPostService.UpdatePost(post);
|
||||
|
||||
return new JsonResult(new { status = 200, message = "OK" });
|
||||
|
|
|
@ -38,7 +38,7 @@ internal sealed class BlogPost : IBlogPost
|
|||
public IReadOnlyList<string> Tags { get; internal set; } = ArraySegment<string>.Empty;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Title { get; internal set; } = string.Empty;
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTimeOffset? Updated { get; internal set; }
|
||||
|
|
|
@ -70,10 +70,10 @@ public interface IBlogPost
|
|||
IReadOnlyList<string> Tags { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the title of the post.
|
||||
/// Gets or sets the title of the post.
|
||||
/// </summary>
|
||||
/// <value>The title of the post.</value>
|
||||
string Title { get; }
|
||||
string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the date and time the post was last updated.
|
||||
|
|
|
@ -52,7 +52,7 @@ declare const Prism: any;
|
|||
saveButton.setAttribute("disabled", "disabled");
|
||||
saveButton.innerHTML = '<i class="fa-solid fa-spinner fa-spin fa-fw"></i> Saving ...';
|
||||
|
||||
post = await API.updatePost(post, content.value);
|
||||
post = await API.updatePost(post, {content: content.value, title: title.value});
|
||||
|
||||
saveButton.classList.add("btn-success");
|
||||
saveButton.classList.remove("btn-primary");
|
||||
|
|
|
@ -30,9 +30,9 @@ class API {
|
|||
return new Author(response);
|
||||
}
|
||||
|
||||
static async updatePost(post: BlogPost, content: string): Promise<BlogPost> {
|
||||
static async updatePost(post: BlogPost, options: any): Promise<BlogPost> {
|
||||
try {
|
||||
await API.patch(`/post/${post.id}`, {body: content});
|
||||
await API.patch(`/post/${post.id}`, {body: JSON.stringify(options)});
|
||||
} catch {
|
||||
return post;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue