From db9be6bb315297ef83cc3816fd30dabcf7e89a1e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Mar 2020 08:40:31 +0100 Subject: [PATCH] Can delete comments --- src/entities/Comment.ts | 4 ++++ src/helpers/CommentsHelper.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/entities/Comment.ts b/src/entities/Comment.ts index 5b3baaa..fa3362c 100644 --- a/src/entities/Comment.ts +++ b/src/entities/Comment.ts @@ -36,4 +36,8 @@ export class Comment implements CommentBuilder { get imageURL() : string { return pathUserData(this.imagePath); } + + get imageSysPath() : string { + return pathUserData(this.imagePath, true); + } } \ No newline at end of file diff --git a/src/helpers/CommentsHelper.ts b/src/helpers/CommentsHelper.ts index 2d99428..56d7e2c 100644 --- a/src/helpers/CommentsHelper.ts +++ b/src/helpers/CommentsHelper.ts @@ -1,5 +1,7 @@ import { Comment } from "../entities/Comment"; import { DatabaseHelper } from "./DatabaseHelper"; +import { unlinkSync, existsSync } from "fs"; +import { LikesHelper, LikesType } from "./LikesHelper"; /** * Comments helper @@ -28,13 +30,38 @@ export class CommentsHelper { return results.map(this.DbToComment); } + /** + * Permanently delete a comment + * + * @param comment Information about the comment to delete + */ + public static async Delete(comment: Comment) { + + // Delete associated image (if any) + if(comment.hasImage && existsSync(comment.imageSysPath)) { + unlinkSync(comment.imageSysPath); + } + + // Delete likes associated with the comment + await LikesHelper.DeleteAll(comment.id, LikesType.COMMENT); + + // Delete the comment from the database + await DatabaseHelper.DeleteRows(COMMENTS_TABLE, { + ID: comment.id + }) + } + /** * Delete all the comments associated to a post * * @param postID Target post ID */ public static async DeleteAll(postID: number) { - // TODO : implement + + // Get the comments of the post + for(const comment of await this.Get(postID)) { + await this.Delete(comment); + } } /**