feat: add post title edit capability

This commit is contained in:
Oliver Booth 2024-02-26 17:44:22 +00:00
parent 593036a712
commit 7cb6e9d463
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
5 changed files with 15 additions and 9 deletions

View File

@ -1,4 +1,5 @@
using System.Text; using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Asp.Versioning; using Asp.Versioning;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -42,10 +43,15 @@ public sealed class BlogPostController : ControllerBase
return new JsonResult(new { status = 404, message = "Not Found" }); return new JsonResult(new { status = 404, message = "Not Found" });
} }
using var reader = new StreamReader(Request.Body, Encoding.UTF8); var body = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(Request.Body);
string content = await reader.ReadToEndAsync(); 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); _blogPostService.UpdatePost(post);
return new JsonResult(new { status = 200, message = "OK" }); return new JsonResult(new { status = 200, message = "OK" });

View File

@ -38,7 +38,7 @@ internal sealed class BlogPost : IBlogPost
public IReadOnlyList<string> Tags { get; internal set; } = ArraySegment<string>.Empty; public IReadOnlyList<string> Tags { get; internal set; } = ArraySegment<string>.Empty;
/// <inheritdoc /> /// <inheritdoc />
public string Title { get; internal set; } = string.Empty; public string Title { get; set; } = string.Empty;
/// <inheritdoc /> /// <inheritdoc />
public DateTimeOffset? Updated { get; internal set; } public DateTimeOffset? Updated { get; internal set; }

View File

@ -70,10 +70,10 @@ public interface IBlogPost
IReadOnlyList<string> Tags { get; } IReadOnlyList<string> Tags { get; }
/// <summary> /// <summary>
/// Gets the title of the post. /// Gets or sets the title of the post.
/// </summary> /// </summary>
/// <value>The title of the post.</value> /// <value>The title of the post.</value>
string Title { get; } string Title { get; set; }
/// <summary> /// <summary>
/// Gets the date and time the post was last updated. /// Gets the date and time the post was last updated.

View File

@ -52,7 +52,7 @@ declare const Prism: any;
saveButton.setAttribute("disabled", "disabled"); saveButton.setAttribute("disabled", "disabled");
saveButton.innerHTML = '<i class="fa-solid fa-spinner fa-spin fa-fw"></i> Saving ...'; 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.add("btn-success");
saveButton.classList.remove("btn-primary"); saveButton.classList.remove("btn-primary");

View File

@ -30,9 +30,9 @@ class API {
return new Author(response); return new Author(response);
} }
static async updatePost(post: BlogPost, content: string): Promise<BlogPost> { static async updatePost(post: BlogPost, options: any): Promise<BlogPost> {
try { try {
await API.patch(`/post/${post.id}`, {body: content}); await API.patch(`/post/${post.id}`, {body: JSON.stringify(options)});
} catch { } catch {
return post; return post;
} }