diff --git a/OliverBooth/Controllers/Api/v1/BlogPostController.cs b/OliverBooth/Controllers/Api/v1/BlogPostController.cs index b30107e..75066c9 100644 --- a/OliverBooth/Controllers/Api/v1/BlogPostController.cs +++ b/OliverBooth/Controllers/Api/v1/BlogPostController.cs @@ -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>(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" }); diff --git a/OliverBooth/Data/Blog/BlogPost.cs b/OliverBooth/Data/Blog/BlogPost.cs index 79161d1..7be2a16 100644 --- a/OliverBooth/Data/Blog/BlogPost.cs +++ b/OliverBooth/Data/Blog/BlogPost.cs @@ -38,7 +38,7 @@ internal sealed class BlogPost : IBlogPost public IReadOnlyList Tags { get; internal set; } = ArraySegment.Empty; /// - public string Title { get; internal set; } = string.Empty; + public string Title { get; set; } = string.Empty; /// public DateTimeOffset? Updated { get; internal set; } diff --git a/OliverBooth/Data/Blog/IBlogPost.cs b/OliverBooth/Data/Blog/IBlogPost.cs index 05c252a..8daaaad 100644 --- a/OliverBooth/Data/Blog/IBlogPost.cs +++ b/OliverBooth/Data/Blog/IBlogPost.cs @@ -70,10 +70,10 @@ public interface IBlogPost IReadOnlyList Tags { get; } /// - /// Gets the title of the post. + /// Gets or sets the title of the post. /// /// The title of the post. - string Title { get; } + string Title { get; set; } /// /// Gets the date and time the post was last updated. diff --git a/src/ts/admin/EditBlogPost.ts b/src/ts/admin/EditBlogPost.ts index e58d90c..0de9fea 100644 --- a/src/ts/admin/EditBlogPost.ts +++ b/src/ts/admin/EditBlogPost.ts @@ -52,7 +52,7 @@ declare const Prism: any; saveButton.setAttribute("disabled", "disabled"); saveButton.innerHTML = ' 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"); diff --git a/src/ts/app/API.ts b/src/ts/app/API.ts index aba4deb..4863db5 100644 --- a/src/ts/app/API.ts +++ b/src/ts/app/API.ts @@ -30,9 +30,9 @@ class API { return new Author(response); } - static async updatePost(post: BlogPost, content: string): Promise { + static async updatePost(post: BlogPost, options: any): Promise { try { - await API.patch(`/post/${post.id}`, {body: content}); + await API.patch(`/post/${post.id}`, {body: JSON.stringify(options)}); } catch { return post; }