From 627600685d7230b556ee253d1c1a33ffda20d00c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 20 Mar 2020 18:41:32 +0100 Subject: [PATCH] Can update post content --- src/controllers/PostsController.ts | 21 +++++++++++++++++++++ src/controllers/Routes.ts | 2 ++ src/entities/RequestHandler.ts | 2 +- src/helpers/PostsHelper.ts | 18 ++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 568e439..e9695f4 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -310,9 +310,30 @@ export class PostsController { await PostsHelper.SetLevel(postID, newVisibility); + // TODO : depending on new level, delete (or not) notifications about the post + h.success(""); } + /** + * Update the content of a post + * + * @param h Request handler + */ + public static async UpdateContent(h: RequestHandler) { + const postID = await h.postPostIDWithAccess("postID", PostAccessLevel.FULL_ACCESS); + const content = h.postContent("new_content"); + + if(!check_string_before_insert(content)) + h.error(401, "Given content is invalid!"); + + await PostsHelper.SetContent(postID, content); + + // TODO : delete notifications targetting current user about the post + + h.success(); + } + /** * Send multiple posts to the API * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 18b2814..9668a21 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -196,6 +196,8 @@ export const Routes : Route[] = [ {path: "/posts/set_visibility_level", cb: (h) => PostsController.SetVisibilityLevel(h)}, + {path: "/posts/update_content", cb: (h) => PostsController.UpdateContent(h)}, + // Notifications controller diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 7314757..97b03d8 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -572,7 +572,7 @@ export class RequestHandler { * * @param message Message associated to success */ - public success(message: string) { + public success(message: string = "") { this.responseSent = true; diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index 619a8b2..c49c69b 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -460,6 +460,24 @@ export class PostsHelper { }) } + /** + * Set new content to the post + * + * @param postID Target post ID + * @param content Target content + */ + public static async SetContent(postID: number, content: string) { + await DatabaseHelper.UpdateRows({ + table: TABLE_NAME, + where: { + ID: postID + }, + set: { + texte: content + } + }) + } + /** * Turn a database entry into a row object *